nixos/systemd-tmpfiles: add settings option

This commit is contained in:
DavHau 2023-10-21 19:02:32 +01:00 committed by nikstur
parent 2caca43de3
commit cae154a67e
2 changed files with 107 additions and 1 deletions

View file

@ -20,6 +20,102 @@ in
''; '';
}; };
systemd.tmpfiles.settings = mkOption {
description = lib.mdDoc ''
Declare systemd-tmpfiles rules to create, delete, and clean up volatile
and temporary files and directories.
Even though the service is called `*tmp*files` you can also create
persistent files.
'';
example = {
"10-mypackage" = {
"/var/lib/my-service/statefolder".d = {
mode = "0755";
user = "root";
group = "root";
};
};
};
default = {};
type = types.attrsOf (types.attrsOf (types.attrsOf (types.submodule ({ name, config, ... }: {
options.type = mkOption {
type = types.str;
default = name;
example = "d";
description = lib.mdDoc ''
The type of operation to perform on the file.
The type consists of a single letter and optionally one or more
modifier characters.
Please see the upstream documentation for the available types and
more details:
<https://www.freedesktop.org/software/systemd/man/tmpfiles.d>
'';
};
options.mode = mkOption {
type = types.str;
default = "-";
example = "0755";
description = lib.mdDoc ''
The file access mode to use when creating this file or directory.
'';
};
options.user = mkOption {
type = types.str;
default = "-";
example = "root";
description = lib.mdDoc ''
The user of the file.
This may either be a numeric ID or a user/group name.
If omitted or when set to `"-"`, the user and group of the user who
invokes systemd-tmpfiles is used.
'';
};
options.group = mkOption {
type = types.str;
default = "-";
example = "root";
description = lib.mdDoc ''
The group of the file.
This may either be a numeric ID or a user/group name.
If omitted or when set to `"-"`, the user and group of the user who
invokes systemd-tmpfiles is used.
'';
};
options.age = mkOption {
type = types.str;
default = "-";
example = "10d";
description = lib.mdDoc ''
Delete a file when it reaches a certain age.
If a file or directory is older than the current time minus the age
field, it is deleted.
If set to `"-"` no automatic clean-up is done.
'';
};
options.argument = mkOption {
type = types.str;
default = "";
example = "";
description = lib.mdDoc ''
An argument whose meaning depends on the type of operation.
Please see the upstream documentation for the meaning of this
parameter in different situations:
<https://www.freedesktop.org/software/systemd/man/tmpfiles.d>
'';
};
}))));
};
systemd.tmpfiles.packages = mkOption { systemd.tmpfiles.packages = mkOption {
type = types.listOf types.package; type = types.listOf types.package;
default = []; default = [];
@ -100,7 +196,13 @@ in
${concatStringsSep "\n" cfg.rules} ${concatStringsSep "\n" cfg.rules}
''; '';
}) })
]; ] ++ (mapAttrsToList (name: paths:
pkgs.writeTextDir "lib/tmpfiles.d/${name}.conf" (concatStrings (mapAttrsToList (path: types:
concatStrings (mapAttrsToList (_type: entry: ''
'${entry.type}' '${path}' '${entry.mode}' '${entry.user}' '${entry.group}' '${entry.age}' ${entry.argument}
'') types)
) paths ))
) cfg.settings);
systemd.tmpfiles.rules = [ systemd.tmpfiles.rules = [
"d /nix/var 0755 root root - -" "d /nix/var 0755 root root - -"

View file

@ -13,6 +13,7 @@ in {
environment.variables.EDITOR = lib.mkOverride 0 "emacs"; environment.variables.EDITOR = lib.mkOverride 0 "emacs";
documentation.nixos.enable = lib.mkOverride 0 true; documentation.nixos.enable = lib.mkOverride 0 true;
systemd.tmpfiles.rules = [ "d /tmp 1777 root root 10d" ]; systemd.tmpfiles.rules = [ "d /tmp 1777 root root 10d" ];
systemd.tmpfiles.settings."10-test"."/tmp/somefile".d = {};
virtualisation.fileSystems = { "/tmp2" = virtualisation.fileSystems = { "/tmp2" =
{ fsType = "tmpfs"; { fsType = "tmpfs";
options = [ "mode=1777" "noauto" ]; options = [ "mode=1777" "noauto" ];
@ -117,6 +118,9 @@ in {
) )
machine.fail("[ -e /tmp/foo ]") machine.fail("[ -e /tmp/foo ]")
with subtest("whether systemd-tmpfiles settings works"):
machine.succeed("[ -e /tmp/somefile ]")
with subtest("whether automounting works"): with subtest("whether automounting works"):
machine.fail("grep '/tmp2 tmpfs' /proc/mounts") machine.fail("grep '/tmp2 tmpfs' /proc/mounts")
machine.succeed("touch /tmp2/x") machine.succeed("touch /tmp2/x")