diff --git a/lib/modules.nix b/lib/modules.nix index 89c12bcf1a77..73cc9fc94ca4 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -389,6 +389,7 @@ let value = m; _type = m._type; expectedClass = class; + prefix = args._prefix; } ) else if isList m then @@ -1977,6 +1978,10 @@ let file != null && file != unknownModule ) ", while trying to load a module into ${toString file}"; + into_prefix_maybe = + prefix: + optionalString (prefix != [ ]) ", while trying to load a module into ${code (showOption prefix)}"; + /** Format text with one line break between each list item. */ @@ -2029,7 +2034,7 @@ let paragraphs ( [ '' - Expected a module, but found a value of type ${warn (escapeNixString _type)}${into_fallback_file_maybe fallbackFile}. + Expected a module, but found a value of type ${warn (escapeNixString _type)}${into_fallback_file_maybe fallbackFile}${into_prefix_maybe prefix}. A module is typically loaded by adding it to the ${code "imports = [ ... ];"} attribute of an existing module, or in the ${code "modules = [ ... ];"} argument of various functions. Please make sure that each of the list items is a module, and not a different kind of value. '' diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index d7006d9c58fc..929543fd827f 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -606,6 +606,7 @@ checkConfigError 'Expected a module, but found a value of type .*"flake".*, whil checkConfigOutput '^true$' config.enable ./declare-enable.nix ./define-enable-with-top-level-mkIf.nix checkConfigError 'Expected a module, but found a value of type .*"configuration".*, while trying to load a module into .*/import-configuration.nix.' config ./import-configuration.nix checkConfigError 'please only import the modules that make up the configuration' config ./import-configuration.nix +checkConfigError 'Expected a module, but found a value of type "configuration", while trying to load a module into .*/import-error-submodule.nix, while trying to load a module into .*foo.*\.' config.foo ./import-error-submodule.nix # doRename works when `warnings` does not exist. checkConfigOutput '^1234$' config.c.d.e ./doRename-basic.nix diff --git a/lib/tests/modules/import-error-submodule.nix b/lib/tests/modules/import-error-submodule.nix new file mode 100644 index 000000000000..d2237c58bb5a --- /dev/null +++ b/lib/tests/modules/import-error-submodule.nix @@ -0,0 +1,18 @@ +{ lib, ... }: +let + myconf = lib.evalModules { modules = [ { } ]; }; +in +{ + options.foo = lib.mkOption { + type = lib.types.submodule { }; + default = { }; + }; + config.foo = + { ... }: + { + imports = [ + # error, like `import-configuration.nix`, but in a submodule this time + myconf + ]; + }; +}