summary refs log tree commit diff
path: root/lib/generators.nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2016-12-04 22:11:24 +0100
committerGitHub <noreply@github.com>2016-12-04 22:11:24 +0100
commitea412cd5a1b3ca93d86f7a43dcdecb5784155dae (patch)
tree82fdd805e6681835ca638db97c07642b27b37aad /lib/generators.nix
parent2f861e6ba652eb580babd179a632a1413caae061 (diff)
downloadnixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.tar
nixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.tar.gz
nixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.tar.bz2
nixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.tar.lz
nixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.tar.xz
nixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.tar.zst
nixlib-ea412cd5a1b3ca93d86f7a43dcdecb5784155dae.zip
lib/generators: add toKeyValue & mkKeyValueLine (#20903)
generators for the common use case of simple config files which hold keys and
values. Used in the implementation for toINI.
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix29
1 files changed, 25 insertions, 4 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 27d4142e8d9a..2c0cf5775f47 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -17,7 +17,29 @@ in
 
 rec {
 
-  /* Generates an INI-style config file from an
+  /* Generate a line of key k and value v, separated by
+   * character sep. If sep appears in k, it is escaped.
+   * Helper for synaxes with different separators.
+   *
+   * mkKeyValueLine ":" "f:oo" "bar"
+   * > "f\:oo:bar"
+   */
+  mkKeyValueLine = sep: k: v:
+    "${libStr.escape [sep] k}${sep}${toString v}";
+
+
+  /* Generate a key-value-style config file from an attrset.
+   *
+   * mkKeyValue is the same as in toINI.
+   */
+  toKeyValue = {
+    mkKeyValue ? mkKeyValueLine "="
+  }: attrs:
+    let mkLine = k: v: mkKeyValue k v + "\n";
+    in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs);
+
+
+  /* Generate an INI-style config file from an
    * attrset of sections to an attrset of key-value pairs.
    *
    * generators.toINI {} {
@@ -41,17 +63,16 @@ rec {
     # apply transformations (e.g. escapes) to section names
     mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
     # format a setting line from key and value
-    mkKeyValue    ? (k: v: "${libStr.escape ["="] k}=${toString v}")
+    mkKeyValue    ? mkKeyValueLine "="
   }: attrsOfAttrs:
     let
         # map function to string for each key val
         mapAttrsToStringsSep = sep: mapFn: attrs:
           libStr.concatStringsSep sep
             (libAttr.mapAttrsToList mapFn attrs);
-        mkLine = k: v: mkKeyValue k v + "\n";
         mkSection = sectName: sectValues: ''
           [${mkSectionName sectName}]
-        '' + libStr.concatStrings (libAttr.mapAttrsToList mkLine sectValues);
+        '' + toKeyValue { inherit mkKeyValue; } sectValues;
     in
       # map input to ini sections
       mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;