From 34acef2dbfa2894377a981ebbe211a89455c5c1d Mon Sep 17 00:00:00 2001 From: Rutherther Date: Wed, 17 Apr 2024 21:52:40 +0200 Subject: [PATCH] feat: add multiple instances of tmpfiles --- modules/home.nix | 4 +- modules/tmpfiles.nix | 217 +++++++++++++++++++++++-------------------- 2 files changed, 118 insertions(+), 103 deletions(-) diff --git a/modules/home.nix b/modules/home.nix index 50276c7..a9f769d 100644 --- a/modules/home.nix +++ b/modules/home.nix @@ -46,7 +46,7 @@ in { }; config = lib.mkIf (config.home.user != null) { - tmpfiles.selfManagement.targetDir = "${config.home.homeDirectory}/.local/share/user-tmpfiles.d"; + # tmpfiles.selfManagement.targetDir = "${config.home.homeDirectory}/.local/share/user-tmpfiles.d"; tmpfiles.defaultUser = config.home.user; tmpfiles.defaultGroup = config.home.group; @@ -68,7 +68,7 @@ in { }) config.home.packages); - tmpfiles.files = (lib.attrValues (lib.mapAttrs (name: conf: { + tmpfiles.instances.home.files = (lib.attrValues (lib.mapAttrs (name: conf: { type = "L+"; mode = "-"; user = "-"; diff --git a/modules/tmpfiles.nix b/modules/tmpfiles.nix index 1ef996b..a1421c1 100644 --- a/modules/tmpfiles.nix +++ b/modules/tmpfiles.nix @@ -3,23 +3,28 @@ let inherit (tmpLib) mkTmpFile; - tmpFiles = lib.lists.filter (file: file.enable) config.tmpfiles.files; + 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; - }; - - configurationPackage = lib.mkOption { - type = lib.types.package; - description = '' - This package contains the tmpfiles configuration package - ''; - }; - }; + # 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; @@ -28,80 +33,105 @@ in { 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` - ''; + 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 - ]; - }; - }) - - (lib.mkIf (!config.tmpfiles.selfManagement.enable) { - tmpfiles.activationPackage = pkgs.symlinkJoin { - name = "tmpfiles-activation"; - - paths = [ - (pkgs.writeShellScriptBin "activate" '' - 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" - '') - config.tmpfiles.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; @@ -109,21 +139,6 @@ in { # }) # 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-files.conf"; - destination = "/share/user-tmpfiles.d/100-tmpactivator-files.conf"; - text = lib.concatStringsSep "\n" config.tmpfiles.configurationFileLines; - }; - } - ]; + # } + # ]; } -- 2.48.1