Skip to content

Commit

Permalink
lib: introduce types.hex
Browse files Browse the repository at this point in the history
This change introduces lib.types.hex, I thought that it's a common
enough numer that it's worth the change.
I decided against adding it to other lib functions like
lib.types.number, so the interface keeps stable.
  • Loading branch information
Janik-Haag committed Apr 12, 2024
1 parent 8a22284 commit a8eb148
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ checkConfigError 'A definition for option .* is not of type.*positive integer.*.
checkConfigOutput '^42$' config.value ./declare-int-between-value.nix ./define-value-int-positive.nix
checkConfigError 'A definition for option .* is not of type.*between.*-21 and 43.*inclusive.*. Definition values:\n\s*- In .*: -23' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix

# Check hex string type
# types.hexStr
checkConfigOutput '^"64075c"$' config.value ./declare-hex-value.nix ./define-value-is-hex.nix
checkConfigError 'A definition for option .* is not of type.*hexadecimal value represented as string.*. Definition values:\n\s*- In .*: "notAhex"' config.value ./declare-hex-value.nix ./define-value-not-hex.nix

# Check either types
# types.either
checkConfigOutput '^42$' config.value ./declare-either.nix ./define-value-int-positive.nix
Expand Down
9 changes: 9 additions & 0 deletions lib/tests/modules/declare-hex-value.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ lib, ... }:

{
options = {
value = lib.mkOption {
type = lib.types.hexStr;
};
};
}
3 changes: 3 additions & 0 deletions lib/tests/modules/define-value-is-hex.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
value = "64075c";
}
3 changes: 3 additions & 0 deletions lib/tests/modules/define-value-not-hex.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
value = "notAhex";
}
8 changes: 8 additions & 0 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,14 @@ rec {
merge = mergeEqualOption;
};

hexStr = mkOptionType {
name = "hexStr";
description = "hexadecimal value represented as string";
descriptionClass = "noun";
check = x: str.check x && builtins.match "[0-9a-f]+" x != null;
merge = mergeEqualOption;
};

number = either int float;

numbers = let
Expand Down
6 changes: 6 additions & 0 deletions nixos/doc/manual/development/option-types.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ merging is handled.

: A string. Multiple definitions are concatenated with a colon `":"`.

`types.hexStr`

: A hexadecimal value represented as string. The type only allows lowercase
values. The type also does not allow for hex prefixes like `0x` or `#`.


`types.strMatching`

: A string matching a specific regular expression. Multiple
Expand Down

0 comments on commit a8eb148

Please sign in to comment.