git.iko.soy/typeable/nixpkgs
on master help sign in
lib.types: Introduce `types.optionType`

This type correctly merges multiple option types together while also annotating them with file information. In a future commit this will be used for `_module.freeformType`

commit5cbeddfde486ca5524baeaf3da6e8944075cf463 authorSilvan Mosberger <contact@infinisil.com> dateWed 8 Dec 2021, 19:02 (5 years ago) parent891e65b
6 files changed+113−1
lib/tests/modules.sh+7 lib/tests/modules/optionTypeFile.nix+28 lib/tests/modules/optionTypeMerging.nix+27 lib/types.nix+30−1 nixos/doc/manual/development/option-types.section.md+7 nixos/doc/manual/from_md/development/option-types.section.xml+14
lib/tests/modules.sh+7
@@ -299,6 +299,13 @@ checkConfigOutput "10" config.processedToplevel ./raw.nix
299299checkConfigError "The option .multiple. is defined multiple times" config.multiple ./raw.nix
300300checkConfigOutput "bar" config.priorities ./raw.nix
301301
302+# Test that types.optionType merges types correctly
303+checkConfigOutput '^10$' config.theOption.int ./optionTypeMerging.nix
304+checkConfigOutput '^"hello"$' config.theOption.str ./optionTypeMerging.nix
305+
306+# Test that types.optionType correctly annotates option locations
307+checkConfigError 'The option .theOption.nested. in .other.nix. is already declared in .optionTypeFile.nix.' config.theOption.nested ./optionTypeFile.nix
308+
302309cat <<EOF
303310====== module tests ======
304311$pass Pass
lib/tests/modules/optionTypeFile.nixnew file+28
@@ -0,0 +1,28 @@
1+{ config, lib, ... }: {
2+
3+ _file = "optionTypeFile.nix";
4+
5+ options.theType = lib.mkOption {
6+ type = lib.types.optionType;
7+ };
8+
9+ options.theOption = lib.mkOption {
10+ type = config.theType;
11+ default = {};
12+ };
13+
14+ config.theType = lib.mkMerge [
15+ (lib.types.submodule {
16+ options.nested = lib.mkOption {
17+ type = lib.types.int;
18+ };
19+ })
20+ (lib.types.submodule {
21+ _file = "other.nix";
22+ options.nested = lib.mkOption {
23+ type = lib.types.str;
24+ };
25+ })
26+ ];
27+
28+}
lib/tests/modules/optionTypeMerging.nixnew file+27
@@ -0,0 +1,27 @@
1+{ config, lib, ... }: {
2+
3+ options.theType = lib.mkOption {
4+ type = lib.types.optionType;
5+ };
6+
7+ options.theOption = lib.mkOption {
8+ type = config.theType;
9+ };
10+
11+ config.theType = lib.mkMerge [
12+ (lib.types.submodule {
13+ options.int = lib.mkOption {
14+ type = lib.types.int;
15+ default = 10;
16+ };
17+ })
18+ (lib.types.submodule {
19+ options.str = lib.mkOption {
20+ type = lib.types.str;
21+ };
22+ })
23+ ];
24+
25+ config.theOption.str = "hello";
26+
27+}
lib/types.nix+30−1
@@ -61,7 +61,11 @@ let
6161 boolToString
6262 ;
6363
64− inherit (lib.modules) mergeDefinitions;
64+ inherit (lib.modules)
65+ mergeDefinitions
66+ fixupOptionType
67+ mergeOptionDecls
68+ ;
6569 outer_types =
6670rec {
6771 isType = type: x: (x._type or "") == type;
@@ -525,6 +529,31 @@ rec {
525529 modules = toList modules;
526530 };
527531
532+ # The type of a type!
533+ optionType = mkOptionType {
534+ name = "optionType";
535+ description = "optionType";
536+ check = value: value._type or null == "option-type";
537+ merge = loc: defs:
538+ let
539+ # Prepares the type definitions for mergeOptionDecls, which
540+ # annotates submodules types with file locations
541+ optionModules = map ({ value, file }:
542+ {
543+ _file = file;
544+ # There's no way to merge types directly from the module system,
545+ # but we can cheat a bit by just declaring an option with the type
546+ options = lib.mkOption {
547+ type = value;
548+ };
549+ }
550+ ) defs;
551+ # Merges all the types into a single one, including submodule merging.
552+ # This also propagates file information to all submodules
553+ mergedOption = fixupOptionType loc (mergeOptionDecls loc optionModules);
554+ in mergedOption.type;
555+ };
556+
528557 submoduleWith =
529558 { modules
530559 , specialArgs ? {}
nixos/doc/manual/development/option-types.section.md+7
@@ -74,6 +74,13 @@ merging is handled.
7474 should only be used when checking, merging and nested evaluation are not
7575 desirable.
7676
77+`types.optionType`
78+
79+: The type of an option's type. Its merging operation ensures that nested
80+ options have the correct file location annotated, and that if possible,
81+ multiple option definitions are correctly merged together. The main use
82+ case is as the type of the `_module.freeformType` option.
83+
7784`types.attrs`
7885
7986: A free-form attribute set.
nixos/doc/manual/from_md/development/option-types.section.xml+14
@@ -111,6 +111,20 @@
111111 </para>
112112 </listitem>
113113 </varlistentry>
114+ <varlistentry>
115+ <term>
116+ <literal>types.optionType</literal>
117+ </term>
118+ <listitem>
119+ <para>
120+ The type of an option’s type. Its merging operation ensures
121+ that nested options have the correct file location
122+ annotated, and that if possible, multiple option definitions
123+ are correctly merged together. The main use case is as the
124+ type of the <literal>_module.freeformType</literal> option.
125+ </para>
126+ </listitem>
127+ </varlistentry>
114128 <varlistentry>
115129 <term>
116130 <literal>types.attrs</literal>