{ 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; }; configurationFile = lib.mkOption { type = lib.types.str; }; removalConfigurationFile = lib.mkOption { type = 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.configurationFile = pkgs.lib.concatStringsSep "\n" config.tmpfiles.configurationFileLines; tmpfiles.removalConfigurationFile = lib.concatStrings (builtins.map (file: tmpLib.mkRmTmpFile ({ type = if file.type == "d" then "R" else "r"; target = file.target; }) + "\n") tmpFiles); tmpfiles.configurationPackage = pkgs.runCommand "tmpfiles-configuration" { configuration = config.tmpfiles.configurationFile; removal = config.tmpfiles.removalConfigurationFile; } '' mkdir -p $out/share/tmpfiles $out/lib/tmpfiles.d echo -n "$configuration" > "$out/lib/tmpfiles.d/tmpfiles.conf" echo -n "$removal" > "$out/share/tmpfiles/rm-tmpfiles.conf" ''; tmpfiles.activationPackage = pkgs.symlinkJoin { name = "tmpfiles-activation"; paths = [ (pkgs.writeShellScriptBin "activate" '' systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/lib/tmpfiles.d/tmpfiles.conf" '') (pkgs.writeShellScriptBin "deactivate" '' systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/share/tmpfiles/rm-tmpfiles.conf" '') config.tmpfiles.configurationPackage ]; }; }; }