about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-02-07 21:13:06 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-05-06 18:29:04 +0200
commit9714487f743d0ac9aec50a5513cb89a97932d4a6 (patch)
treefd0052da89a5b18aee9db5e9f46dcc7e99676d0e /lib
parent2e689d58cbbe6b7047bb132dc79097016e606dd0 (diff)
downloadnixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar
nixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.gz
nixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.bz2
nixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.lz
nixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.xz
nixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.zst
nixlib-9714487f743d0ac9aec50a5513cb89a97932d4a6.zip
lib/modules: Explain that a configuration can't be loaded as a module
Diffstat (limited to 'lib')
-rw-r--r--lib/modules.nix7
-rwxr-xr-xlib/tests/modules.sh2
2 files changed, 7 insertions, 2 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 3bcd8d280b74..cb8f5cd198e6 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -371,7 +371,12 @@ let
           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}."}"
+            throw (
+              "Could not load a value as a module, because it is of type ${lib.strings.escapeNixString m._type}"
+              + lib.optionalString (fallbackFile != unknownModule) ", in file ${toString fallbackFile}."
+              + lib.optionalString (m._type == "configuration") " If you do intend to import this configuration, please only import the modules that make up the configuration. You may have to create a `let` binding, file or attribute to give yourself access to the relevant modules.\nWhile loading a configuration into the module system is a very sensible idea, it can not be done cleanly in practice."
+               # Extended explanation: That's because a finalized configuration is more than just a set of modules. For instance, it has its own `specialArgs` that, by the nature of `specialArgs` can't be loaded through `imports` or the the `modules` argument. So instead, we have to ask you to extract the relevant modules and use those instead. This way, we keep the module system comparatively simple, and hopefully avoid a bad surprise down the line.
+            )
         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 d12e503c4b1d..c2a8e566cb8c 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -368,7 +368,7 @@ checkConfigError 'The module foo.nix#darwinModules.default was imported into 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
-checkConfigError 'Could not load a value as a module, because it is of type "configuration", in file .*/import-configuration.nix' config ./import-configuration.nix
+checkConfigError 'Could not load a value as a module, because it is of type "configuration", in file .*/import-configuration.nix.*please only import the modules that make up the configuration.*' config ./import-configuration.nix
 
 # doRename works when `warnings` does not exist.
 checkConfigOutput '^1234$' config.c.d.e ./doRename-basic.nix