summary refs log tree commit diff
path: root/lib/modules.nix
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/modules.nix
parent2e689d58cbbe6b7047bb132dc79097016e606dd0 (diff)
downloadnixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar
nixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.gz
nixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.bz2
nixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.lz
nixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.xz
nixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.tar.zst
nixpkgs-9714487f743d0ac9aec50a5513cb89a97932d4a6.zip
lib/modules: Explain that a configuration can't be loaded as a module
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix7
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 3bcd8d280b7..cb8f5cd198e 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}"