vars: new option prompts.<name>.createFile

This commit is contained in:
DavHau
2024-09-04 15:40:30 +02:00
parent a100d74a8c
commit dc4fcb7329
3 changed files with 72 additions and 3 deletions

View File

@@ -4,13 +4,32 @@
pkgs,
...
}:
let
inherit (lib)
attrNames
concatMapStrings
genAttrs
filterAttrs
makeBinPath
mkOptionDefault
optionalString
;
promptToFile = name: ''
cat "$prompts/${name}" > "$out/${name}"
'';
promptsToFilesScript = concatMapStrings promptToFile;
filePromptNames = attrNames (filterAttrs (_name: prompt: prompt.createFile) config.prompts);
in
{
finalScript = lib.mkOptionDefault ''
finalScript = mkOptionDefault ''
set -eu -o pipefail
export PATH="${lib.makeBinPath config.runtimeInputs}:${pkgs.coreutils}/bin"
export PATH="${makeBinPath config.runtimeInputs}:${pkgs.coreutils}/bin"
${lib.optionalString (pkgs.stdenv.hostPlatform.isLinux) ''
${optionalString (pkgs.stdenv.hostPlatform.isLinux) ''
# prepare sandbox user on platforms where this is supported
mkdir -p /etc
@@ -31,6 +50,9 @@
::1 localhost
EOF
''}
${promptsToFilesScript filePromptNames}
${config.script}
'';
files = genAttrs filePromptNames (_name: { });
}

View File

@@ -120,6 +120,21 @@ in
type = attrsOf (
submodule (prompt: {
options = options {
createFile = {
description = ''
Whether the prompted value should be stored in a file with the same name as the prompt.
If enabled, the behavior is equivalent to the following configuration:
```nix
{
files.<name>.secret = true;
script = "cp $prompts/<name> $out/<name>";
}
```
'';
type = bool;
default = true;
};
description = {
description = ''
The description of the prompted value
@@ -165,6 +180,7 @@ in
The script should produce the files specified in the 'files' attribute under $out.
'';
type = either str path;
default = "";
};
finalScript = {
description = ''