Systemd/User Services: Difference between revisions
Denperidge (talk | contribs) Modified NixOS Search link to not specify channel=unstable. This way the default option for the website will be selected, which should usually be the latest default NixOS channel. Might be more in tune with user expectancy, as people using unstable are advanced enough to select the channel manually |
No edit summary |
||
Line 1: | Line 1: | ||
<translate> | |||
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: https://wiki.archlinux.org/title/Systemd/User | 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: https://wiki.archlinux.org/title/Systemd/User | ||
</translate> | |||
<translate> | |||
In NixOS, a user service can be expressed with {{ic|systemd.user.services.<name>}}, as documented here: https://search.nixos.org/options?query=systemd.user.services | In NixOS, a user service can be expressed with {{ic|systemd.user.services.<name>}}, as documented here: https://search.nixos.org/options?query=systemd.user.services | ||
</translate> | |||
<translate> | |||
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. | 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. | ||
</translate> | |||
<translate> | |||
Here is an example: | Here is an example: | ||
</translate> | |||
<translate> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
systemd.user.services.my-cool-user-service = { | systemd.user.services.my-cool-user-service = { | ||
Line 19: | Line 24: | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</translate> | |||
<translate> | |||
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 {{ic|<nowiki>wantedBy = [ "default.target" ]</nowiki>}} in the example. | 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 {{ic|<nowiki>wantedBy = [ "default.target" ]</nowiki>}} in the example. | ||
</translate> | |||
<translate> | |||
== Keeping user services running after logout == | == Keeping user services running after logout == | ||
</translate> | |||
<translate> | |||
If you need a user service to stay running after a user logs out, you need to enable "[https://search.nixos.org/options?channel=unstable&show=users.users.%3Cname%3E.linger&from=0&size=50&sort=relevance&type=packages&query=users.users.%3Cname%3E.linger lingering]" by setting {{ic|<nowiki>users.users.<username>.linger = true;</nowiki>}} | If you need a user service to stay running after a user logs out, you need to enable "[https://search.nixos.org/options?channel=unstable&show=users.users.%3Cname%3E.linger&from=0&size=50&sort=relevance&type=packages&query=users.users.%3Cname%3E.linger lingering]" by setting {{ic|<nowiki>users.users.<username>.linger = true;</nowiki>}} | ||
</translate> | |||
<translate> | |||
You'll also likely want to change to {{ic|<nowiki>wantedBy = [ "multi-user.target" ];</nowiki>}} so the service starts at boot time. | You'll also likely want to change to {{ic|<nowiki>wantedBy = [ "multi-user.target" ];</nowiki>}} so the service starts at boot time. | ||
</translate> | |||
[[Category:systemd]] | [[Category:systemd]] |
Revision as of 16:42, 5 August 2024
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: https://wiki.archlinux.org/title/Systemd/User
In NixOS, a user service can be expressed with systemd.user.services.<name>
, as documented here: https://search.nixos.org/options?query=systemd.user.services
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:
systemd.user.services.my-cool-user-service = {
enable = true;
after = [ "network.target" ];
wantedBy = [ "default.target" ];
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 = [ "default.target" ]
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 = [ "multi-user.target" ];
so the service starts at boot time.