diff options
author | Naïm Favier <n@monade.li> | 2022-07-17 12:32:28 +0200 |
---|---|---|
committer | Naïm Favier <n@monade.li> | 2022-07-24 18:03:28 +0200 |
commit | eb72be85415765d1538bdf6e3b06090614f7a6f7 (patch) | |
tree | 537401af701737820e4a78b17e79a6ef92bab28a /lib | |
parent | e703a434b7210164d8042585509b6e87eb3fadb6 (diff) |
lib/types: add `number`
For numbers that can be ints or floats.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/types.nix | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/lib/types.nix b/lib/types.nix index 354714b28733..76001804e721 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -226,11 +226,11 @@ rec { }; int = mkOptionType { - name = "int"; - description = "signed integer"; - check = isInt; - merge = mergeEqualOption; - }; + name = "int"; + description = "signed integer"; + check = isInt; + merge = mergeEqualOption; + }; # Specialized subdomains of int ints = @@ -291,10 +291,34 @@ rec { port = ints.u16; float = mkOptionType { - name = "float"; - description = "floating point number"; - check = isFloat; - merge = mergeEqualOption; + name = "float"; + description = "floating point number"; + check = isFloat; + merge = mergeEqualOption; + }; + + number = either int float; + + numbers = let + betweenDesc = lowest: highest: + "${builtins.toJSON lowest} and ${builtins.toJSON highest} (both inclusive)"; + in { + between = lowest: highest: + assert lib.assertMsg (lowest <= highest) + "numbers.between: lowest must be smaller than highest"; + addCheck number (x: x >= lowest && x <= highest) // { + name = "numberBetween"; + description = "integer or floating point number between ${betweenDesc lowest highest}"; + }; + + nonnegative = addCheck number (x: x >= 0) // { + name = "numberNonnegative"; + description = "nonnegative integer or floating point number, meaning >=0"; + }; + positive = addCheck number (x: x > 0) // { + name = "numberPositive"; + description = "positive integer or floating point number, meaning >0"; + }; }; str = mkOptionType { |