nixos/systemd-user: enable systemd-tmpfiles-clean.timer

Set systemd.user.timers.systemd-tmpfiles-clean.wantedBy when any user tmpfiles
rules are set so NixOS knows to enable the unit.

schnusch 5241898a 17ef695a

Changed files
+23 -5
nixos
modules
system
boot
systemd
tests
+10 -5
nixos/modules/system/boot/systemd/user.nix
···
let
cfg = config.systemd.user;
+
hasTmpfiles =
+
cfg.tmpfiles.rules != [ ] || any (cfg': cfg'.rules != [ ]) (attrValues cfg.tmpfiles.users);
+
systemd = config.systemd.package;
inherit (systemdUtils.lib)
···
// mapAttrs' (n: v: nameValuePair "${n}.target" (targetToUnit v)) cfg.targets
// mapAttrs' (n: v: nameValuePair "${n}.timer" (timerToUnit v)) cfg.timers;
+
systemd.user.timers = {
+
# enable systemd user tmpfiles
+
systemd-tmpfiles-clean.wantedBy = optional hasTmpfiles "timers.target";
+
}
# Generate timer units for all services that have a ‘startAt’ value.
-
systemd.user.timers = mapAttrs (name: service: {
+
// (mapAttrs (name: service: {
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = service.startAt;
-
}) (filterAttrs (name: service: service.startAt != [ ]) cfg.services);
+
}) (filterAttrs (name: service: service.startAt != [ ]) cfg.services));
# Provide the systemd-user PAM service, required to run systemd
# user instances.
···
systemd.services.systemd-user-sessions.restartIfChanged = false; # Restart kills all active sessions.
# enable systemd user tmpfiles
-
systemd.user.services.systemd-tmpfiles-setup.wantedBy = optional (
-
cfg.tmpfiles.rules != [ ] || any (cfg': cfg'.rules != [ ]) (attrValues cfg.tmpfiles.users)
-
) "basic.target";
+
systemd.user.services.systemd-tmpfiles-setup.wantedBy = optional hasTmpfiles "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
+13
nixos/tests/systemd-user-tmpfiles-rules.nix
···
users.alice.rules = [
"d %h/only_alice"
];
+
users.bob.rules = [
+
"D %h/cleaned_up - - - 0"
+
];
+
};
+
+
# run every 10 seconds
+
systemd.user.timers.systemd-tmpfiles-clean.timerConfig = {
+
OnStartupSec = "10s";
+
OnUnitActiveSec = "10s";
};
};
···
machine.wait_until_succeeds("systemctl --user --machine=bob@ is-active systemd-tmpfiles-setup.service")
machine.succeed("[ -d ~bob/user_tmpfiles_created ]")
machine.succeed("[ ! -e ~bob/only_alice ]")
+
+
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 ]")
'';
}