diff --git a/nixos/modules/system/boot/stage-2.nix b/nixos/modules/system/boot/stage-2.nix
index 2616c8649d55..f2a177fbdbc1 100644
--- a/nixos/modules/system/boot/stage-2.nix
+++ b/nixos/modules/system/boot/stage-2.nix
@@ -79,6 +79,7 @@ in
'';
};
+ # FIXME: should replace this with something that uses systemd-tmpfiles.
cleanTmpDir = mkOption {
type = types.bool;
default = false;
diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix
index 5f07d6482b7f..5d144a3642bc 100644
--- a/nixos/modules/system/boot/systemd.nix
+++ b/nixos/modules/system/boot/systemd.nix
@@ -143,6 +143,7 @@ let
# Temporary file creation / cleanup.
"systemd-tmpfiles-clean.service"
+ "systemd-tmpfiles-clean.timer"
"systemd-tmpfiles-setup.service"
"systemd-tmpfiles-setup-dev.service"
]
@@ -629,6 +630,22 @@ in
'';
};
+ systemd.tmpfiles.rules = mkOption {
+ type = types.listOf types.str;
+ default = [];
+ example = [ "d /tmp 1777 root root 10d" ];
+ description = ''
+ Rules for creating and cleaning up temporary files
+ automatically. See
+ tmpfiles.d5
+ for the exact format. You should not use this option to create
+ files required by systemd services, since there is no
+ guarantee that systemd-tmpfiles runs when
+ the system is reconfigured using
+ nixos-rebuild.
+ '';
+ };
+
};
@@ -747,5 +764,12 @@ in
environment.etc."tmpfiles.d/x11.conf".source = "${systemd}/example/tmpfiles.d/x11.conf";
+ environment.etc."tmpfiles.d/nixos.conf".text =
+ ''
+ # This file is created automatically and should not be modified.
+ # Please change the option ‘systemd.tmpfiles.rules’ instead.
+ ${concatStringsSep "\n" cfg.tmpfiles.rules}
+ '';
+
};
}
diff --git a/nixos/tests/misc.nix b/nixos/tests/misc.nix
index 76298f1abd47..363be2cbb357 100644
--- a/nixos/tests/misc.nix
+++ b/nixos/tests/misc.nix
@@ -8,6 +8,7 @@ import ./make-test.nix {
[ { device = "/root/swapfile"; size = 128; } ];
environment.variables.EDITOR = pkgs.lib.mkOverride 0 "emacs";
services.nixosManual.enable = pkgs.lib.mkOverride 0 true;
+ systemd.tmpfiles.rules = [ "d /tmp 1777 root root 10d" ];
};
testScript =
@@ -69,6 +70,16 @@ import ./make-test.nix {
$machine->waitForUnit('systemd-udev-settle.service');
$machine->succeed('lsmod | grep psmouse');
};
+
+ # Test whether systemd-tmpfiles-clean works.
+ subtest "tmpfiles", sub {
+ $machine->succeed('touch /tmp/foo');
+ $machine->succeed('systemctl start systemd-tmpfiles-clean');
+ $machine->succeed('[ -e /tmp/foo ]');
+ $machine->succeed('date -s "@$(($(date +%s) + 1000000))"'); # move into the future
+ $machine->succeed('systemctl start systemd-tmpfiles-clean');
+ $machine->fail('[ -e /tmp/foo ]');
+ };
'';
}