Custom systemd user service: systemd complains about missing service

So I have this custom systemd service:

{ config, lib, pkgs, ... }:
let
  cfg = config.services.custom.polybar;
  polybar-config = lib.strings.readFile ../../../dots/polybar/config.ini;

in with lib; {
  options.services.custom.polybar = {
    enable = mkEnableOption "Enable polybar service";
    extraConfig = mkOption {
      type = lib.types.lines;
      default = "";
    };
    package = mkOption {
      type = lib.types.package;
      default = pkgs.polybarFull;
    };
    startup-script = mkOption {
      type = lib.types.lines;
      default = ''
        polybar primary -c /etc/polybar/config.ini &
      '';
    };
  };

  config = mkIf cfg.enable {
    environment = {
      systemPackages = [ cfg.package ];
      etc."polybar/config.ini".text = polybar-config;
    };

    services.dbus = { enable = true; };

    systemd.user.services.polybar = {
      description = "Polybar status bar";
      path = [
        pkgs.coreutils pkgs.dbus
        cfg.package
        pkgs.gnugrep
        pkgs.xorg.xrandr
        pkgs.procps
      ];
      unitConfig = {
        PartOf = [ "graphical-session.target" ];
        X-Restart-Triggers = [ "${config.environment.etc."polybar/config.ini".source}" ];
      };
      serviceConfig = {
        Type = "forking";
        ExecStart = let
          script = cfg.startup-script;
          scriptPkg = pkgs.writeShellScriptBin "polybar-start" script;
        in "${scriptPkg}/bin/polybar-start";
        Restart = "on-failure";
      };
      wantedBy = [ "graphical-session.target" ];
    };
  };
}

and the generated systemd service:

➜  ~ cat /etc/systemd/user/polybar.service
[Unit]
Description=Polybar status bar
PartOf=graphical-session.target
X-Restart-Triggers=/nix/store/jk43q8aj32rgibchxhgzdkwjfwbfdcpb-etc-config.ini

[Service]
Environment="LOCALE_ARCHIVE=/nix/store/7g9464a3s07lxpr3w6cgfw938zdfgys9-glibc-locales-2.33-59/lib/locale/locale-archive"
Environment="PATH=/nix/store/176gh50y24c0lx2bnnmsvf9wazb73php-coreutils-9.0/bin:/nix/store/xh4xz5aa4smirb66xi9xjaxjxcbsz5mg-dbus-1.12.20/bin:/nix/store/a0sbmz1nxgya2j2gnwqvzvipxagx9v3b-polybar-3.5.7/bin:/nix/store/pkj79ap8x2dalzl63ndpdmxg2crxpjl8-gnugrep-3.7/bin:/nix/store/1v42phibm02h0pq6dp949p2v5s3cqlmg-xrandr-1.5.1/bin:/nix/store/57jzk5a2w4j4iggg0icihm0i7y03vqjp-procps-3.3.16/bin:/nix/store/176gh50y24c0lx2bnnmsvf9wazb73php-coreutils-9.0/bin:/nix/store/g0hhds9sdiqds0xxgpn7v4pwcv89varr-findutils-4.8.0/bin:/nix/store/pkj79ap8x2dalzl63ndpdmxg2crxpjl8-gnugrep-3.7/bin:/nix/store/4na05j9gmpp3dwhmnc1q0a108ymf2qjy-gnused-4.8/bin:/nix/store/cfhpzlarbhfw3scj91dcz5ai04ayfzik-systemd-249.7/bin:/nix/store/176gh50y24c0lx2bnnmsvf9wazb73php-coreutils-9.0/sbin:/nix/store/xh4xz5aa4smirb66xi9xjaxjxcbsz5mg-dbus-1.12.20/sbin:/nix/store/a0sbmz1nxgya2j2gnwqvzvipxagx9v3b-polybar-3.5.7/sbin:/nix/store/pkj79ap8x2dalzl63ndpdmxg2crxpjl8-gnugrep-3.7/sbin:/nix/store/1v42phibm02h0pq6dp949p2v5s3cqlmg-xrandr-1.5.1/sbin:/nix/store/57jzk5a2w4j4iggg0icihm0i7y03vqjp-procps-3.3.16/sbin:/nix/store/176gh50y24c0lx2bnnmsvf9wazb73php-coreutils-9.0/sbin:/nix/store/g0hhds9sdiqds0xxgpn7v4pwcv89varr-findutils-4.8.0/sbin:/nix/store/pkj79ap8x2dalzl63ndpdmxg2crxpjl8-gnugrep-3.7/sbin:/nix/store/4na05j9gmpp3dwhmnc1q0a108ymf2qjy-gnused-4.8/sbin:/nix/store/cfhpzlarbhfw3scj91dcz5ai04ayfzik-systemd-249.7/sbin"
Environment="TZDIR=/nix/store/bhdq8kpbx4c68mnscbj1gzcdkhjmldwi-tzdata-2021e/share/zoneinfo"


ExecStart=/nix/store/zxxljgv8l4wv91wb5l4xl3rpyf13yvdg-polybar-start/bin/polybar-start
Restart=on-failure
Type=forking

which worked until recently, but now systemd complains that the service is missing when I try to restart it

➜  ~ systemctl --user restart polybar.service
Failed to restart polybar.service: Unit polybar.service not found.

even though the service exists as posted above

➜  ~ l /etc/systemd/user/polybar.service
lrwxrwxrwx 2 root root 80 Jan  1  1970 /etc/systemd/user/polybar.service -> /nix/store/8vcl0c150y6bi9mgbcdykxvi7gaqa827-unit-polybar.service/polybar.service

I’ve tried running systemctl --user daemon-reload as well to no avail.

Edit: Updated issue after feedback

Thanks, I noticed that right after making the post. I’ve edited to reflect my initial issue

IIRC, nixos isn’t as good about automatically doing the daemon-reload for the user service manager. Try systemctl daemon-reload --user maybe?

Yes I have tried that to no avail

What does your ~/.config/systemd/user directory look like?

it contained one file: polybar.service which was a symlink to an empty file. So I deleted it and then systemd --user restart polybar.service started working again

1 Like