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

Hi Everybody,

I tried to create a user (home-manager) systemd service like this

  systemd.user.services.foobar = {
      Unit = { Description = "my service"; };
      Service = {
        Type = "exec";
        ExecStart = "${cfg.foobar}/bin/foobar";
        Restart = "on-failure";
      };
      Install = { WantedBy = [ "default.target" ]; };
    };

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:

[Service]
Environment="LOCALE_ARCHIVE=/nix/store/ja7cry6cb9wwclhlphmffgg4fv0ky4cd-glibc-locales-2.37-8/lib/locale/locale-archive"
Environment="PATH=/nix/store/j4fwy5gi1rdlrlbk2c0vnbs7fmlm60a7-coreutils-9.1/bin:/nix/store/x6rwgp1jl5sgzwbsaigqkdbdc7krzwj7-findutils-4.9.0/bin:/nix/store/8mzvz6kk57p9aqdk72pq1adsl38bkzi6-gnugrep-3.7/bin:/nix/store/vqj2w8rqghmmp4wkn9lkcym5kzlqk372-gnused-4.9/bin:/nix/store/rpagyb9792jx4f2hlqz9q0ld3frlzxq5-systemd-253.6/bin:/nix/store/j4fwy5gi1rdlrlbk2c0vnbs7fmlm60a7-coreutils-9.1/sbin:/nix/store/x6rwgp1jl5sgzwbsaigqkdbdc7krzwj7-findutils-4.9.0/sbin:/nix/store/8mzvz6kk57p9aqdk72pq1adsl38bkzi6-gnugrep-3.7/sbin:/nix/store/vqj2w8rqghmmp4wkn9lkcym5kzlqk372-gnused-4.9/sbin:/nix/store/rpagyb9792jx4f2hlqz9q0ld3frlzxq5-systemd-253.6/sbin"
Environment="TZDIR=/nix/store/3yx6fa7gxgp4p6d79skvscvdd21alclp-tzdata-2023c/share/zoneinfo"

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:

systemd.user.services.<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).

1 Like

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

https://nix-community.github.io/home-manager/options.html

looking for systemd.user.services.<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.

Edit:

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.

1 Like