Rebuild fails because of systemd.service

Hello,
i am trying to add a custom service to my nixos config:

configuration.nix:

  systemd.services.reset-home = {
    wantedBy = [ "multi-user.target" ];
    before = [ "gdm.service" ];
    description = "redacted";
    serviceConfig = {
      Type = "oneshot";
      ExecStart = "rsync --verbose --delete --chmod=u+rw,go-w --chown=redacted:users --archive /backup/redacted/ /home/redacted/";
    };
  };

The command in ExecStart seems to work when i just call it in the terminal.
I looked through journalctl as root but it just says the service failed to start:

Aug 09 13:22:56 redactedpc systemd[1]: File System Check on Root Device was skipped because of an unmet condition check (ConditionPathIsReadWrite=!/).
Aug 09 13:22:56 redactedpc systemd[1]: Reached target Local File Systems.
Aug 09 13:22:56 redactedpc systemd[1]: Reached target Remote File Systems.
Aug 09 13:22:56 redactedpc systemd[1]: Starting redacted Description of Service
Aug 09 13:22:56 redactedpc systemd[1]: Starting Load Kernel Module efi_pstore...
Aug 09 13:22:56 redactedpc systemd[1]: Starting Create SUID/SGID Wrappers...
Aug 09 13:22:56 redactedpc systemd[1]: Rebuild Journal Catalog was skipped because of an unmet condition check (ConditionNeedsUpdate=/var).
Aug 09 13:22:56 redactedpc systemd[1]: Update is Completed was skipped because no trigger condition checks were met.
Aug 09 13:22:56 redactedpc systemd[1]: reset-home.service: Main process exited, code=exited, status=203/EXEC
Aug 09 13:22:56 redactedpc systemd[1]: reset-home.service: Failed with result 'exit-code'.
Aug 09 13:22:56 redactedpc systemd[1]: Failed to start redacted Description of Service
Aug 09 13:22:56 redactedpc systemd[1]: modprobe@efi_pstore.service: Deactivated successfully.
Aug 09 13:22:56 redactedpc systemd[1]: Finished Load Kernel Module efi_pstore.
Aug 09 13:22:56 redactedpc systemd[1]: Platform Persistent Storage Archival was skipped because of an unmet condition check (ConditionDirectoryNotEmpty=/sys/fs/pstore).
Aug 09 13:22:56 redactedpc systemd[1]: suid-sgid-wrappers.service: Deactivated successfully.
Aug 09 13:22:56 redactedpc systemd[1]: Finished Create SUID/SGID Wrappers.
Aug 09 13:22:57 redactedpc nixos[4848]: switching to system configuration /nix/store/48iag0iimrxbj3plq5lvyc5m9569b6fj-nixos-system-redactedpc-24.05.20240806.21cc704 failed (status 4)
Aug 09 13:22:57 redactedpc systemd[1]: nixos-rebuild-switch-to-configuration.service: Main process exited, code=exited, status=4/NOPERMISSION
Aug 09 13:22:57 redactedpc systemd[1]: nixos-rebuild-switch-to-configuration.service: Failed with result 'exit-code'.
Aug 09 13:22:57 redactedpc sudo[4844]: pam_unix(sudo:session): session closed for user root
Aug 09 13:22:57 redactedpc sudo[4799]: pam_unix(sudo:session): session closed for user root

The rebuild call output:

[myname@Latitude9430:~/projects/redacted]$ ./scripts/rebuild-pool.sh 
[sudo] Passwort für redacted: 
warning: creating lock file '/home/redacted/nixos-config/flake.lock'
building the system configuration...
activating the configuration...
setting up /etc...
reloading user units for root...
reloading user units for redadmin...
reloading user units for reduser...
restarting sysinit-reactivation.target
warning: the following units failed: reset-home.service

× reset-home.service - redacted Description.
     Loaded: loaded (/etc/systemd/system/reset-home.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Fri 2024-08-09 13:22:56 CEST; 147ms ago
    Process: 5059 ExecStart=rsync --verbose --delete --chmod=u+rw,go-w --chown=reduser:users --archive /backup/reduser/ /home/reduser/ (code=exited, status=203/EXEC)
   Main PID: 5059 (code=exited, status=203/EXEC)
         IP: 0B in, 0B out
        CPU: 2ms

Aug 09 13:22:56 redpoolpc systemd[1]: Starting Setzt den Home-Folder des redusers bei jedem Neustart zurück....
Aug 09 13:22:56 redpoolpc systemd[1]: reset-home.service: Main process exited, code=exited, status=203/EXEC
Aug 09 13:22:56 redpoolpc systemd[1]: reset-home.service: Failed with result 'exit-code'.
Aug 09 13:22:56 redpoolpc systemd[1]: Failed to start redacted Description
warning: error(s) occurred while switching to the new configuration
Connection to 10.102.12.135 closed.

I don’t know where to start looking for errors.
Thanks in advance!

try replacing rsync with ${pkgs.rsync}/bin/rsync

1 Like

yes that works, i just came up with that aswell but i don’t know where to find a good resource that explains services in nixos.

I had to add rsync to the system packages aswell which confused me because rsync seemed to be already installed. I would guess it is because the generated service file needs the correct path to the binary inside the store.

Also where can i find the packages so that i can see that the binary is under /bin/. Do all pkgs follow that scheme?

Thanks!

You can run nix-build '<nixpkgs>' -A rsync to see a package (it appears as ./result)
You can also run nix build nixpkgs#rsync if you want to use flakes

1 Like

This behaviour has nothing to do with NixOS, this how systemd works.
They intentionally ignore the environment in systemd services, so you must either set the environment appropriately or pass the full path to binaries.