summary refs log tree commit diff
path: root/lib/generators.nix
diff options
context:
space:
mode:
authorSilvan Mosberger2020-03-10 02:38:28 +0100
committerSilvan Mosberger2020-03-10 16:01:04 +0100
commit575354babf195cbf55af27dd5e61b8481dfd0558 (patch)
treeb1b4efa1d9c4bbebd899f6a62e7dcc79692f6b74 /lib/generators.nix
parent169fc2eecaf48348d1d2dc81b500fb55a94518e6 (diff)
lib/generators: Add toINI option for duplicate keys
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix18
1 files changed, 12 insertions, 6 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index a64e94bd5cbd..240a19789b54 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -76,10 +76,14 @@ rec {
    * mkKeyValue is the same as in toINI.
    */
   toKeyValue = {
-    mkKeyValue ? mkKeyValueDefault {} "="
-  }: attrs:
-    let mkLine = k: v: mkKeyValue k v + "\n";
-    in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs);
+    mkKeyValue ? mkKeyValueDefault {} "=",
+    listsAsDuplicateKeys ? false
+  }:
+  let mkLine = k: v: mkKeyValue k v + "\n";
+      mkLines = if listsAsDuplicateKeys
+        then k: v: map (mkLine k) (if lib.isList v then v else [v])
+        else k: v: [ (mkLine k v) ];
+  in attrs: libStr.concatStrings (lib.concatLists (libAttr.mapAttrsToList mkLines attrs));
 
 
   /* Generate an INI-style config file from an
@@ -106,7 +110,9 @@ rec {
     # apply transformations (e.g. escapes) to section names
     mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
     # format a setting line from key and value
-    mkKeyValue    ? mkKeyValueDefault {} "="
+    mkKeyValue    ? mkKeyValueDefault {} "=",
+    # allow lists as values for duplicate keys
+    listsAsDuplicateKeys ? false
   }: attrsOfAttrs:
     let
         # map function to string for each key val
@@ -115,7 +121,7 @@ rec {
             (libAttr.mapAttrsToList mapFn attrs);
         mkSection = sectName: sectValues: ''
           [${mkSectionName sectName}]
-        '' + toKeyValue { inherit mkKeyValue; } sectValues;
+        '' + toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
     in
       # map input to ini sections
       mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;