NixOS 22.11: Systemd user services don't start automatically (but global ones do)

I’m facing the problem that my systemd user services don’t start automatically, unlike global services. I’m running a fresh NixOS 22.11 installation with GNOME as desktop environment.

Defining a systemd user service requires to use the systemd.user.services.<service> configuration property. Let’s look at this example:

systemd.user.services.tftp-network-boot = {
      enable = true;
      description = "TFTP Network Boot Server";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig.ExecStart = "${pkgs.atftp}/sbin/atftpd --daemon --bind-address ${hostIp} ${tftpRoot}";
      # Ensure that the directory is created, if it doesn't exist.
      preStart = ''
        mkdir -m 0777 -p ${tftpRoot}
      '';
    };

$ systemctl --user status tftp-network-boot shows:

○ tftp-network-boot.service - TFTP Network Boot Server
     Loaded: loaded (/etc/systemd/user/tftp-network-boot.service; enabled; preset: enabled)
     Active: inactive (dead)

This is the case after nixos rebuild switch as well as after a reboot.

If I switch the configuration to systemd.services.tftp-network-boot (removed user.),
$ sudo systemctl status tftp-network-boot shows:

○ tftp-network-boot.service - TFTP Network Boot Server
     Loaded: loaded (/etc/systemd/system/tftp-network-boot.service; enabled; preset: enabled)
     Active: inactive (dead) since Fri 2023-01-20 15:31:25 CET; 37ms ago
   Duration: 1ms
    Process: 37431 ExecStartPre=/nix/store/vrdnkmh0qfygi17yrl31ydxnng4h4vv4-unit-script-tftp-network-boot-pre-start/bin/tftp-network-boot-pre-start (code=exited, status=0/SUCCESS)
    Process: 37433 ExecStart=/nix/store/h4q25fqsc68qas4b1ypkmmymv45h3lyb-atftp-0.7.5/sbin/atftpd --daemon --bind-address 192.168.44.100 /home/myuser/tftpboot (code=exited, status=0/SUCCESS)
   Main PID: 37433 (code=exited, status=0/SUCCESS)
         IP: 0B in, 0B out
        CPU: 4ms

Jan 20 15:31:25 nixos systemd[1]: Starting TFTP Network Boot Server...
Jan 20 15:31:25 nixos systemd[1]: Started TFTP Network Boot Serve

Surprisingly, I can’t find online documentation for starting user services automatically.

1 Like

There is no multi-user.target in the user systemd instance, so setting it to be wantedBy that doesn’t accomplish anything.

Also default.target and graphical-session.target are not working.

btw, systemctl --type=target lists the multi-user.target

Did you mean systemctl --user --type=target? If so, it’s probably because you implicitly created it by having a unit reference it. If not, you were listing the system instance :slight_smile:

$ systemctl --user --type=target
  UNIT                         LOAD   ACTIVE SUB    DESCRIPTION                                                                       
  basic.target                 loaded active active Basic System
  default.target               loaded active active Main User Target
  graphical-session-pre.target loaded active active Session services which should run early before the graphical session is brought up
  graphical-session.target     loaded active active Current graphical user session
  hm-graphical-session.target  loaded active active Home Manager X session
  paths.target                 loaded active active Paths
  sockets.target               loaded active active Sockets
  timers.target                loaded active active Timers
  tray.target                  loaded active active Home Manager System Tray

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
9 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

Did you try a reboot (or at least a relog) again with any of these?

1 Like

Ah, rebooting with default.target indeed does the trick. Thanks a lot! I didn’t know that I have to look at systemctl --user --type=target (with --user) to find suitable targets.

2 Likes

Glad I could help.

It seems nixos doesn’t automatically try to start any new or changed user services during activation, like it does for system services. Home-manager will start its own user services, but that leaves nixos’ user services without a helping hand from anywhere :-/

4 Likes

hi tejing NixOS wizard

What exactly do you mean by this? Do you mean that if it exists in Home-Manager nobody will fix this issue in NixOS?

I wasn’t being that indirect. I just meant that nothing currently starts them.