summary refs log tree commit diff
path: root/lib/types.nix
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2017-01-14 04:14:01 +0300
committerNikolay Amiantov <ab@fmap.me>2017-02-02 01:53:00 +0300
commit5cbcd3af69d859a6d919ad26261c57649e9c888f (patch)
tree48632f4694ed079a27f73eb2f98a57716571c873 /lib/types.nix
parentf673243aff0bc9ae4d2e96ccd60124ff9fe5b103 (diff)
downloadnixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.tar
nixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.tar.gz
nixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.tar.bz2
nixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.tar.lz
nixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.tar.xz
nixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.tar.zst
nixpkgs-5cbcd3af69d859a6d919ad26261c57649e9c888f.zip
types library: add coercedTo
Diffstat (limited to 'lib/types.nix')
-rw-r--r--lib/types.nix22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/types.nix b/lib/types.nix
index 9366d394da7..0d1a88a00f2 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -352,6 +352,28 @@ rec {
       functor = (defaultFunctor name) // { wrapped = [ t1 t2 ]; };
     };
 
+    coercedTo = coercedType: coerceFunc: finalType:
+      assert coercedType.getSubModules == null;
+      mkOptionType rec {
+        name = "coercedTo";
+        description = "${finalType.description} or ${coercedType.description}";
+        check = x: finalType.check x || coercedType.check x;
+        merge = loc: defs:
+          let
+            coerceVal = val:
+              if finalType.check val then val
+              else let
+                coerced = coerceFunc val;
+              in assert finalType.check coerced; coerced;
+
+          in finalType.merge loc (map (def: def // { value = coerceVal def.value; }) defs);
+        getSubOptions = finalType.getSubOptions;
+        getSubModules = finalType.getSubModules;
+        substSubModules = m: coercedTo coercedType coerceFunc (finalType.substSubModules m);
+        typeMerge = t1: t2: null;
+        functor = (defaultFunctor name) // { wrapped = finalType; };
+      };
+
     # Obsolete alternative to configOf.  It takes its option
     # declarations from the ‘options’ attribute of containing option
     # declaration.