-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
systemd user-units can't be easily enabled/disabled per user #21460
Comments
There might be some related work in #9250 I would be interested in seeing this progress as well. |
Also, for the record: users can do the install "by hand" with:
which is all that 'systemctl --user enable' is really doing under the hood. |
I recently tried sddm and this issue bit me. All user services started for the sddm user, and continued to run even after I logged in. This kept the same services from starting for my user account due to Can we create systemd user target that is started when "real" users log in and have all NixOS user services use |
I see that current behaviour for 20.03 is that user services enabled for all users. |
Oh dear. |
I marked this as stale due to inactivity. → More info |
still important to me |
I marked this as stale due to inactivity. → More info |
This caused some confusion here since pipewire is launching on the root user, even though it won't get access to a dbus instance and therefore do nothing: https://discourse.nixos.org/t/cannot-get-sound-to-work-at-all-please-help/14453 |
I've also just hit this problem. My usecase is that I have a set top box on which I want to run an application that plays audio (so it needs pulseaudio, which needs a non-root user). The end state should be a user systemd service that is only enabled for a specific user. I've tried numerous things and right now I settled for this very suboptimal solution: # TODO: do this declaratively
system.activationScripts.multimedia.text = ''
rm -rf /home/multimedia/.config/systemd
mkdir -p /home/multimedia/.config/systemd/user/default.target.wants
ln -s /etc/systemd/user/taped.service /home/multimedia/.config/systemd/user/default.target.wants/
chown -R multimedia:multimedia /home/multimedia/.config/systemd
''; I'm running pipewire-pulse to provide the pulseaudio socket so |
AFAICT this is still relevant? The UX of defining an auto-enable and auto-start user service could be improved. Maybe we could add an optional
The submodule logic would then override Or, alternatively have an |
This is not a general solution to this problem, but for this specific use case, where you have a systemd service that you want to start on boot and run as a single specific user, you can just do something like: systemd.services.taped.serviceConfig = {
User = "multimedia";
Group = "users";
}; I.e. don't use a user-unit at all, use a system unit that runs as that user. I've used this approach for both snapclient and calibre-server in the past. Depending on what taped does, you might also need to add some stuff to its This bug is primarily a problem when you want to declare a user-unit, and want to run it for >1, but not necessarily all, users, and/or want users to be able to manage it themselves using |
Yep.
I like this one more -- wantedBy is orthogonal to which users the unit is enabled for. The special-keywords I think a completed version of this would look something like: systemd.services.foo = {
# Choose who to enable the service for declaratively; the service will be instantiated
# for these users and started on login (by default) or on boot (for users with linger enabled),
# subject as always to wantedBy and similar settings.
enableFor = [ "user1" "user2" ]; # list of users to enable the service for
# enableFor = "systemUsers"; # or all system users
# enableFor = "normalUsers"; # or all normal users
# enableFor = "allUsers"; # or everyone
# Control whether users can easily enable the service with `systemctl --user enable`
# Note that this is not a security boundary; if this is off users can still enable it
# on an individual basis using `ln -s`.
allowUserEnable = true;
}; I'm honestly not sure if the latter setting should exist at all or if we should just unconditionally emit an I'm also not sure how |
Thinking about this a bit more, and refreshing my memory on what the module looks like:
And there are two problems here, which are kind of conflated in this bug but which, I think, are different problems:
The first problem is fixed by The second problem involves editing the contents of the user's home directory and thus may more properly belong to |
FYI for anyone who needs a workaround: {
systemd.user.services.${name}.unitConfig.ConditionUser = "!@system"; # or ["user1" "!user2"]
} |
or |
I'm running into this issue now on a multi-user system with user services intended to be enabled by users individually. So I'm leaving this here in hopes of coming across a solution or workaround. After a reboot all services are enabled for every user, but not for the root user as described in the original post. System users seem to be unaffected as well. This is unfortunate because it unnecessarily hogs resources when there are a lot of users. Manually disabling services is moot:
An example of such a service: systemd.user.services.jellyfin = {
enable = true;
after = [ "network.target" ];
wantedBy = [ "default.target" ];
description = "Jellyfin";
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.jellyfin}/bin/jellyfin";
};
}; Removing
I'm considering the following:
Edit: loosely related issue. I'm going to look into per-user services using home-manager first. I'd like to hear how others deal with this. |
The unit files emitted by
systemd.user.services.*
don't have an[Install]
section, so users can't* enable/disable it withsystemctl --user (enable|disable)
(they can still create the ~/.config/systemd/user/default.target.wants/ symlinks by hand if needed).It might seem that you could fix this in
configuration.nix
with something like:But that turns it on for all users, including root(!) and sddm(!!).
I know NixOS doesn't use
[Install]
in general, but for user-units, which are meant to be used and, to some extent, configured by individual, nonprivileged users, it makes sense to permit it; furthermore -- although I'm not sure what a good way to accomplish this -- it would be nice if the system administrator had some way of saying "auto-enable this user-unit only for the following users".The text was updated successfully, but these errors were encountered: