From 7ff4ad5c49e7933841cb358091882e50cd215d1a Mon Sep 17 00:00:00 2001 From: cinereal Date: Sat, 3 May 2025 11:39:24 +0200 Subject: [PATCH] wip: templates --- backends/on-machine.nix | 9 +++++++++ options.nix | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/backends/on-machine.nix b/backends/on-machine.nix index df072b0..bddc83e 100644 --- a/backends/on-machine.nix +++ b/backends/on-machine.nix @@ -60,6 +60,15 @@ let echo "All secrets for ${gen.name} are present" elif [ $all_files_missing = true ] ; then + # templates + templates=$(mktemp -d) + trap 'rm -rf $templates' EXIT + export templates + mkdir -p "$templates" + ${lib.concatMapStringsSep "\n" (file: '' + cp "${file.template}" "$templates/${file.name}" + '') (lib.filter (file: file.template != null) (lib.attrValues gen.files))} + # prompts prompts=$(mktemp -d) trap 'rm -rf $prompts' EXIT diff --git a/options.nix b/options.nix index 377b2d4..2d668ff 100644 --- a/options.nix +++ b/options.nix @@ -119,6 +119,21 @@ type = lib.types.nullOr lib.types.str; default = null; }; + placeholder = lib.mkOption { + description = '' + A placeholder that may be used to safely reference the variable in a template. + ''; + readOnly = true; + default = ""; + }; + template = lib.mkOption { + description = '' + A template file with placeholders from the generator's dependencies. + These will be substituted. + ''; + type = lib.types.nullOr lib.types.path; + default = null; + }; }; }) ); @@ -185,6 +200,8 @@ - $in: The directory containing the output values of all declared dependencies - $out: The output directory to put the generated files - $prompts: The directory containing the prompted values as files + - $templates: The template files, with placeholders - to be replaced in the script like + cat $templates/foo | sed "s/''${config.vars.generators.bar.files.baz.placeholder}/$(cat "$in/bar/baz")/g" > "$out/foo" The script should produce the files specified in the 'files' attribute under $out. ''; type = lib.types.either lib.types.str lib.types.path;