From 462c048c77fa0aa0cb4c47d7812a57f094587f68 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 31 Oct 2017 17:30:15 +0100 Subject: [PATCH] lib/types: add `ints.positive`. For values that are positive, but cannot be 0. --- lib/tests/modules.sh | 2 ++ lib/tests/modules/declare-int-positive-value.nix | 9 +++++++++ lib/tests/modules/define-value-int-zero.nix | 3 +++ lib/types.nix | 4 ++++ nixos/doc/manual/development/option-types.xml | 7 +++++++ 5 files changed, 25 insertions(+) create mode 100644 lib/tests/modules/declare-int-positive-value.nix create mode 100644 lib/tests/modules/define-value-int-zero.nix diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index ac0372e66f8..96a91c0fffb 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -65,6 +65,8 @@ checkConfigError 'The option .* defined in .* does not exist.' config.enable ./d # unsigned checkConfigOutput "42" config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix checkConfigError 'The option value .* in .* is not of type.*unsigned integer.*' config.value ./declare-int-unsigned-value.nix ./define-value-int-negative.nix +# positive +checkConfigError 'The option value .* in .* is not of type.*positive integer.*' config.value ./declare-int-positive-value.nix ./define-value-int-zero.nix # between 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 diff --git a/lib/tests/modules/declare-int-positive-value.nix b/lib/tests/modules/declare-int-positive-value.nix new file mode 100644 index 00000000000..6e48c6ac8fe --- /dev/null +++ b/lib/tests/modules/declare-int-positive-value.nix @@ -0,0 +1,9 @@ +{ lib, ... }: + +{ + options = { + value = lib.mkOption { + type = lib.types.ints.positive; + }; + }; +} diff --git a/lib/tests/modules/define-value-int-zero.nix b/lib/tests/modules/define-value-int-zero.nix new file mode 100644 index 00000000000..68bb9f415c3 --- /dev/null +++ b/lib/tests/modules/define-value-int-zero.nix @@ -0,0 +1,3 @@ +{ + value = 0; +} diff --git a/lib/types.nix b/lib/types.nix index 021641fdc22..1675a54156d 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -151,6 +151,10 @@ rec { name = "unsignedInt"; description = "unsigned integer, meaning >=0"; }; + positive = addCheck types.int (x: x > 0) // { + name = "positiveInt"; + description = "positive integer, meaning >0"; + }; u8 = unsign 8 256; u16 = unsign 16 65536; u32 = unsign 32 4294967296; diff --git a/nixos/doc/manual/development/option-types.xml b/nixos/doc/manual/development/option-types.xml index 0b00f7077c2..83dcf0232d9 100644 --- a/nixos/doc/manual/development/option-types.xml +++ b/nixos/doc/manual/development/option-types.xml @@ -78,6 +78,13 @@ for 8 bits). + + + types.ints.positive + + A positive integer (that is > 0). + + String-related types: