Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/types: Add oneOf, extension of either to a list of types #65728

Merged
merged 2 commits into from Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/tests/modules.sh
Expand Up @@ -71,6 +71,15 @@ checkConfigError 'The option value .* in .* is not of type.*positive integer.*'
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

# Check either types
# types.either
checkConfigOutput "42" config.value ./declare-either.nix ./define-value-int-positive.nix
checkConfigOutput "\"24\"" config.value ./declare-either.nix ./define-value-string.nix
# types.oneOf
checkConfigOutput "42" config.value ./declare-oneOf.nix ./define-value-int-positive.nix
checkConfigOutput "[ ]" config.value ./declare-oneOf.nix ./define-value-list.nix
checkConfigOutput "\"24\"" config.value ./declare-oneOf.nix ./define-value-string.nix

# Check mkForce without submodules.
set -- config.enable ./declare-enable.nix ./define-enable.nix
checkConfigOutput "true" "$@"
Expand Down
5 changes: 5 additions & 0 deletions lib/tests/modules/declare-either.nix
@@ -0,0 +1,5 @@
{ lib, ... }: {
options.value = lib.mkOption {
type = lib.types.either lib.types.int lib.types.str;
};
}
9 changes: 9 additions & 0 deletions lib/tests/modules/declare-oneOf.nix
@@ -0,0 +1,9 @@
{ lib, ... }: {
options.value = lib.mkOption {
type = lib.types.oneOf [
lib.types.int
(lib.types.listOf lib.types.int)
lib.types.str
];
};
}
7 changes: 7 additions & 0 deletions lib/types.nix
Expand Up @@ -443,6 +443,13 @@ rec {
functor = (defaultFunctor name) // { wrapped = [ t1 t2 ]; };
};

# Any of the types in the given list
oneOf = ts:
let
head' = if ts == [] then throw "types.oneOf needs to get at least one type in its argument" else head ts;
tail' = tail ts;
in foldl' either head' tail';

# Either value of type `finalType` or `coercedType`, the latter is
# converted to `finalType` using `coerceFunc`.
coercedTo = coercedType: coerceFunc: finalType:
Expand Down
12 changes: 12 additions & 0 deletions nixos/doc/manual/development/option-types.xml
Expand Up @@ -346,6 +346,18 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname>types.oneOf</varname> [ <replaceable>t1</replaceable> <replaceable>t2</replaceable> ... ]
</term>
<listitem>
<para>
Type <replaceable>t1</replaceable> or type <replaceable>t2</replaceable> and so forth,
e.g. <literal>with types; oneOf [ int str bool ]</literal>. Multiple definitions
cannot be merged.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname>types.coercedTo</varname> <replaceable>from</replaceable> <replaceable>f</replaceable> <replaceable>to</replaceable>
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/backup/automysqlbackup.nix
Expand Up @@ -41,7 +41,7 @@ in
};

config = mkOption {
type = with types; attrsOf (either (either str (either int bool)) (listOf str));
type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
default = {};
description = ''
automysqlbackup configuration. Refer to
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/games/minecraft-server.nix
Expand Up @@ -118,7 +118,7 @@ in {
};

serverProperties = mkOption {
type = with types; attrsOf (either bool (either int str));
type = with types; attrsOf (oneOf [ bool int str ]);
default = {};
example = literalExample ''
{
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/mail/davmail.nix
Expand Up @@ -7,7 +7,7 @@ let
cfg = config.services.davmail;

configType = with types;
either (either (attrsOf configType) str) (either int bool) // {
oneOf [ (attrsOf configType) str int bool ] // {
description = "davmail config type (str, int, bool or attribute set thereof)";
};

Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/mail/postfix.nix
Expand Up @@ -447,7 +447,7 @@ in
};

config = mkOption {
type = with types; attrsOf (either bool (either str (listOf str)));
type = with types; attrsOf (oneOf [ bool str (listOf str) ]);
description = ''
The main.cf configuration file as key value set.
'';
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/mail/rspamd.nix
Expand Up @@ -331,7 +331,7 @@ in
};

config = mkOption {
type = with types; attrsOf (either bool (either str (listOf str)));
type = with types; attrsOf (oneOf [ bool str (listOf str) ]);
description = ''
Addon to postfix configuration
'';
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/mail/rss2email.nix
Expand Up @@ -30,7 +30,7 @@ in {
};

config = mkOption {
type = with types; attrsOf (either str (either int bool));
type = with types; attrsOf (oneOf [ str int bool ]);
default = {};
description = ''
The configuration to give rss2email.
Expand Down
4 changes: 2 additions & 2 deletions nixos/modules/services/networking/znc/default.nix
Expand Up @@ -62,9 +62,9 @@ let
concatStringsSep "\n" (toLines cfg.config);

semanticTypes = with types; rec {
zncAtom = nullOr (either (either int bool) str);
zncAtom = nullOr (oneOf [ int bool str ]);
zncAttr = attrsOf (nullOr zncConf);
zncAll = either (either zncAtom (listOf zncAtom)) zncAttr;
zncAll = oneOf [ zncAtom (listOf zncAtom) zncAttr ];
zncConf = attrsOf (zncAll // {
# Since this is a recursive type and the description by default contains
# the description of its subtypes, infinite recursion would occur without
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/security/bitwarden_rs/default.nix
Expand Up @@ -36,7 +36,7 @@ in {
};

config = mkOption {
type = attrsOf (nullOr (either (either bool int) str));
type = attrsOf (nullOr (oneOf [ bool int str ]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type = attrsOf (nullOr (oneOf [ bool int str ]));
type = attrsOf (oneOf [ (nullOr bool) int str ]);

or oneOf [ null bool int str ]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think null is a type? And I prefer the nullOr (oneOf ...), because it gives the meaning of "either unset or one of these"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've suggested it so it would be compatible with my another suggestion - #65728 (comment)

But I'm fine with this as well.

default = {};
example = literalExample ''
{
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/web-apps/limesurvey.nix
Expand Up @@ -14,7 +14,7 @@ let

pkg = pkgs.limesurvey;

configType = with types; either (either (attrsOf configType) str) (either int bool) // {
configType = with types; oneOf [ (attrsOf configType) str int bool ] // {
description = "limesurvey config type (str, int, bool or attribute set thereof)";
};

Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/services/x11/compton.nix
Expand Up @@ -215,7 +215,7 @@ in {
};

settings = let
configTypes = with types; either bool (either int (either float str));
configTypes = with types; oneOf [ bool int float str ];
# types.loaOf converts lists to sets
loaOf = t: with types; either (listOf t) (attrsOf t);
in mkOption {
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/system/boot/systemd-unit-options.nix
Expand Up @@ -226,7 +226,7 @@ in rec {

environment = mkOption {
default = {};
type = with types; attrsOf (nullOr (either str (either path package)));
type = with types; attrsOf (nullOr (oneOf [ str path package ]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example = { PATH = "/foo/bar/bin"; LANG = "nl_NL.UTF-8"; };
description = "Environment variables passed to the service's processes.";
};
Expand Down
2 changes: 1 addition & 1 deletion nixos/modules/system/boot/systemd.nix
Expand Up @@ -520,7 +520,7 @@ in
};

systemd.globalEnvironment = mkOption {
type = with types; attrsOf (nullOr (either str (either path package)));
type = with types; attrsOf (nullOr (oneOf [ str path package ]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default = {};
example = { TZ = "CET"; };
description = ''
Expand Down