formats.ini: Introduce `listToValue` argument (#121613)

Allows coercing lists to values. E.g.

formats.ini { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); }

authored by

Silvan Mosberger and committed by
GitHub
b8336c2b 3cc34f9e

+51 -3
+11 -1
nixos/doc/manual/development/settings-options.xml
··· 50 50 </varlistentry> 51 51 <varlistentry> 52 52 <term> 53 - <varname>pkgs.formats.ini</varname> { <replaceable>listsAsDuplicateKeys</replaceable> ? false, ... } 53 + <varname>pkgs.formats.ini</varname> { <replaceable>listsAsDuplicateKeys</replaceable> ? false, <replaceable>listToValue</replaceable> ? null, ... } 54 54 </term> 55 55 <listitem> 56 56 <para> ··· 63 63 <listitem> 64 64 <para> 65 65 A boolean for controlling whether list values can be used to represent duplicate INI keys 66 + </para> 67 + </listitem> 68 + </varlistentry> 69 + <varlistentry> 70 + <term> 71 + <varname>listToValue</varname> 72 + </term> 73 + <listitem> 74 + <para> 75 + A function for turning a list of values into a single value. 66 76 </para> 67 77 </listitem> 68 78 </varlistentry>
+24 -2
pkgs/pkgs-lib/formats.nix
··· 56 56 }; 57 57 }; 58 58 59 - ini = { listsAsDuplicateKeys ? false, ... }@args: { 59 + ini = { 60 + # Represents lists as duplicate keys 61 + listsAsDuplicateKeys ? false, 62 + # Alternative to listsAsDuplicateKeys, converts list to non-list 63 + # listToValue :: [IniAtom] -> IniAtom 64 + listToValue ? null, 65 + ... 66 + }@args: 67 + assert !listsAsDuplicateKeys || listToValue == null; 68 + { 60 69 61 70 type = with lib.types; let 62 71 ··· 74 83 coercedTo singleIniAtom lib.singleton (listOf singleIniAtom) // { 75 84 description = singleIniAtom.description + " or a list of them for duplicate keys"; 76 85 } 86 + else if listToValue != null then 87 + coercedTo singleIniAtom lib.singleton (nonEmptyListOf singleIniAtom) // { 88 + description = singleIniAtom.description + " or a non-empty list of them"; 89 + } 77 90 else 78 91 singleIniAtom; 79 92 80 93 in attrsOf (attrsOf iniAtom); 81 94 82 - generate = name: value: pkgs.writeText name (lib.generators.toINI args value); 95 + generate = name: value: 96 + let 97 + transformedValue = 98 + if listToValue != null 99 + then 100 + lib.mapAttrs (section: lib.mapAttrs (key: val: 101 + if lib.isList val then listToValue val else val 102 + )) value 103 + else value; 104 + in pkgs.writeText name (lib.generators.toINI (removeAttrs args ["listToValue"]) transformedValue); 83 105 84 106 }; 85 107
+16
pkgs/pkgs-lib/tests/formats.nix
··· 124 124 ''; 125 125 }; 126 126 127 + testIniListToValue = { 128 + drv = evalFormat formats.ini { listToValue = concatMapStringsSep ", " (generators.mkValueStringDefault {}); } { 129 + foo = { 130 + bar = [ null true "test" 1.2 10 ]; 131 + baz = false; 132 + qux = "qux"; 133 + }; 134 + }; 135 + expected = '' 136 + [foo] 137 + bar=null, true, test, 1.200000, 10 138 + baz=false 139 + qux=qux 140 + ''; 141 + }; 142 + 127 143 testTomlAtoms = { 128 144 drv = evalFormat formats.toml {} { 129 145 false = false;