diff --git a/modules/systemd.nix b/modules/systemd.nix index 5a9c7508..cd5b30ed 100644 --- a/modules/systemd.nix +++ b/modules/systemd.nix @@ -258,7 +258,8 @@ in { # If we run under a Linux system we assume that systemd is # available, in particular we assume that systemctl is in PATH. - (mkIf pkgs.stdenv.isLinux { + # Do not install any user services if username is root. + (mkIf (pkgs.stdenv.isLinux && config.home.username != "root") { xdg.configFile = mkMerge [ (lib.listToAttrs ((buildServices "service" cfg.services) ++ (buildServices "slices" cfg.slices) diff --git a/tests/modules/systemd/default.nix b/tests/modules/systemd/default.nix index c1779ac5..c949edd9 100644 --- a/tests/modules/systemd/default.nix +++ b/tests/modules/systemd/default.nix @@ -1,5 +1,6 @@ { systemd-services = ./services.nix; + systemd-services-disabled-for-root = ./services-disabled-for-root.nix; systemd-session-variables = ./session-variables.nix; systemd-timers = ./timers.nix; } diff --git a/tests/modules/systemd/services-disabled-for-root.nix b/tests/modules/systemd/services-disabled-for-root.nix new file mode 100644 index 00000000..f54ebd08 --- /dev/null +++ b/tests/modules/systemd/services-disabled-for-root.nix @@ -0,0 +1,23 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + home.username = mkForce "root"; + + systemd.user.services."test-service@" = { + Unit = { Description = "A basic test service"; }; + + Service = { + Environment = [ "VAR1=1" "VAR2=2" ]; + ExecStart = ''/some/exec/start/command --with-arguments "%i"''; + }; + }; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/test-service@.service + assertPathNotExists $serviceFile + ''; + }; +}