How to add PATH into systemd user (home-manager) service

Hi Everybody,

I tried to create a user (home-manager) systemd service like this = {
      Unit = { Description = "my service"; };
      Service = {
        Type = "exec";
        ExecStart = "${cfg.foobar}/bin/foobar";
        Restart = "on-failure";
      Install = { WantedBy = [ "" ]; };

And it works :wink: . The only problem is that the /bin/foobar needs to be able to execute basic things like bash or date.

Looking into system systemd services, they have the following lines:


Is it possible to get the the Environment="PATH= ?

Or should I just manually specify Service = { Environment = ?

Thank you.

There are (at least) two broad ways to go about fixing this:

  1. fix up the service’s path/environment

  2. recognize that foobar has runtime dependencies that aren’t being properly specified and try to fix the package itself

Since you’re asking about the former, I’ll focus on that first. I’m not certain since I don’t use HM, but I imagine in supports most of the same systemd service options as NixOS. The most relevant is:<name>.path
Packages added to the service’s PATH environment variable. Both the bin and sbin subdirectories of each package are added.

Type: list of (package or string)

Default: [ ]

The latter is the more useful fix, but the specifics can depend on what language/ecosystem foobar comes from and whether this is just your own private package or something defined in nixpkgs (in which case I’d say the unspecified dependency is a ~bug).

Thank you @abathur for the answer.

I can’t re-build it. It’s a simple tool written by client of mine and I don’t have source.

I looked at

looking for<name>.path

and it seems to me that the Home Manager way for defining systemd services is just a simple conversion from attr. set to systemd format (INI or TOML) so I will need to specify Service = { Environment = "PATH=...

1 Like

The very dirty solution I just found is to do exactly what you described, declare your path as Environment=PATH=/run/current-system/sw/bin/ or /home/your_user/.nix-profile/bin/.

Very weird to me that it doesn’t work as it does with system services.


The solution was right in front of my eyes the whole time:

I had read that thread before but didn’t quite understand that this is exactly what we were looking for.