Cannot run Home-manager rebuild from a systemd service

I’m trying to keep my server synced to my work laptop

They share a flake so its really easy I manually upgrade and push the lock file on my laptop and the server just needs to pull and rebuild and it will follow the lock file

I don’t want to have to manually rebuild the server all the time so I’m trying to put it on a systemd timer here is my current module config

I tried a lot of stuff before that but home manager never seems to be able to be ran from systemd

always getting error 127

mai 28 16:29:53 fujiserver systemd[602394]: Starting Rebuild Home Manager configuration from flake...
mai 28 16:29:53 fujiserver systemd[602394]: home-manager-rebuild.service: Main process exited, code=exited, status=127/n/a
mai 28 16:29:53 fujiserver systemd[602394]: home-manager-rebuild.service: Failed with result 'exit-code'.
mai 28 16:29:53 fujiserver systemd[602394]: Failed to start Rebuild Home Manager configuration from flake.

as you can see my home manager is written as ${pkgs.home-manager}/bin/home-manager so the service sees it as an absolute path to the nix store therefore I dont understand how it can get an executable not found error

Pretty sure your NIX_CONFIG value should be quoted

Did it and I still have the same issue

Thanks tho

Is this some variation of bug: home-manager-auto-upgrade.service is failing · Issue #3127 · nix-community/home-manager · GitHub? The environment used by systemd and the services it starts is not always the same as what you see in a shell. In particular I’d suspect PATH and NIX_PATH …

this looks like a promising lead however adding the same option

systemd.user.services.home-manager-auto-upgrade.Service.Environment = [
    "PATH=${lib.makeSearchPath "bin" [ pkgs.nix ]}"
  ];

but that doesnt seem to fix it and im not sure what the proper way of importing the nix env would be

Heh I just posted Store size doubled after updating to 25.05 - #10 by ookhoi

I’m calling home-manager switch in a script that first removes the oldest generation at greater-than-or-equal-to 90% disk-usage. The scripts runs as a user service:

  systemd.user = {
    services = {
      home-manager-switch = {
        Unit = {
          Description = "home-manager switch";
        };
        Service = {
          ExecStart = "/bin/sh -xc \"/home/$USER/.config/home-manager/home-manager-switch.sh\"";
        };
      };
    };
    timers = {
      home-manager-switch = {
        Unit = {
          Description = "home-manager switch";
        };
        Timer = {
          OnCalendar = "*:0/30";
          Persistent = "true";
          RandomizedDelaySec = "900";
        };
        Install = {
          WantedBy = [
            "default.target"
            "timers.target"
          ];
        };
      };
    };
  };

/.config/home-manager/home-manager-switch.sh:

#!/run/current-system/sw/bin/bash

set -auEeo pipefail

PATH="$PATH:/run/current-system/sw/bin:/home/$USER/.nix-profile/bin"

if test -n "$(df -h ~/. | grep -E '9[0-9]%|100%')"
then
    df -h ~/.
    home-manager remove-generations "$(home-manager generations | tail -1 | sed 's/^.* id //; s/ -> .*$//')"
    nix-collect-garbage
    nix-store --gc
    nix-store --optimise
fi

home-manager switch

Exactly my situation. I sync both flake.lock and all *.nix configuration between several laptops, servers and workstations, including the laptop of my daughter.

I have tried changing my approach to yours (with your way of setting path) but I still get the same error

if it works for you then I must be doing something wrong heres my second attempt

Could you try to put your flake in ~/.config/home-manager/ and have just home-manager switch in ExecStart, to see if that works?

Nope I still get status 203 failed to locate executable

None of these will work because home-manager isn’t on the PATH for the systemd service. Use the full path to the hm binary, for example: ${lib.getExe pkgs.home-manager}

yes but I have a line that imports my user PATH in the service

if I have only ${lib.getExe pkgs.home-manager} in the exec start I get the help message in the logs as expected but adding switch after and im back at exit code 127

I think there is some step in the home manager switch operation that expects something to be in path that isnt in my service

thus failing with executable not found