git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #147000 from hercules-ci/modules-moduleType

lib/modules: Add moduleType parameter

commit322b3bff7c3e0095b3375b875268eb357e3f1cb3 authorRobert Hensing <roberth@users.noreply.github.com> dateMon 22 Nov 2021, 17:53 (5 years ago) parent859f761d464ccf
4 files changed+41
lib/modules.nix+5 lib/tests/modules.sh+5 lib/tests/modules/declare-variants.nix+9 lib/tests/modules/define-variant.nix+22
lib/modules.nix+5
@@ -62,6 +62,8 @@ rec {
6262 ‘type’: A module system type representing the module set as a submodule,
6363 to be extended by configuration from the containing module set.
6464
65+ This is also available as the module argument ‘moduleType’.
66+
6567 ‘extendModules’: A function similar to ‘evalModules’ but building on top
6668 of the module set. Its arguments, ‘modules’ and ‘specialArgs’ are
6769 added to the existing values.
@@ -74,6 +76,8 @@ rec {
7476 override existing configuration fundamentally requires a new
7577 fixpoint to be constructed.
7678
79+ This is also available as a module argument.
80+
7781 ‘_module’: A portion of the configuration tree which is elided from
7882 ‘config’. It contains some values that are mostly internal to the
7983 module system implementation.
@@ -146,6 +150,7 @@ rec {
146150 config = {
147151 _module.args = {
148152 inherit extendModules;
153+ moduleType = type;
149154 } // args;
150155 };
151156 };
lib/tests/modules.sh+5
@@ -281,6 +281,11 @@ checkConfigError 'A definition for option .fun.\[function body\]. is not of type
281281checkConfigOutput "b a" config.result ./functionTo/list-order.nix
282282checkConfigOutput "a c" config.result ./functionTo/merging-attrs.nix
283283
284+# moduleType
285+checkConfigOutput "a b" config.resultFoo ./declare-variants.nix ./define-variant.nix
286+checkConfigOutput "a y z" config.resultFooBar ./declare-variants.nix ./define-variant.nix
287+checkConfigOutput "a b c" config.resultFooFoo ./declare-variants.nix ./define-variant.nix
288+
284289cat <<EOF
285290====== module tests ======
286291$pass Pass
lib/tests/modules/declare-variants.nixnew file+9
@@ -0,0 +1,9 @@
1+{ lib, moduleType, ... }:
2+let inherit (lib) mkOption types;
3+in
4+{
5+ options.variants = mkOption {
6+ type = types.lazyAttrsOf moduleType;
7+ default = {};
8+ };
9+}
lib/tests/modules/define-variant.nixnew file+22
@@ -0,0 +1,22 @@
1+{ config, lib, ... }:
2+let inherit (lib) types mkOption attrNames;
3+in
4+{
5+ options = {
6+ attrs = mkOption { type = types.attrsOf lib.types.int; };
7+ result = mkOption { };
8+ resultFoo = mkOption { };
9+ resultFooBar = mkOption { };
10+ resultFooFoo = mkOption { };
11+ };
12+ config = {
13+ attrs.a = 1;
14+ variants.foo.attrs.b = 1;
15+ variants.bar.attrs.y = 1;
16+ variants.foo.variants.bar.attrs.z = 1;
17+ variants.foo.variants.foo.attrs.c = 3;
18+ resultFoo = lib.concatMapStringsSep " " toString (attrNames config.variants.foo.attrs);
19+ resultFooBar = lib.concatMapStringsSep " " toString (attrNames config.variants.foo.variants.bar.attrs);
20+ resultFooFoo = lib.concatMapStringsSep " " toString (attrNames config.variants.foo.variants.foo.attrs);
21+ };
22+}