git.iko.soy/typeable/nixpkgs
on master help sign in
NixOS modules: Add error context on module arguments evaluation.
commita6b455bbaecb42679e5d1ade51433d62ee21bd03 authorNicolas B. Pierron <nicolas.b.pierron@gmail.com> dateWed 13 May 2015, 22:44 (12 years ago) parent2fa88f6
5 files changed+37−8
lib/modules.nix+14−5 lib/tests/modules.sh+10−2 lib/tests/modules/define-_module-args-custom.nix+7 lib/tests/modules/custom-arg-define-enable.nix → lib/tests/modules/define-enable-with-custom-arg.nix−1 lib/tests/modules/import-custom-arg.nix+6
lib/modules.nix+14−5
@@ -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
lib/tests/modules.sh+10−2
@@ -12,7 +12,7 @@ evalConfig() {
1212 local attr=$1
1313 shift;
1414 local script="import ./default.nix { modules = [ $@ ];}"
15− nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only
15+ nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace
1616}
1717
1818reportFailure() {
@@ -100,7 +100,15 @@ checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-foo-if-enabl
100100checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-foo-enable-if.nix
101101
102102# Check _module.args.
103−checkConfigOutput "true" config.enable ./declare-enable.nix ./custom-arg-define-enable.nix
103+set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix
104+checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' "$@"
105+checkConfigOutput "true" "$@" ./define-_module-args-custom.nix
106+
107+# Check that using _module.args on imports cause infinite recursions, with
108+# the proper error context.
109+set -- "$@" ./define-_module-args-custom.nix ./import-custom-arg.nix
110+checkConfigError 'while evaluating the module argument .*custom.* in .*import-custom-arg.nix.*:' "$@"
111+checkConfigError 'infinite recursion encountered' "$@"
104112
105113# Check _module.check.
106114set -- config.enable ./declare-enable.nix ./define-enable.nix ./define-loaOfSub-foo.nix
lib/tests/modules/define-_module-args-custom.nixnew file+7
@@ -0,0 +1,7 @@
1+{ lib, ... }:
2+
3+{
4+ config = {
5+ _module.args.custom = true;
6+ };
7+}
lib/tests/modules/custom-arg-define-enable.nix → lib/tests/modules/define-enable-with-custom-arg.nixrenamed−1
@@ -2,7 +2,6 @@
22
33{
44 config = {
5− _module.args.custom = true;
65 enable = custom;
76 };
87}
lib/tests/modules/import-custom-arg.nixnew file+6
@@ -0,0 +1,6 @@
1+{ lib, custom, ... }:
2+
3+{
4+ imports = []
5+ ++ lib.optional custom ./define-enable-force.nix;
6+}