pkgs-lib/formats: Use .attrs.json where possible

This expands on https://github.com/NixOS/nixpkgs/pull/498928 that
introduced __structuredAttrs here by actually using data in
`.attrs.json` when it makes sense, instead of relying on environment
variables. This leads to less temporary files, faster execution and
nicer code.
This commit is contained in:
Yuriy Taraday
2026-05-26 12:25:47 +02:00
parent bf343540cd
commit 691dc02df0
+16 -30
View File
@@ -142,14 +142,12 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ jq ];
value = builtins.toJSON value;
inherit value;
preferLocalBuild = true;
__structuredAttrs = true;
}
''
valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
jq . "$valuePath" > $out
jq .value "$NIX_ATTRS_JSON_FILE" > $out
''
) { };
@@ -167,14 +165,12 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ remarshal_0_17 ];
value = builtins.toJSON value;
inherit value;
preferLocalBuild = true;
__structuredAttrs = true;
}
''
valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
json2yaml "$valuePath" "$out"
json2yaml --unwrap value "$NIX_ATTRS_JSON_FILE" "$out"
''
) { };
@@ -192,14 +188,12 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ remarshal ];
value = builtins.toJSON value;
inherit value;
preferLocalBuild = true;
__structuredAttrs = true;
}
''
valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
json2yaml "$valuePath" "$out"
json2yaml --unwrap value "$NIX_ATTRS_JSON_FILE" "$out"
''
) { };
@@ -940,8 +934,8 @@ optionalAttrs allowAliases aliases
python3
black
];
imports = builtins.toJSON (value._imports or [ ]);
value = builtins.toJSON (removeAttrs value [ "_imports" ]);
imports = value._imports or [ ];
value = removeAttrs value [ "_imports" ];
pythonGen = pkgs.writeText "pythonGen" ''
import json
import os
@@ -964,26 +958,20 @@ optionalAttrs allowAliases aliases
else:
return repr(value)
with open(os.environ["importsPath"], "r") as f:
imports = json.load(f)
if imports is not None:
for i in imports:
with open(os.environ["NIX_ATTRS_JSON_FILE"], "r") as f:
attrs = json.load(f)
if attrs["imports"] is not None:
for i in attrs["imports"]:
print(f"import {i}")
print()
with open(os.environ["valuePath"], "r") as f:
for key, value in json.load(f).items():
for key, value in attrs["value"].items():
print(f"{key} = {recursive_repr(value)}")
'';
preferLocalBuild = true;
__structuredAttrs = true;
}
''
export importsPath="$TMPDIR/imports"
printf "%s" "$imports" > "$importsPath"
export valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
cat "$valuePath"
python3 "$pythonGen" > $out
black $out
''
@@ -1013,14 +1001,14 @@ optionalAttrs allowAliases aliases
python3Packages.xmltodict
libxml2Python
];
value = builtins.toJSON value;
inherit value;
pythonGen = pkgs.writeText "pythonGen" ''
import json
import os
import xmltodict
with open(os.environ["valuePath"], "r") as f:
print(xmltodict.unparse(json.load(f), full_document=${
with open(os.environ["NIX_ATTRS_JSON_FILE"], "r") as f:
print(xmltodict.unparse(json.load(f)["value"], full_document=${
if withHeader then "True" else "False"
}, pretty=True, indent=" " * 2))
'';
@@ -1028,8 +1016,6 @@ optionalAttrs allowAliases aliases
__structuredAttrs = true;
}
''
export valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
python3 "$pythonGen" > $out
xmllint $out > /dev/null
''