Why does NixOS and home-manager declare systemd services differently?

In NixOS configurations systemd (both system and user) services are declared with nixified attributes like this:

systemd.user.services.foo = {
  description = "Foo";
  wantedBy = [ "default.target" ];
  
  serviceConfig = {
    ExecStart = "/bin/foo";
  };
};

But in home-manager a more systemd-like syntax is used:

systemd.user.services.foo = {
  Unit = {
    Description = "Foo";
    WantedBy = [ "default.target" ];
  };
  
  Service = {
    ExecStart = "/bin/foo";
  };
};

Why is that? And is there a way to write a universal service you can then import in both NixOS and home-manager?

1 Like

Because they’re independent, separate projects run by different people, and HM’s impl is simpler to write from the maintainers’ perspective. NixOS has more contributors and doesn’t need to hide from complexity where it’s useful.

Why would you want this? Rarely is a system service suitable to run as a user service, moreover with user services you get fewer options to work with.

3 Likes

Thanks!

I have a couple of python scripts running as services. I use system-manager for my non-NixOS server but would like to be able to run them as user services with home-manager on other machines.

Why not simply run it as a hm-managed user-service on all systems?

With home-manager you need to specify the user in the config but i want to be able to install everything without changing in the files.

Also hm-managed user-service dont work on root which is often the only user on VPS i buy