{ config, tmpLib, pkgs, lib, ... }:
let
inherit (tmpLib) mkTmpFile;
upperConfig = config;
enabledInstances = lib.lists.filter (instance: config.tmpfiles.instances.files != []) config.tmpfiles.instances;
in {
options = {
tmpfiles = {
# selfManagement = {
# enable = lib.mkEnableOption "tmpfiles self management";
# targetDir = lib.mkOption {
# type = lib.types.str;
# };
# managedInstances = lib.mkOption {
# type = lib.types.listOf lib.types.str;
# };
# configurationPackage = lib.mkOption {
# type = lib.types.package;
# description = ''
# This package contains the tmpfiles configuration package
# '';
# };
# };
defaultUser = lib.mkOption {
type = lib.types.nullOr lib.types.str;
};
defaultGroup = lib.mkOption {
type = lib.types.nullOr lib.types.str;
};
instances = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule( { name, config, ... }: {
options = {
files = lib.mkOption {
type = lib.types.listOf tmpLib.tmpFileType;
};
priority = lib.mkOption {
type = lib.types.str;
default = "100";
};
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 = let
tmpFiles = lib.lists.filter (file: file.enable) config.files;
tmpFilesPath = "/share/user-tmpfiles.d/${config.priority}-tmpactivator-${name}.conf";
in {
configurationFileLines = builtins.map (file: (tmpLib.mkTmpFile ({
type = file.type;
target = file.target;
mode = file.mode;
user = if file.user == null then upperConfig.tmpfiles.defaultUser else file.user;
group = if file.group == null then upperConfig.tmpfiles.defaultGroup else file.group;
contents = if file.source != null then file.source else file.text;
}))) tmpFiles;
configurationPackage = pkgs.writeTextFile {
name = "${builtins.baseNameOf tmpFilesPath}";
destination = "${tmpFilesPath}";
text = lib.concatStringsSep "\n" config.configurationFileLines;
};
activationPackage = pkgs.symlinkJoin {
name = "tmpfiles-activation";
paths = [
(pkgs.writeShellScriptBin "activate" ''
systemd-tmpfiles --create "${config.configurationPackage}/${tmpFilesPath}"
'')
(pkgs.writeShellScriptBin "deactivate" ''
systemd-tmpfiles --remove "${config.configurationPackage}/${tmpFilesPath}"
'')
config.configurationPackage
];
};
};
}));
};
};
};
# config = lib.mkMerge [
# (lib.mkIf config.tmpfiles.selfManagement.enable {
# tmpfiles.selfManagement.configurationPackage = pkgs.writeTextFile {
# name = "99-tmpactivator.conf";
# destination = "/share/user-tmpfiles.d/99-tmpactivator.conf";
# text = ''
# L+ ${config.tmpfiles.selfManagement.targetDir}/100-tmpactivator-files.conf - - - - ${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf
# r ${config.tmpfiles.selfManagement.targetDir}/100-tmpactivator-files.conf - - - - -
# '';
# };
# tmpfiles.activationPackage = pkgs.symlinkJoin {
# name = "tmpfiles-activation";
# paths = [
# (pkgs.writeShellScriptBin "activate" ''
# systemd-tmpfiles --create "${config.tmpfiles.selfManagement.configurationPackage}/share/user-tmpfiles.d/99-tmpactivator.conf"
# systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
# '')
# (pkgs.writeShellScriptBin "deactivate" ''
# systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
# systemd-tmpfiles --remove "${config.tmpfiles.selfManagement.configurationPackage}/share/user-tmpfiles.d/99-tmpactivator.conf"
# '')
# config.tmpfiles.configurationPackage
# config.tmpfiles.selfManagement.configurationPackage
# ];
# };
# })
# {
# assertions = builtins.map
# (file: {
# assertion = file.source == null || file.text == null;
# message = "Either text or source can be set, not both.";
# })
# tmpFiles;
#
# }
# ];
}