From 413f4666cdd4813f34de17b174c7f93dd573ec41 Mon Sep 17 00:00:00 2001 From: dramforever Date: Sun, 22 Mar 2026 13:49:52 +0800 Subject: [PATCH 1/2] lib/modules: Improve errors involving pushDownProperties If an attrset option was given a definition like: { system = lib.mkIf true false; } Before this change, we get the non-explanatory error message: error: expected a set but found a Boolean: true With the stack trace having nothing to do with the module involved. After this change, we get the better: error: In module `[...]', you're trying to define a value of type `bool' [...] Which names the actual module involved. --- lib/modules.nix | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index c67ea2247ca7..054f18a004f3 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -1320,13 +1320,24 @@ let : 1\. Function argument */ pushDownProperties = + let + mapAttrsIfAttrs = + f: val: + if isAttrs val then + mapAttrs f val + else + # This does not actually work, since arriving here means we have e.g. + # (lib.mkIf cond nonAttrs), while an attrset is expected. However, + # avoiding the mapAttrs call here gives better errors later. + val; + in cfg: if cfg._type or "" == "merge" then concatMap pushDownProperties cfg.contents else if cfg._type or "" == "if" then - map (mapAttrs (n: v: mkIf cfg.condition v)) (pushDownProperties cfg.content) + map (mapAttrsIfAttrs (n: v: mkIf cfg.condition v)) (pushDownProperties cfg.content) else if cfg._type or "" == "override" then - map (mapAttrs (n: v: mkOverride cfg.priority v)) (pushDownProperties cfg.content) + map (mapAttrsIfAttrs (n: v: mkOverride cfg.priority v)) (pushDownProperties cfg.content) # FIXME: handle mkOrder? else [ cfg ]; From d48a370de11ccfd407d12ee3f15ac6e7ceedc5cc Mon Sep 17 00:00:00 2001 From: dramforever Date: Sun, 22 Mar 2026 14:11:01 +0800 Subject: [PATCH 2/2] lib/modules: Add error message test for pushing down non-attrsets --- lib/tests/modules.sh | 2 ++ lib/tests/modules/test-push-down-non-attrs.nix | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 lib/tests/modules/test-push-down-non-attrs.nix diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index aa9d3443fb6a..bf309a604f26 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -171,6 +171,8 @@ checkConfigError() { # Shorthand meta attribute does not duplicate the config checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix +checkConfigError "In module .*test-push-down-non-attrs.nix., you're trying to define a value of type \`bool'\n\s*rather than an attribute set for the option" config ./test-push-down-non-attrs.nix + checkConfigOutput '^true$' config.result ./test-mergeAttrDefinitionsWithPrio.nix # Check that a module argument is passed, also when a default is available diff --git a/lib/tests/modules/test-push-down-non-attrs.nix b/lib/tests/modules/test-push-down-non-attrs.nix new file mode 100644 index 000000000000..ce9222ca3cfd --- /dev/null +++ b/lib/tests/modules/test-push-down-non-attrs.nix @@ -0,0 +1,5 @@ +{ lib, ... }: + +{ + config = lib.mkIf true true; +}