{ config, tmpLib, pkgs, lib, ... }:
let
inherit (tmpLib) mkTmpFile;
tmpFiles = lib.lists.filter (file: file.enable) config.tmpfiles.files;
in {
options = {
tmpfiles = {
defaultUser = lib.mkOption {
type = lib.types.nullOr lib.types.str;
};
defaultGroup = lib.mkOption {
type = lib.types.nullOr lib.types.str;
};
files = lib.mkOption {
type = lib.types.listOf tmpLib.tmpFileType;
default = [];
description = ''
The files to configure.
'';
};
configurationFileLines = lib.mkOption {
type = lib.types.listOf lib.types.str;
};
configurationPackage = lib.mkOption {
type = lib.types.package;
description = ''
This package contains the tmpfiles configuration package
'';
};
activationPackage = lib.mkOption {
type = lib.types.package;
description = ''
This package contains a script for activation of the tmp files using `systemd-tmpfiles`
'';
};
};
};
config = {
# assertions = builtins.map
# (file: {
# assertion = file.source == null || file.text == null;
# message = "Either text or source can be set, not both.";
# })
# tmpFiles;
#
tmpfiles.configurationFileLines = builtins.map (file: (tmpLib.mkTmpFile ({
type = file.type;
target = file.target;
mode = file.mode;
user = if file.user == null then config.tmpfiles.defaultUser else file.user;
group = if file.group == null then config.tmpfiles.defaultGroup else file.group;
contents = if file.source != null then file.source else file.text;
}))) tmpFiles;
tmpfiles.configurationPackage = pkgs.writeTextFile {
name = "100-tmpactivator.conf";
destination = "/lib/tmpfiles.d/100-tmpactivator.conf";
text = lib.concatStringsSep "\n" config.tmpfiles.configurationFileLines;
};
tmpfiles.activationPackage = pkgs.symlinkJoin {
name = "tmpfiles-activation";
paths = [
(pkgs.writeShellScriptBin "activate" ''
systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/lib/tmpfiles.d/100-tmpactivator.conf"
'')
(pkgs.writeShellScriptBin "deactivate" ''
systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/lib/tmpfiles.d/100-tmpactivator.conf"
'')
config.tmpfiles.configurationPackage
];
};
};
}