summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/modules.nix9
-rwxr-xr-xlib/tests/modules.sh4
-rw-r--r--lib/tests/modules/define-enable-with-top-level-mkIf.nix5
-rw-r--r--lib/tests/modules/module-imports-_type-check.nix3
4 files changed, 20 insertions, 1 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 1e8f085e6f4f8..8233b4b9e84eb 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -362,8 +362,15 @@ let
 
       # Like unifyModuleSyntax, but also imports paths and calls functions if necessary
       loadModule = args: fallbackFile: fallbackKey: m:
-        if isFunction m || isAttrs m then
+        if isFunction m then
           unifyModuleSyntax fallbackFile fallbackKey (applyModuleArgsIfFunction fallbackKey m args)
+        else if isAttrs m then
+          if m._type or "module" == "module" then
+            unifyModuleSyntax fallbackFile fallbackKey (applyModuleArgsIfFunction fallbackKey m args)
+          else if m._type == "if" || m._type == "override" then
+            loadModule args fallbackFile fallbackKey { config = m; }
+          else
+            throw "Could not load a value as a module, because it is of type ${lib.strings.escapeNixString m._type}${lib.optionalString (fallbackFile != null) ", in file ${toString fallbackFile}."}"
         else if isList m then
           let defs = [{ file = fallbackFile; value = m; }]; in
           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index 073dc60548603..4da0cb38f685a 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -365,6 +365,10 @@ checkConfigOutput '^{ }$' config.ok.config ./class-check.nix
 checkConfigError 'The module .*/module-class-is-darwin.nix was imported into nixos instead of darwin.' config.fail.config ./class-check.nix
 checkConfigError 'The module foo.nix#darwinModules.default was imported into nixos instead of darwin.' config.fail-anon.config ./class-check.nix
 
+# _type check
+checkConfigError 'Could not load a value as a module, because it is of type "flake", in file .*/module-imports-_type-check.nix' config.ok.config ./module-imports-_type-check.nix
+checkConfigOutput '^true$' "$@" config.enable ./declare-enable.nix ./define-enable-with-top-level-mkIf.nix
+
 # doRename works when `warnings` does not exist.
 checkConfigOutput '^1234$' config.c.d.e ./doRename-basic.nix
 # doRename adds a warning.
diff --git a/lib/tests/modules/define-enable-with-top-level-mkIf.nix b/lib/tests/modules/define-enable-with-top-level-mkIf.nix
new file mode 100644
index 0000000000000..4909c16d82b45
--- /dev/null
+++ b/lib/tests/modules/define-enable-with-top-level-mkIf.nix
@@ -0,0 +1,5 @@
+{ lib, ... }:
+# I think this might occur more realistically in a submodule
+{
+  imports = [ (lib.mkIf true { enable = true; }) ];
+}
diff --git a/lib/tests/modules/module-imports-_type-check.nix b/lib/tests/modules/module-imports-_type-check.nix
new file mode 100644
index 0000000000000..1e29c469daa52
--- /dev/null
+++ b/lib/tests/modules/module-imports-_type-check.nix
@@ -0,0 +1,3 @@
+{
+  imports = [ { _type = "flake"; } ];
+}