From abe66194b93701323ea321a618c969c9050046dc Mon Sep 17 00:00:00 2001 From: Jacob Birkett Date: Thu, 22 May 2025 19:13:03 -0500 Subject: [PATCH] lib/types: add sourceFileOrLines Signed-off-by: Austin Horstman --- modules/lib/types.nix | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/modules/lib/types.nix b/modules/lib/types.nix index 4c88415e0..523331729 100644 --- a/modules/lib/types.nix +++ b/modules/lib/types.nix @@ -169,4 +169,72 @@ rec { ); in valueType; + + sourceFile = + targetDir: fileName: + let + targetFile = "${targetDir}/${fileName}"; + in + types.submodule ( + { config, ... }: + { + options = { + target = mkOption { + type = types.singleLineStr; + internal = true; + readOnly = true; + }; + source = mkOption { + type = types.nullOr types.path; + default = null; + description = '' + The path to be linked to `${targetDir}` if {option}`source` is a directory, + or to `${targetFile}` if it is a file. + ''; + }; + text = mkOption { + type = types.lines; + default = ""; + description = '' + Text to be included in `${targetFile}`. + ''; + }; + recursive = lib.mkEnableOption '' + Whether to recursively link files from {option}`source` (if it is a directory) in `${targetDir}`. + ''; + }; + config = { + target = + if config.source != null && lib.pathIsDirectory config.source then targetDir else targetFile; + }; + } + ); + + sourceFileOrLines = + targetDir: fileName: + let + fileType = sourceFile targetDir fileName; + union = types.either types.lines fileType; + in + union + // { + merge = + loc: defs: + fileType.merge loc ( + map ( + def: + if types.lines.check def.value then + { + inherit (def) file; + value = { + text = def.value; + source = null; + recursive = false; + }; + } + else + def + ) defs + ); + }; }