Systemd user units are not started

So after I realized that you can just create systemd user units with the nixos configuration while also getting the paths right automatically, I wanted to create a simple service that handles mail fetching for me:

  # periodic automated mail fetching
  systemd.user.services.mailfetch = {
    enable = true;
    description = "Automatically fetches for new mail when the network is up";
    after = [ "network-online.target" ];
    wantedBy = [ "network-online.target" ];
    serviceConfig = {
      Restart = "always";
      RestartSec = "60";
    };
    path = with pkgs; [ bash notmuch isync ];
    script = ''
      mbsync -a && /home/felix/.config/neomutt/notmuch-hook.sh
    '';
  };

But the issue is: Even though the network-online.target is reached, the service is never started:

● network-online.target - Network is Online
     Loaded: loaded (/nix/store/zpzn7c5g58srji21flwqmxzbnaa8w29j-systemd-246.6/example/systemd/system/network-online.target; enabled; vendor preset: enabled)
    Drop-In: /nix/store/31q27y40ihb95g3gyl9ywvplsqm4vvyy-system-units/network-online.target.d
             └─overrides.conf
     Active: active since Thu 2021-05-27 08:07:46 CEST; 36min ago
       Docs: man:systemd.special(7)
             https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget

May 27 08:07:46 entropy systemd[1]: Reached target Network is Online.

Is there anything I’m doing wrong? How can I get the service to fire up automatically (without having to put a systemd user unit into my ~/.config/systemd/user folder manually)?

wantedBy = [ "network-online.target" ];

A user service cannot depend on a system service. Has nothing to with NixOS.

3 Likes

Ugh, makes total sense. I guess my lacking systemd knowledge is to blame here. Thanks for taking the time to answer!