diff options
author | Robert Hensing <robert@roberthensing.nl> | 2022-03-10 22:45:41 +0100 |
---|---|---|
committer | Robert Hensing <robert@roberthensing.nl> | 2022-06-14 23:01:23 +0200 |
commit | 4746f6d03e4f8dc6e7399f45aaba0ca3aac32761 (patch) | |
tree | 454bacc3665fb4f41f6f06322dd799367403ebb3 /lib | |
parent | 9ef09e06806e79e32e30d17aee6879d69c011037 (diff) |
lib.types: Add deferredModule
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/tests/modules.sh | 3 | ||||
-rw-r--r-- | lib/tests/modules/deferred-module.nix | 54 | ||||
-rw-r--r-- | lib/types.nix | 8 |
3 files changed, 65 insertions, 0 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 36af32ca89da0..9b1348f58c9c2 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -194,6 +194,9 @@ checkConfigOutput '^"submodule"$' options.submodule.type.description ./declare-s ## Paths should be allowed as values and work as expected checkConfigOutput '^true$' config.submodule.enable ./declare-submoduleWith-path.nix +## Deferred module +checkConfigOutput '"beta"' config.nodes.foo.settingsDict.c ./deferred-module.nix + # Check the file location information is propagated into submodules checkConfigOutput the-file.nix config.submodule.internalFiles.0 ./submoduleFiles.nix diff --git a/lib/tests/modules/deferred-module.nix b/lib/tests/modules/deferred-module.nix new file mode 100644 index 0000000000000..faf459a991fa6 --- /dev/null +++ b/lib/tests/modules/deferred-module.nix @@ -0,0 +1,54 @@ +{ lib, ... }: +let + inherit (lib) types mkOption setDefaultModuleLocation; + inherit (types) deferredModule lazyAttrsOf submodule str raw; +in +{ + imports = [ + # generic module, declaring submodules: + # - nodes.<name> + # - default + # where all nodes include the default + ({ config, ... }: { + _file = "generic.nix"; + options.nodes = mkOption { + type = lazyAttrsOf (submodule { imports = config.default; }); + default = {}; + }; + options.default = mkOption { + type = deferredModule; + default = { }; + description = '' + Module that is included in all nodes. + ''; + }; + }) + + { + _file = "default-1.nix"; + default = { config, ... }: { + options.settingsDict = lib.mkOption { type = lazyAttrsOf str; default = {}; }; + }; + } + + { + _file = "default-a-is-b.nix"; + default = { config, ... }: { + settingsDict.a = config.settingsDict.b; + }; + } + + { + _file = "nodes-foo.nix"; + nodes.foo.settingsDict.b = "beta"; + } + + { + _file = "nodes-foo-c-is-a.nix"; + nodes.foo = { config, ... }: { + settingsDict.c = config.settingsDict.a; + }; + } + + ]; +} diff --git a/lib/types.nix b/lib/types.nix index caaa6dccc6d41..22a3292644572 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -539,6 +539,14 @@ rec { modules = toList modules; }; + # A module to be imported in some other part of the configuration. + deferredModule = mkOptionType { + name = "deferredModule"; + description = "module"; + check = t: isAttrs t || isFunction t; + merge = loc: defs: map (def: lib.setDefaultModuleLocation "${showOption loc} from ${def.file}" def.value) defs; + }; + # The type of a type! optionType = mkOptionType { name = "optionType"; |