Skip to content

Commit

Permalink
lib/modules.nix: Make entire definition list strict in config check
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
roberth committed Jul 11, 2023
1 parent c28dd7d commit 6acc311
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6acc311

Please sign in to comment.