about summary refs log tree commit diff
path: root/nixpkgs/lib
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2019-08-22 13:47:37 +0000
committerAlyssa Ross <hi@alyssa.is>2019-08-22 18:22:22 +0000
commit1b9a13c4689af7e088eb7af5589f8c811282846a (patch)
tree3ed032953008280fb94ef894c869ff3e2a2f7865 /nixpkgs/lib
parent4999a38db7c5de0ea9f514a12ecd4133cce647f3 (diff)
parent1412af4b2cfae71d447164097d960d426e9752c0 (diff)
downloadnixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.tar
nixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.tar.gz
nixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.tar.bz2
nixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.tar.lz
nixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.tar.xz
nixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.tar.zst
nixlib-1b9a13c4689af7e088eb7af5589f8c811282846a.zip
Merge remote-tracking branch 'channels/nixos-unstable'
Diffstat (limited to 'nixpkgs/lib')
-rw-r--r--nixpkgs/lib/default.nix2
-rw-r--r--nixpkgs/lib/lists.nix13
-rwxr-xr-xnixpkgs/lib/tests/modules.sh9
-rw-r--r--nixpkgs/lib/tests/modules/declare-either.nix5
-rw-r--r--nixpkgs/lib/tests/modules/declare-oneOf.nix9
-rw-r--r--nixpkgs/lib/types.nix7
6 files changed, 44 insertions, 1 deletions
diff --git a/nixpkgs/lib/default.nix b/nixpkgs/lib/default.nix
index 3efaaf0f8f9e..18d2dfae1e18 100644
--- a/nixpkgs/lib/default.nix
+++ b/nixpkgs/lib/default.nix
@@ -71,7 +71,7 @@ let
       zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
       recursiveUpdate matchAttrs overrideExisting getOutput getBin
       getLib getDev chooseDevOutputs zipWithNames zip;
-    inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
+    inherit (lists) singleton forEach foldr fold foldl foldl' imap0 imap1
       concatMap flatten remove findSingle findFirst any all count
       optional optionals toList range partition zipListsWith zipLists
       reverseList listDfs toposort sort naturalSort compareLists take
diff --git a/nixpkgs/lib/lists.nix b/nixpkgs/lib/lists.nix
index 30d87ece6641..e4fcf959b60c 100644
--- a/nixpkgs/lib/lists.nix
+++ b/nixpkgs/lib/lists.nix
@@ -21,6 +21,19 @@ rec {
   */
   singleton = x: [x];
 
+  /*  Apply the function to each element in the list. Same as `map`, but arguments
+      flipped.
+
+      Type: forEach :: [a] -> (a -> b) -> [b]
+
+      Example:
+        forEach [ 1 2 ] (x:
+          toString x
+        )
+        => [ "1" "2" ]
+  */
+  forEach = xs: f: map f xs;
+
   /* “right fold” a binary function `op` between successive elements of
      `list` with `nul' as the starting value, i.e.,
      `foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))`.
diff --git a/nixpkgs/lib/tests/modules.sh b/nixpkgs/lib/tests/modules.sh
index eadaa0521b35..cf344122cf4e 100755
--- a/nixpkgs/lib/tests/modules.sh
+++ b/nixpkgs/lib/tests/modules.sh
@@ -71,6 +71,15 @@ checkConfigError 'The option value .* in .* is not of type.*positive integer.*'
 checkConfigOutput "42" config.value ./declare-int-between-value.nix ./define-value-int-positive.nix
 checkConfigError 'The option value .* in .* is not of type.*between.*-21 and 43.*inclusive.*' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix
 
+# Check either types
+# types.either
+checkConfigOutput "42" config.value ./declare-either.nix ./define-value-int-positive.nix
+checkConfigOutput "\"24\"" config.value ./declare-either.nix ./define-value-string.nix
+# types.oneOf
+checkConfigOutput "42" config.value ./declare-oneOf.nix ./define-value-int-positive.nix
+checkConfigOutput "[ ]" config.value ./declare-oneOf.nix ./define-value-list.nix
+checkConfigOutput "\"24\"" config.value ./declare-oneOf.nix ./define-value-string.nix
+
 # Check mkForce without submodules.
 set -- config.enable ./declare-enable.nix ./define-enable.nix
 checkConfigOutput "true" "$@"
diff --git a/nixpkgs/lib/tests/modules/declare-either.nix b/nixpkgs/lib/tests/modules/declare-either.nix
new file mode 100644
index 000000000000..5a0fa978a138
--- /dev/null
+++ b/nixpkgs/lib/tests/modules/declare-either.nix
@@ -0,0 +1,5 @@
+{ lib, ... }: {
+  options.value = lib.mkOption {
+    type = lib.types.either lib.types.int lib.types.str;
+  };
+}
diff --git a/nixpkgs/lib/tests/modules/declare-oneOf.nix b/nixpkgs/lib/tests/modules/declare-oneOf.nix
new file mode 100644
index 000000000000..df092a14f81e
--- /dev/null
+++ b/nixpkgs/lib/tests/modules/declare-oneOf.nix
@@ -0,0 +1,9 @@
+{ lib, ... }: {
+  options.value = lib.mkOption {
+    type = lib.types.oneOf [
+      lib.types.int
+      (lib.types.listOf lib.types.int)
+      lib.types.str
+    ];
+  };
+}
diff --git a/nixpkgs/lib/types.nix b/nixpkgs/lib/types.nix
index e22bcd326c86..9c00656ab918 100644
--- a/nixpkgs/lib/types.nix
+++ b/nixpkgs/lib/types.nix
@@ -443,6 +443,13 @@ rec {
       functor = (defaultFunctor name) // { wrapped = [ t1 t2 ]; };
     };
 
+    # Any of the types in the given list
+    oneOf = ts:
+      let
+        head' = if ts == [] then throw "types.oneOf needs to get at least one type in its argument" else head ts;
+        tail' = tail ts;
+      in foldl' either head' tail';
+
     # Either value of type `finalType` or `coercedType`, the latter is
     # converted to `finalType` using `coerceFunc`.
     coercedTo = coercedType: coerceFunc: finalType: