Commit 5241898a authored by schnusch's avatar schnusch
Browse files

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.
parent 17ef695a
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -12,6 +12,9 @@ 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)
@@ -210,11 +213,15 @@ in
      // 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.
@@ -233,9 +240,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 (
      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 −0
Original line number Diff line number Diff line
@@ -21,6 +21,15 @@
        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";
      };
    };

@@ -36,5 +45,9 @@
      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 ]")
    '';
}