git.iko.soy/typeable/nixpkgs
on master help sign in
modules: Add moduleType to module arguments
commitd464ccfdd9e101d4f5a112ab58d91ccbb912524a authorRobert Hensing <robert@roberthensing.nl> dateMon 22 Nov 2021, 14:39 (5 years ago) parent426ab31
4 files changed+39
lib/modules.nix+3 lib/tests/modules.sh+5 lib/tests/modules/declare-variants.nix+9 lib/tests/modules/define-variant.nix+22
lib/modules.nix+3
@@ -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.
@@ -148,6 +150,7 @@ rec {
148150 config = {
149151 _module.args = {
150152 inherit extendModules;
153+ moduleType = type;
151154 } // args;
152155 };
153156 };
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+}