From 6acc3114c30564d112c2c83836c9eb0685165d9a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 11 Jul 2023 12:18:41 +0200 Subject: [PATCH] lib/modules.nix: Make entire definition list strict in config check This is a non-trivial refactor that slightly changes the semantics of the internal definition lists. Whereas previously only individual list items would trigger the exception, now the error is promoted to the whole list. This is mostly ok, because we compute the value, it is wrong to ignore a definition. However, we don't always compute the value. For instance `readOnly` only needs to count definitions. That won't be possible anymore when the error is raised for one of the items. As a consequence, an error will be raised for the errant definition instead of the number of definitions. --- lib/modules.nix | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index 18d3f23b1c1..5ae8bd1a4f7 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -556,48 +556,45 @@ let ) subtree ) options); + + # The root of any module definition must be an attrset. + checkedConfigs = + assert + lib.all + (c: + isAttrs c.config || throw '' + You're trying to define a value of type `${builtins.typeOf c.config}' + rather than an attribute set for the option + `${builtins.concatStringsSep "." prefix}'! + + This usually happens if `${builtins.concatStringsSep "." prefix}' has option + definitions inside that are not matched. Please check how to properly define + this option by e.g. referring to `man 5 configuration.nix'! + '' + ) + configs; + configs; + # an attrset 'name' => list of submodules that define ‘name’. pushedDownDefinitionsByName = zipAttrsWith (n: concatLists) (map (module: let subtree = module.config; in - if !(builtins.isAttrs subtree) then - throw '' - You're trying to define a value of type `${builtins.typeOf subtree}' - rather than an attribute set for the option - `${builtins.concatStringsSep "." prefix}'! - - This usually happens if `${builtins.concatStringsSep "." prefix}' has option - definitions inside that are not matched. Please check how to properly define - this option by e.g. referring to `man 5 configuration.nix'! - '' - else mapAttrs (n: value: map (config: { inherit (module) file; inherit config; }) (pushDownProperties value) ) subtree - ) configs); + ) checkedConfigs); # extract the definitions for each loc rawDefinitionsByName = zipAttrsWith (n: concatLists) (map (module: let subtree = module.config; in - if !(builtins.isAttrs subtree) then - throw '' - You're trying to define a value of type `${builtins.typeOf subtree}' - rather than an attribute set for the option - `${builtins.concatStringsSep "." prefix}'! - - This usually happens if `${builtins.concatStringsSep "." prefix}' has option - definitions inside that are not matched. Please check how to properly define - this option by e.g. referring to `man 5 configuration.nix'! - '' - else mapAttrs (n: value: [{ inherit (module) file; inherit value; }] ) subtree - ) configs); + ) checkedConfigs); # Convert an option tree decl to a submodule option decl optionTreeToOption = decl: