Systemd User services broken after upgrade

Hey everyone,

after moving from 20.09 → 21.05 it seems that systemd user units are no longer working correctly for me. I have a service that fetches my mails for me, defined in configuration.nix as follows:

  systemd.user.services.mailfetch = {
    enable = true;
    description = "Automatically fetches new mails.";
    wantedBy = [ "graphical-session.target" ];
    after = [ "graphical-session.target" ];
    serviceConfig = {
      Restart = "always";
      RestartSec = "60";
    };
    path = with pkgs; [ bash notmuch isync ];
    script = ''
      mbsync -a && /home/felix/.config/neomutt/notmuch-hook.sh
    '';
  };

After the upgrade I realised that the service was still using an older isync version because the configuration syntax had changed but the new syntax created errors. Yesterday I pruned all profiles older than 14 days to free up some space and since then all my systemd user services are gone:

felix@entropy ~> systemctl start --user mailfetch.service
Failed to start mailfetch.service: Unit mailfetch.service not found.

I updated my system multiple times since the upgrade, but it appears the service definition was pointing to the pre-upgrade version the whole time, until I deleted it by running the garbage collection.

Has anybody experienced something similar or knows how to fix the issue? Or should that be filed as a bug over on Github?

Does /etc/systemd/user/mailfetch.service exists on your system? Do you have any files in there?
Do you by accident maybe put the definition into some other nested structure so it won’t be defined?
You can also put your system configuration in a repl for inspection: Nix command/repl - NixOS Wiki

The path indeed exists and links to a file that appears to be the correct service description, at least judging from the notmuch version (the discrepancy I mentioned before came from this package).

The repl also lists several user services, among them mailfetch, but none of them are listed in systemctl --user --all.

To narrow down the issue. Does it work if you add the same service via home-manager?

I have not used home-manager so far, but I can install it later. Will let you know if it works that way.

Sorry for getting back to you with this so late but it appears that services created by home-manager work. At least the mbsync service you linked works for me. What could be the reason for nixos-rebuild not creating the services properly?

If it weren’t for other user services provided by nix packages, I’d say I leave it be. But unfortunately, stuff like xdg-portal services are also missing.

Could the breaking of the systemd services be related to me tampering with system.stateVersion? I updated it from “20.09” to “21.05” because I only later learned that updating NixOS only entails changing nix-channels. I then grepped the nixpkgs repository for the state variable and found no evidence of possible conflicts, so I left it be.