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.
This commit is contained in:
dramforever
2026-03-22 13:49:52 +08:00
parent 82648ce63b
commit 413f4666cd
+13 -2
View File
@@ -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 ];