From 363e6978ced1023c6daceaad647155361e3bda9e Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Fri, 4 Dec 2015 07:15:14 +0300 Subject: [PATCH 1/2] Allow enum of integers (and any other type) Closes #9826. --- lib/types.nix | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index 7276f9af9fe..d750768335c 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -230,11 +230,18 @@ rec { substSubModules = m: submodule m; }; - enum = values: mkOptionType { - name = "one of ${concatStringsSep ", " values}"; - check = flip elem values; - merge = mergeOneOption; - }; + enum = values: + let + show = v: + if builtins.isString v then ''"${v}"'' + else if builtins.isInt v then builtins.toString v + else ''<${builtins.typeOf v}>''; + in + mkOptionType { + name = "one of ${concatMapStringsSep ", " show values}"; + check = flip elem values; + merge = mergeOneOption; + }; either = t1: t2: mkOptionType { name = "${t1.name} or ${t2.name}"; From b078ae9c16ce0cbb0022d7c5c59c571a8390d580 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Fri, 4 Dec 2015 07:22:57 +0300 Subject: [PATCH 2/2] Use x == null instead of builtins.isNull x --- lib/types.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index d750768335c..b833417e73d 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -193,9 +193,9 @@ rec { nullOr = elemType: mkOptionType { name = "null or ${elemType.name}"; - check = x: builtins.isNull x || elemType.check x; + check = x: x == null || elemType.check x; merge = loc: defs: - let nrNulls = count (def: isNull def.value) defs; in + let nrNulls = count (def: def.value == null) defs; in if nrNulls == length defs then null else if nrNulls != 0 then throw "The option `${showOption loc}' is defined both null and not null, in ${showFiles (getFiles defs)}."