From 44c7414cc634635bfeee3527fc8cc4ecf52c50c9 Mon Sep 17 00:00:00 2001 From: schnusch Date: Fri, 22 Aug 2025 12:44:04 +0200 Subject: [PATCH] nixos/systemd-user: add systemd.user.tmpfiles.enable enabled by default, see #391976 --- nixos/modules/system/boot/systemd/user.nix | 14 +++++++++----- nixos/tests/systemd-user-tmpfiles-rules.nix | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/nixos/modules/system/boot/systemd/user.nix b/nixos/modules/system/boot/systemd/user.nix index 63472a66653b..802893cecf13 100644 --- a/nixos/modules/system/boot/systemd/user.nix +++ b/nixos/modules/system/boot/systemd/user.nix @@ -12,9 +12,6 @@ with lib; let cfg = config.systemd.user; - hasTmpfiles = - cfg.tmpfiles.rules != [ ] || any (cfg': cfg'.rules != [ ]) (attrValues cfg.tmpfiles.users); - systemd = config.systemd.package; inherit (systemdUtils.lib) @@ -123,6 +120,13 @@ in }; systemd.user.tmpfiles = { + enable = + (mkEnableOption "systemd user units systemd-tmpfiles-setup.service and systemd-tmpfiles-clean.timer") + // { + default = true; + example = false; + }; + rules = mkOption { type = types.listOf types.str; default = [ ]; @@ -215,7 +219,7 @@ in systemd.user.timers = { # enable systemd user tmpfiles - systemd-tmpfiles-clean.wantedBy = optional hasTmpfiles "timers.target"; + systemd-tmpfiles-clean.wantedBy = optional cfg.tmpfiles.enable "timers.target"; } # Generate timer units for all services that have a ‘startAt’ value. // (mapAttrs (name: service: { @@ -240,7 +244,7 @@ in systemd.services.systemd-user-sessions.restartIfChanged = false; # Restart kills all active sessions. # enable systemd user tmpfiles - systemd.user.services.systemd-tmpfiles-setup.wantedBy = optional hasTmpfiles "basic.target"; + systemd.user.services.systemd-tmpfiles-setup.wantedBy = optional cfg.tmpfiles.enable "basic.target"; # /run/current-system/sw/etc/xdg is in systemd's $XDG_CONFIG_DIRS so we can # write the tmpfiles.d rules for everyone there diff --git a/nixos/tests/systemd-user-tmpfiles-rules.nix b/nixos/tests/systemd-user-tmpfiles-rules.nix index a4a81bb61a85..db621c5e606f 100644 --- a/nixos/tests/systemd-user-tmpfiles-rules.nix +++ b/nixos/tests/systemd-user-tmpfiles-rules.nix @@ -6,9 +6,8 @@ maintainers = [ schnusch ]; }; - nodes.machine = - { ... }: - { + nodes = rec { + machine = { users.users = { alice.isNormalUser = true; bob.isNormalUser = true; @@ -32,6 +31,11 @@ OnUnitActiveSec = "10s"; }; }; + disabled = { + imports = [ machine ]; + systemd.user.tmpfiles.enable = false; + }; + }; testScript = { ... }: @@ -49,5 +53,12 @@ machine.succeed("systemctl --user --machine=bob@ is-active systemd-tmpfiles-clean.timer") machine.succeed("runuser -u bob -- touch ~bob/cleaned_up/file") machine.wait_until_fails("[ -e ~bob/cleaned_up/file ]") + + # disabled user tmpfiles + disabled.succeed("loginctl enable-linger alice bob") + for user in ("alice", "bob"): + for verb in ("is-enabled", "is-active"): + for unit in ("systemd-tmpfiles-setup.service", "systemd-tmpfiles-clean.timer"): + disabled.fail(f"systemctl --user --machine={user}@ {verb} {unit}") ''; }