about summary refs log tree commit diff
path: root/pkgs/pkgs-lib/formats.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2021-05-04 09:49:25 +0200
committerGitHub <noreply@github.com>2021-05-04 09:49:25 +0200
commitb8336c2b8abaabb4307ef2f1c38ec48f2f1dbf6f (patch)
treea930fd87aeef8f1719fcdd6722123c6f11b44393 /pkgs/pkgs-lib/formats.nix
parent3cc34f9e559e3d6d06c7fdb29392e04beef805e1 (diff)
formats.ini: Introduce `listToValue` argument (#121613)
Allows coercing lists to values. E.g.

  formats.ini { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); }
Diffstat (limited to 'pkgs/pkgs-lib/formats.nix')
-rw-r--r--pkgs/pkgs-lib/formats.nix26
1 files changed, 24 insertions, 2 deletions
diff --git a/pkgs/pkgs-lib/formats.nix b/pkgs/pkgs-lib/formats.nix
index 14589f8ecdc3a..4b6982f387d08 100644
--- a/pkgs/pkgs-lib/formats.nix
+++ b/pkgs/pkgs-lib/formats.nix
@@ -56,7 +56,16 @@ rec {
       };
     };
 
-  ini = { listsAsDuplicateKeys ? false, ... }@args: {
+  ini = {
+    # Represents lists as duplicate keys
+    listsAsDuplicateKeys ? false,
+    # Alternative to listsAsDuplicateKeys, converts list to non-list
+    # listToValue :: [IniAtom] -> IniAtom
+    listToValue ? null,
+    ...
+    }@args:
+    assert !listsAsDuplicateKeys || listToValue == null;
+    {
 
     type = with lib.types; let
 
@@ -74,12 +83,25 @@ rec {
           coercedTo singleIniAtom lib.singleton (listOf singleIniAtom) // {
             description = singleIniAtom.description + " or a list of them for duplicate keys";
           }
+        else if listToValue != null then
+          coercedTo singleIniAtom lib.singleton (nonEmptyListOf singleIniAtom) // {
+            description = singleIniAtom.description + " or a non-empty list of them";
+          }
         else
           singleIniAtom;
 
     in attrsOf (attrsOf iniAtom);
 
-    generate = name: value: pkgs.writeText name (lib.generators.toINI args value);
+    generate = name: value:
+      let
+        transformedValue =
+          if listToValue != null
+          then
+            lib.mapAttrs (section: lib.mapAttrs (key: val:
+              if lib.isList val then listToValue val else val
+            )) value
+          else value;
+      in pkgs.writeText name (lib.generators.toINI (removeAttrs args ["listToValue"]) transformedValue);
 
   };