pkgs.formats: performance cleanups (#529122)

This commit is contained in:
isabel
2026-06-10 20:26:41 +00:00
committed by GitHub
+48 -46
View File
@@ -23,6 +23,7 @@ let
pipe
singleton
strings
toJSON
toPretty
types
versionAtLeast
@@ -37,6 +38,7 @@ let
toKeyValue
toLua
mkLuaInline
toPlist
;
inherit (lib.types)
@@ -142,7 +144,7 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ jq ];
value = builtins.toJSON value;
value = toJSON value;
preferLocalBuild = true;
__structuredAttrs = true;
}
@@ -167,7 +169,7 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ remarshal_0_17 ];
value = builtins.toJSON value;
value = toJSON value;
preferLocalBuild = true;
__structuredAttrs = true;
}
@@ -192,7 +194,7 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ remarshal ];
value = builtins.toJSON value;
value = toJSON value;
preferLocalBuild = true;
__structuredAttrs = true;
}
@@ -250,12 +252,23 @@ optionalAttrs allowAliases aliases
description = "section of an INI file (attrs of " + atom.description + ")";
};
maybeToList =
maybeCoerceList =
listToValue:
if listToValue != null then
mapAttrs (key: val: if isList val then listToValue val else val)
else
id;
maybeCoerceAllLists =
listToValue:
if listToValue != null then
mapAttrs (_: mapAttrs (key: val: if isList val then listToValue val else val))
else
id;
ignoredArgs = [
"listToValue"
"atomsCoercedToLists"
];
in
{
ini =
@@ -286,13 +299,8 @@ optionalAttrs allowAliases aliases
generate =
name: value:
pipe value [
(mapAttrs (_: maybeToList listToValue))
(toINI (
removeAttrs args [
"listToValue"
"atomsCoercedToLists"
]
))
(maybeCoerceAllLists listToValue)
(toINI (removeAttrs args ignoredArgs))
(pkgs.writeText name)
];
};
@@ -341,15 +349,10 @@ optionalAttrs allowAliases aliases
...
}:
pkgs.writeText name (
toINIWithGlobalSection
(removeAttrs args [
"listToValue"
"atomsCoercedToLists"
])
{
globalSection = maybeToList listToValue globalSection;
sections = mapAttrs (_: maybeToList listToValue) sections;
}
toINIWithGlobalSection (removeAttrs args ignoredArgs) {
globalSection = maybeCoerceList listToValue globalSection;
sections = maybeCoerceAllLists listToValue sections;
}
);
};
@@ -357,7 +360,7 @@ optionalAttrs allowAliases aliases
{
listsAsDuplicateKeys ? false,
...
}@args:
}:
let
atom = iniAtom {
inherit listsAsDuplicateKeys;
@@ -396,7 +399,7 @@ optionalAttrs allowAliases aliases
// {
generate =
name: value:
lib.warn
warn
"Direct use of `pkgs.formats.systemd` has been deprecated, please use `pkgs.formats.systemd { }` instead."
rawFormat.generate
name
@@ -448,15 +451,15 @@ optionalAttrs allowAliases aliases
attrsOf atom;
generate =
name: value:
let
transformedValue =
transformValue =
if listToValue != null then
mapAttrs (key: val: if isList val then listToValue val else val) value
mapAttrs (key: val: if isList val then listToValue val else val)
else
value;
id;
finalArgs = removeAttrs args [ "listToValue" ];
in
pkgs.writeText name (toKeyValue (removeAttrs args [ "listToValue" ]) transformedValue);
name: value: pkgs.writeText name (toKeyValue finalArgs (transformValue value));
};
@@ -468,7 +471,7 @@ optionalAttrs allowAliases aliases
generate =
name: value:
pkgs.callPackage (
{ runCommand, remarshal }:
{ runCommand }:
runCommand name
{
nativeBuildInputs = [ json2x ];
@@ -496,8 +499,7 @@ optionalAttrs allowAliases aliases
*/
cdn =
{ }:
json { }
// {
{
type = serializableValueWith { typeName = "CDN"; };
generate =
@@ -507,7 +509,7 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ json2cdn ];
value = builtins.toJSON value;
value = toJSON value;
preferLocalBuild = true;
__structuredAttrs = true;
}
@@ -553,9 +555,6 @@ optionalAttrs allowAliases aliases
[Tuple]: <https://hexdocs.pm/elixir/Tuple.html>
*/
elixirConf =
{
elixir ? pkgs.elixir,
}:
let
toElixir =
value:
@@ -622,12 +621,14 @@ optionalAttrs allowAliases aliases
tuple = values: "{${listContent values}}";
toConf =
values:
let
keyConfig =
rootKey: key: value:
"config ${rootKey}, ${key}, ${toElixir value}";
keyConfigs = rootKey: values: mapAttrsToList (keyConfig rootKey) values;
in
values:
let
rootConfigs = flatten (mapAttrsToList keyConfigs values);
in
''
@@ -636,6 +637,9 @@ optionalAttrs allowAliases aliases
${concatStringsSep "\n" rootConfigs}
'';
in
{
elixir ? pkgs.elixir,
}:
{
type =
let
@@ -812,7 +816,7 @@ optionalAttrs allowAliases aliases
''
) { };
# Alias for mkLuaInline
lib.mkRaw = lib.mkLuaInline;
lib.mkRaw = mkLuaInline;
};
nixConf =
@@ -836,14 +840,12 @@ optionalAttrs allowAliases aliases
float
str
path
package
types.package
]);
in
attrsOf atomType;
generate =
name: value:
let
# note that list type has been omitted here as the separator varies, see `nix.settings.*`
mkValueString =
v:
@@ -871,8 +873,8 @@ optionalAttrs allowAliases aliases
mkKeyValuePairs = attrs: concatStringsSep "\n" (mapAttrsToList mkKeyValue attrs);
isExtra = key: hasPrefix "extra-" key;
in
name: value:
pkgs.writeTextFile {
inherit name;
# workaround for https://github.com/NixOS/nix/issues/9487
@@ -885,7 +887,7 @@ optionalAttrs allowAliases aliases
${mkKeyValuePairs (filterAttrs (key: _: isExtra key) value)}
${extraOptions}
'';
checkPhase = lib.optionalString checkConfig (
checkPhase = optionalString checkConfig (
if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then
''
echo "Ignoring validation for cross-compilation"
@@ -940,8 +942,8 @@ optionalAttrs allowAliases aliases
python3
black
];
imports = builtins.toJSON (value._imports or [ ]);
value = builtins.toJSON (removeAttrs value [ "_imports" ]);
imports = toJSON (value._imports or [ ]);
value = toJSON (removeAttrs value [ "_imports" ]);
pythonGen = pkgs.writeText "pythonGen" ''
import json
import os
@@ -1013,7 +1015,7 @@ optionalAttrs allowAliases aliases
python3Packages.xmltodict
libxml2Python
];
value = builtins.toJSON value;
value = toJSON value;
pythonGen = pkgs.writeText "pythonGen" ''
import json
import os
@@ -1061,11 +1063,10 @@ optionalAttrs allowAliases aliases
in
valueType;
generate = name: value: pkgs.writeText name (lib.generators.toPlist { inherit escape; } value);
generate = name: value: pkgs.writeText name (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
@@ -1081,6 +1082,7 @@ optionalAttrs allowAliases aliases
value;
jsonFormat = json { };
in
args:
jsonFormat
// {
generate = name: value: jsonFormat.generate name (mapAttrs (_: transform) value);