formats: extract things needed from lib in the let binding

It's more vertical space but it ought to be more performant.
This commit is contained in:
Philip Taron
2025-06-10 16:28:50 -07:00
parent ac79b3eb63
commit f13654fe8a
+67 -36
View File
@@ -1,7 +1,39 @@
{ lib, pkgs }:
let
inherit (lib)
concatStringsSep
escape
flatten
id
isAttrs
isFloat
isInt
isList
isString
mapAttrs
mapAttrsToList
mkOption
optionalAttrs
optionalString
pipe
types
singleton
warn
;
inherit (lib.generators)
mkValueStringDefault
toGitINI
toINI
toINIWithGlobalSection
toKeyValue
toLua
mkLuaInline
;
inherit (lib.types)
attrsOf
atom
bool
coercedTo
either
@@ -15,6 +47,7 @@ let
oneOf
path
str
submodule
;
# Attributes added accidentally in https://github.com/NixOS/nixpkgs/pull/335232 (2024-08-18)
@@ -40,7 +73,7 @@ let
;
};
in
lib.optionalAttrs pkgs.config.allowAliases aliases
optionalAttrs allowAliases aliases
// rec {
/*
@@ -188,7 +221,7 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
}:
let
singleIniAtomOr =
if atomsCoercedToLists then coercedTo singleIniAtom lib.singleton else either singleIniAtom;
if atomsCoercedToLists then coercedTo singleIniAtom singleton else either singleIniAtom;
in
if listsAsDuplicateKeys then
singleIniAtomOr (listOf singleIniAtom)
@@ -212,9 +245,9 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
maybeToList =
listToValue:
if listToValue != null then
lib.mapAttrs (key: val: if lib.isList val then listToValue val else val)
mapAttrs (key: val: if isList val then listToValue val else val)
else
lib.id;
id;
in
{
ini =
@@ -239,14 +272,14 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
in
{
type = lib.types.attrsOf (iniSection atom);
type = attrsOf (iniSection atom);
lib.types.atom = atom;
generate =
name: value:
lib.pipe value [
(lib.mapAttrs (_: maybeToList listToValue))
(lib.generators.toINI (
pipe value [
(mapAttrs (_: maybeToList listToValue))
(toINI (
removeAttrs args [
"listToValue"
"atomsCoercedToLists"
@@ -277,14 +310,14 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
};
in
{
type = lib.types.submodule {
type = submodule {
options = {
sections = lib.mkOption rec {
type = lib.types.attrsOf (iniSection atom);
sections = mkOption rec {
type = attrsOf (iniSection atom);
default = { };
description = type.description;
};
globalSection = lib.mkOption rec {
globalSection = mkOption rec {
type = iniSection atom;
default = { };
description = "global " + type.description;
@@ -300,14 +333,14 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
...
}:
pkgs.writeText name (
lib.generators.toINIWithGlobalSection
toINIWithGlobalSection
(removeAttrs args [
"listToValue"
"atomsCoercedToLists"
])
{
globalSection = maybeToList listToValue globalSection;
sections = lib.mapAttrs (_: maybeToList listToValue) sections;
sections = mapAttrs (_: maybeToList listToValue) sections;
}
);
};
@@ -327,7 +360,7 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
{
type = attrsOf (attrsOf (either atom (attrsOf atom)));
lib.types.atom = atom;
generate = name: value: pkgs.writeText name (lib.generators.toGitINI value);
generate = name: value: pkgs.writeText name (toGitINI value);
};
}
@@ -343,7 +376,7 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
# optional config options.
systemd =
let
mkValueString = lib.generators.mkValueStringDefault { };
mkValueString = mkValueStringDefault { };
mkKeyValue = k: v: if v == null then "# ${k} is unset" else "${k} = ${mkValueString v}";
in
ini {
@@ -379,12 +412,12 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
atom =
if listsAsDuplicateKeys then
coercedTo singleAtom lib.singleton (listOf singleAtom)
coercedTo singleAtom singleton (listOf singleAtom)
// {
description = singleAtom.description + " or a list of them for duplicate keys";
}
else if listToValue != null then
coercedTo singleAtom lib.singleton (nonEmptyListOf singleAtom)
coercedTo singleAtom singleton (nonEmptyListOf singleAtom)
// {
description = singleAtom.description + " or a non-empty list of them";
}
@@ -399,13 +432,11 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
let
transformedValue =
if listToValue != null then
lib.mapAttrs (key: val: if lib.isList val then listToValue val else val) value
mapAttrs (key: val: if isList val then listToValue val else val) value
else
value;
in
pkgs.writeText name (
lib.generators.toKeyValue (removeAttrs args [ "listToValue" ]) transformedValue
);
pkgs.writeText name (toKeyValue (removeAttrs args [ "listToValue" ]) transformedValue);
};
@@ -543,18 +574,18 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
"true"
else if value == false then
"false"
else if lib.isInt value || lib.isFloat value then
else if isInt value || isFloat value then
toString value
else if lib.isString value then
else if isString value then
string value
else if lib.isAttrs value then
else if isAttrs value then
attrs value
else if lib.isList value then
else if isList value then
list value
else
abort "formats.elixirConf: should never happen (value = ${value})";
escapeElixir = lib.escape [
escapeElixir = escape [
"\\"
"#"
"\""
@@ -568,11 +599,11 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
else
let
toKeyword = name: value: "${name}: ${toElixir value}";
keywordList = lib.concatStringsSep ", " (lib.mapAttrsToList toKeyword set);
keywordList = concatStringsSep ", " (mapAttrsToList toKeyword set);
in
"[" + keywordList + "]";
listContent = values: lib.concatStringsSep ", " (map toElixir values);
listContent = values: concatStringsSep ", " (map toElixir values);
list = values: "[" + (listContent values) + "]";
@@ -593,7 +624,7 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
set:
let
toEntry = name: value: "${toElixir name} => ${toElixir value}";
entries = lib.concatStringsSep ", " (lib.mapAttrsToList toEntry set);
entries = concatStringsSep ", " (mapAttrsToList toEntry set);
in
"%{${entries}}";
@@ -605,13 +636,13 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
keyConfig =
rootKey: key: value:
"config ${rootKey}, ${key}, ${toElixir value}";
keyConfigs = rootKey: values: lib.mapAttrsToList (keyConfig rootKey) values;
rootConfigs = lib.flatten (lib.mapAttrsToList keyConfigs values);
keyConfigs = rootKey: values: mapAttrsToList (keyConfig rootKey) values;
rootConfigs = flatten (mapAttrsToList keyConfigs values);
in
''
import Config
${lib.concatStringsSep "\n" rootConfigs}
${concatStringsSep "\n" rootConfigs}
'';
in
{
@@ -715,7 +746,7 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
# Wrap standard types, since anything in the Elixir configuration
# can be raw Elixir
}
// lib.mapAttrs (_name: type: elixirOr type) lib.types;
// mapAttrs (_name: type: elixirOr type) types;
};
generate =
@@ -771,12 +802,12 @@ lib.optionalAttrs pkgs.config.allowAliases aliases
inherit columnWidth;
inherit indentWidth;
indentType = if indentUsingTabs then "Tabs" else "Spaces";
value = lib.generators.toLua { inherit asBindings multiline; } value;
value = toLua { inherit asBindings multiline; } value;
passAsFile = [ "value" ];
preferLocalBuild = true;
}
''
${lib.optionalString (!asBindings) ''
${optionalString (!asBindings) ''
echo -n 'return ' >> $out
''}
cat $valuePath >> $out