nix-community / home-manager

Manage a user environment using Nix [maintainer=@rycee]
https://nix-community.github.io/home-manager/
MIT License
6.41k stars 1.71k forks source link

Switch `systemd.user.startServices` default to `sd-switch` #5452

Open rycee opened 2 months ago

rycee commented 2 months ago

Description

With version 0.4.0 of sd-switch, it should be more robust and work out of the box for most people. We should therefore switch to using it by default.

Sd-switch is a tool that automatically starts/stops/reloads systemd services when activating a Home Manager configuration.

You can use it today by adding systemd.user.startServices = "sd-switch" to your configuration. If it works well for you then consider adding a thumbs up reaction to this issue. If it does not work OK, then please add a comment outlining the issue you have. Make sure you have an up-to-date version of Nixpkgs unstable.

drupol commented 2 months ago

Enabled, so far so good on my side.

rycee commented 2 months ago

@drupol Thanks, that's good 🙂

Janik-Haag commented 2 months ago

@rycee I have noticed a small behavior change since switching to sd-switch. When I stop a user unit declared with home-manager, and then do a nixos-rebuild switch the unit starts again which didn't happen before.

rycee commented 1 month ago

@Janik-Haag Thanks for the feedback. This is probably an effect of sd-switch not starting new services directly, rather it starts all active targets. Doing it that way makes systemd figure out which of the new service units should be started and starts them.

Starting the target also starts stopped services, which is what you see. Shouldn't be terribly tricky to fix. I'll make sd-switch do the figuring out and start parts instead.

rycee commented 1 month ago

@Janik-Haag I've released a new version of sd-switch that is a bit more restrictive about starting unmanaged services. It will still start up managed services that are wanted by an active target, though. I'm not certain there is much that can be done about that since I don't know of a good way to detect "manually stopped" from "not running for other reasons". The new version should be available in all channels now.

CyberShadow commented 3 weeks ago

I don't know of a good way to detect "manually stopped" from "not running for other reasons".

That sounds like an interesting and tricky problem, can you share more details?

Would it make sense for sd-switch to use the following logic:

  1. If a unit was enabled in the previous configuration, but disabled/absent in the new configuration, then stop it
  2. If a unit was disabled/absent in the previous configuration, but enabled in the new configuration, then start it

By the way, I noticed that sd-switch is described as a "third party application". But isn't it technically first-party, since it was created by the author of home-manager? :slightly_smiling_face:

jtrees commented 3 weeks ago

My use case is simple but using sd-switch works perfectly for it. The default method failed to restart the service when my config changed.

Here's my config snippet:

systemd.user.services.hyprpaper = {
  Unit = {
    Description = "desktop wallpaper";
    X-Restart-Triggers = [ "${config.home-manager.users.foo.xdg.configFile.hyprpaper.source}" ];
  };
  Install = {
    WantedBy = [ "graphical-session.target" ];
  };
  Service = {
    ExecStart = lib.getExe pkgs.hyprpaper;
  };
};

Thanks!