From 5cbcd3af69d859a6d919ad26261c57649e9c888f Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Sat, 14 Jan 2017 04:14:01 +0300 Subject: types library: add coercedTo --- lib/types.nix | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib') 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. -- cgit 1.4.1