summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-07-11 12:18:41 +0200
committerRobert Hensing <robert@roberthensing.nl>2023-07-11 12:22:58 +0200
commit6acc3114c30564d112c2c83836c9eb0685165d9a (patch)
tree8dc00a5646c2d966c721cae016cabafc6a835660
parentc28dd7d9210731257775e8bd8722e27eb260c00e (diff)
downloadnixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.tar
nixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.tar.gz
nixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.tar.bz2
nixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.tar.lz
nixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.tar.xz
nixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.tar.zst
nixpkgs-6acc3114c30564d112c2c83836c9eb0685165d9a.zip
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.
-rw-r--r--lib/modules.nix45
1 files 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: