@@ -91,9 +91,11 @@ rec {
9191 let
9292 toClosureList = file: parentKey: imap (n: x:
9393 if isAttrs x || isFunction x then
94− unifyModuleSyntax file "${parentKey}:anon-${toString n}" (unpackSubmodule applyIfFunction x args)
94+ let key = "${parentKey}:anon-${toString n}"; in
95+ unifyModuleSyntax file key (unpackSubmodule (applyIfFunction key) x args)
9596 else
96− unifyModuleSyntax (toString x) (toString x) (applyIfFunction (import x) args));
97+ let file = toString x; key = toString x; in
98+ unifyModuleSyntax file key (applyIfFunction key (import x) args));
9799 in
98100 builtins.genericClosure {
99101 startSet = toClosureList unknownModule "" modules;
@@ -122,7 +124,7 @@ rec {
122124 config = removeAttrs m ["key" "_file" "require" "imports"];
123125 };
124126
125− applyIfFunction = f: arg@{ config, options, lib, ... }: if isFunction f then
127+ applyIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then
126128 let
127129 # Module arguments are resolved in a strict manner when attribute set
128130 # deconstruction is used. As the arguments are now defined with the
@@ -137,11 +139,18 @@ rec {
137139 # not their values. The values are forwarding the result of the
138140 # evaluation of the option.
139141 requiredArgs = builtins.attrNames (builtins.functionArgs f);
142+ context = name: ''while evaluating the module argument `${name}' in "${key}":'';
140143 extraArgs = builtins.listToAttrs (map (name: {
141144 inherit name;
142− value = config._module.args.${name};
145+ value = addErrorContext (context name)
146+ (args.${name} or config._module.args.${name});
143147 }) requiredArgs);
144− in f (extraArgs // arg)
148+
149+ # Note: we append in the opposite order such that we can add an error
150+ # context on the explicited arguments of "args" too. This update
151+ # operator is used to make the "args@{ ... }: with args.lib;" notation
152+ # works.
153+ in f (args // extraArgs)
145154 else
146155 f;
147156