Systemd/User Services: Difference between revisions
Mac-chaffee (talk | contribs) Created page with "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 {{ic|systemd.user.services.<name>}}, as documented here: https://search.nixos.org/options?channel=unstable&query=systemd.user.services This may be useful if you want a user to be able to start, stop, and restart their own inst..." |
show how to enable a service for only specific users |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{Systemd/breadcrumb}} | |||
<translate> | |||
<!--T:1--> | |||
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> | |||
In NixOS, a user service can be expressed with {{ic|systemd.user.services.<name>}}, as documented here: https://search.nixos.org/options? | <translate> | ||
<!--T:2--> | |||
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> | |||
<!--T:3--> | |||
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> | |||
<!--T:4--> | |||
Here is an example: | Here is an example: | ||
</translate> | |||
<translate> | |||
<!--T:5--> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
systemd.user.services.my-cool-user-service = { | systemd.user.services.my-cool-user-service = { | ||
Line 19: | Line 31: | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</translate> | |||
<translate> | |||
<!--T:6--> | |||
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> | |||
== Keeping user services running after logout == | <translate> | ||
== Keeping user services running after logout == <!--T:7--> | |||
</translate> | |||
<translate> | |||
<!--T:8--> | |||
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> | |||
<!--T:9--> | |||
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> | |||
== Enabling a service for specific users == | |||
By default, enabling a user service enables it for every user for which systemd spawns a service manager. If you wish for the service to be run only for specific users (say, {{ic|<nowiki>UserA</nowiki>}} and {{ic|<nowiki>UserB</nowiki>}}), use {{ic|<nowiki>ConditionUser</nowiki>}} ({{ic|<nowiki>man 5 systemd.unit</nowiki>}}): | |||
<syntaxhighlight lang="nix"> | |||
systemd.user.services.my-cool-user-service = { | |||
unitConfig.ConditionUser = "UserA|UserB"; | |||
}; | |||
</syntaxhighlight> | |||
[[Category:systemd]] |