pkgs-lib/formats: add hcl1 format

This commit is contained in:
Arian van Putten
2026-02-27 11:37:45 +01:00
parent 0821e97707
commit 0099ab9117
2 changed files with 89 additions and 0 deletions
+23
View File
@@ -1053,4 +1053,27 @@ optionalAttrs allowAliases aliases
generate = name: value: pkgs.writeText name (lib.generators.toPlist { inherit escape; } value);
};
hcl1 =
args:
let
# Helper function to recursively transform values for HCL1 canonicalization
# Rule: If an attribute value is an attribute set, wrap it in a list
transform =
value:
if isAttrs value && !isDerivation value then
# If it's an attribute set, transform it recursively and wrap in a list
[ (mapAttrs (name: transform) value) ]
else if isList value then
# If it's already a list, transform each element
map transform value
else
value;
jsonFormat = json { };
in
jsonFormat
// {
generate = name: value: jsonFormat.generate name (mapAttrs (_: transform) value);
};
}
+66
View File
@@ -1080,4 +1080,70 @@ runBuildTests {
</dict>
</plist>'';
};
hcl1Atoms = shouldPass {
format = formats.hcl1 { };
input = {
resource = {
aws_instance = {
example = {
ami = "ami-12345";
instance_type = "t2.micro";
};
};
};
variable = {
region = {
default = "us-east-1";
};
};
output = {
ip = {
value = "127.0.0.1";
};
};
primitive = "just a string";
number = 42;
enabled = true;
};
expected = ''
{
"enabled": true,
"number": 42,
"output": [
{
"ip": [
{
"value": "127.0.0.1"
}
]
}
],
"primitive": "just a string",
"resource": [
{
"aws_instance": [
{
"example": [
{
"ami": "ami-12345",
"instance_type": "t2.micro"
}
]
}
]
}
],
"variable": [
{
"region": [
{
"default": "us-east-1"
}
]
}
]
}
'';
};
}