systemd.user.services.wantedBy broken?

I want to user systemd user-services on NixOS. I could manually do this, by creating a service-configuration in ~/.config/systemd/user/ and enabling/starting it with systemctl --user enable / systemctl --user start. This works fine.

But NixOS also has the systemd.user.services options, so I thought I could define systemd user services declaratively. But unfortunately, this does not work, since systemd.user.services.wantedBy seems to be ignored.

Example:

  • in configuration.nix:

    systemd.user.services.log_test = {
          enable = true;
          description = "Just a test.";
          script="/home/user/test.sh";
          serviceConfig = {
                  WorkingDirectory="/home/user/";
                  Restart="always";
                  PrivateTmp="true";
                  NoNewPrivileges="true";
          };
          wantedBy = [ "default.target" ];
    };
    
  • systemctl --user cat log_test results in:

    [Unit]
    AssertPathExists=/home/user/test.sh
    Description=Just a test.
    
    [Service]
    Environment="LOCALE_ARCHIVE=..."
    Environment="PATH=..."
    Environment="TZDIR=..."
    
    ExecStart=/nix/store/x76dwav1l70f9n6b4vip2v4ika0lg6l9-unit-script/bin/log_test-start 
    NoNewPrivileges=true
    PrivateTmp=true
    Restart=always
    WorkingDirectory=/home/user/
    
  • and systemctl --user enable log_test tells me:

    The unit files have no installation config (WantedBy, RequiredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units).
    This means they are not meant to be enabled using systemctl.
    Possible reasons for having this kind of units are:
    …

So, is this a bug – or is systemd.user.services meant to be used in a different way?

Maybe it creates a “Wants” section in default.target, rather than adding the “WantedBy” part to your log_test service.
“man configuration.nix” suggests this.

It actually symlinks the unit into default.target.wants/: https://github.com/NixOS/nixpkgs/blob/ac64ce994509aaad8c5b55254595a5f989ba24e9/nixos/modules/system/boot/systemd-lib.nix#L188-L194

1 Like