Add YAML document representation too

Might be something someone somewhere wants
This commit is contained in:
Carl Andersson 2025-10-21 16:15:24 +02:00
commit 50d419507c
2 changed files with 34 additions and 2 deletions

View file

@ -37,6 +37,8 @@ in
manifestJSONFile
manifestYAML
manifestYAMLFile
manifestYAMLList
manifestYAMLFileList
;
deploymentScript = eval.config.kluctl.script;
validationScript = eval.config.validation.script;

View file

@ -9,6 +9,34 @@
type = lib.types.anything;
};
config.internal = rec {
# Makes a valid YAML string, supports multiple documents or single attrsets,
# documents will be JSON formatted since Nix can't render YAML.
toYAMLStr =
input:
if builtins.typeOf input == "list" then
lib.concatStringsSep "\n---\n" (map (doc: builtins.toJSON doc) input)
else if builtins.typeOf input == "set" then
builtins.toJSON input
else
throw "toYAML only supports set and list types";
# Makes a valid YAML string, supports multiple documents or single attrsets,
# reformatted using "yq-go".
toYAMLFile =
filename: input:
pkgs.runCommand filename
{
nativeBuildInputs = [
pkgs.yq-go
];
yamlContent = toYAMLStr input;
passAsFile = [ "yamlContent" ];
}
#bash
''
yq --prettyPrint < $yamlContentPath > $out
'';
manifestAttrs = {
apiVersion = "v1";
kind = "List";
@ -17,11 +45,13 @@
manifestJSON = builtins.toJSON manifestAttrs;
manifestJSONFile = pkgs.writeText "manifest.json" manifestJSON;
# Beware that YAML rendering requires IFD
manifestYAML = builtins.readFile manifestYAMLFile;
manifestYAMLFile =
manifestYAMLList = builtins.readFile manifestYAMLFile;
manifestYAMLFileList =
pkgs.runCommandNoCCLocal "manifest.yaml" { } # bash
''
${lib.getExe pkgs.yq} --yaml-output '.' ${manifestJSONFile} > $out
'';
manifestYAML = builtins.readFile manifestYAMLFile;
manifestYAMLFile = toYAMLFile "manifest.yaml" config.kubernetes.generated;
};
}