What is the difference between `systemd.services` and `systemd.user.services`?

What is the difference between systemd.services and systemd.user.services?
Both are used quite regularly throughout nixos modules, but I never see the latter being used in conjunction with a configured user. Is it the same as an actual user service, or is it more similar to DynamicUser=yes or is there something entirely different going on?

It’s an actual user service, that is, it’s started by the user’s systemd instance. It’s installed for every user though, by putting the unit file in /etc/systemd/user. If you want a user service for a single user, home-manager is probably the best way to do it.

1 Like

One other thing worth noting about nixos-installed systemd user services:

They don’t start/restart on nixos-rebuild switch like system ones do. You need to relog, causing your systemd user instance to restart from scratch, or manually handle them.

Yet another reason home-manager is often a better way to handle user services.

1 Like

So for a service that is to run as a specific user, it can be run as system service for that user, user service for that user, and home management service for that user? When would it be the right call to let it run as a user service and not using home manager?

When you don’t have and don’t want to use home-manager, I suppose.

And to be clear, home-manager services are systemd user services, too. Just installed a different way.

I suppose they only run for one user rather than all users though, right?
(this implies that I expect services under systemd.user.services to run for all users once logged in, please correct me if I am wrong)

There is also the third option of setting a user to run the system service as: systemd.services.<name>.serviceConfig.User.

Yes. Though it’s a bit confusing because systemd.user.services is also the name of the home-manager option for creating services for just that user. The nixos option by that name affects all users, though.

Yes, that’s also an option. In particular, this is how home-manager activates itself when used as a nixos module.

1 Like