Systemd/User Services

From NixOS Wiki

Systemd supports running a separate instance of systemd for a given user, allowing the user to control their own services. See here for more information:

In NixOS, a user service can be expressed with<name>, as documented here:

This may be useful if you want a user to be able to start, stop, and restart their own instance of a service without needing to make the user a sudoer.

Here is an example: = {
  enable = true;
  after = [ "" ];
  wantedBy = [ "" ];
  description = "My Cool User Service";
  serviceConfig = {
      Type = "simple";
      ExecStart = ''/my/cool/user/service'';

By default, user services will be stopped when the user logs out and will start again when the user logs back in due to us setting wantedBy = [ "" ] in the example.

Keeping user services running after logout

If you need a user service to stay running after a user logs out, you need to enable "lingering" by setting users.users.<username>.linger = true;

You'll also likely want to change to wantedBy = [ "" ]; so the service starts at boot time.