diff options
author | Nikolay Amiantov <ab@fmap.me> | 2017-01-14 04:14:01 +0300 |
---|---|---|
committer | Nikolay Amiantov <ab@fmap.me> | 2017-02-02 01:53:00 +0300 |
commit | 5cbcd3af69d859a6d919ad26261c57649e9c888f (patch) | |
tree | 48632f4694ed079a27f73eb2f98a57716571c873 /lib/types.nix | |
parent | f673243aff0bc9ae4d2e96ccd60124ff9fe5b103 (diff) |
types library: add coercedTo
Diffstat (limited to 'lib/types.nix')
-rw-r--r-- | lib/types.nix | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/types.nix b/lib/types.nix index 9366d394da73..0d1a88a00f2d 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. |