Creating a systemd service that runs a nix command?

You need either:

systemd.services.myservice = {
  enable = true;
  ...
  path = [ pkgs.nix ];
  serviceConfig = {
     ExecStart = "nix run git+https://mygitrepo/server";
   }
}

or:

systemd.services.myservice = {
  enable = true;
  ...
  serviceConfig = {
     ExecStart = "${pkgs.nix}/bin/nix run git+https://mygitrepo/server";
   }
}

This is true for any package used in systemd units, as they do not inherit the system $PATH.

2 Likes