summary refs log tree commit diff
path: root/lib/modules.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-07-11 11:37:56 +0200
committerRobert Hensing <robert@roberthensing.nl>2023-07-11 11:37:56 +0200
commitd1e18a369a6c89d150a503c599d11674fa18a581 (patch)
tree7eb1181fb42c49c543320b8299ee230bccbbe510 /lib/modules.nix
parent63b561d468365379a781c04d14d938b2f9239fe0 (diff)
downloadnixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.tar
nixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.tar.gz
nixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.tar.bz2
nixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.tar.lz
nixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.tar.xz
nixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.tar.zst
nixpkgs-d1e18a369a6c89d150a503c599d11674fa18a581.zip
lib/modules.nix: Inline byName
byName is not an abstraction. This is the first commit in a series
that refactors it away.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix70
1 files changed, 43 insertions, 27 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 0bedd28e877..730d30576f1 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -539,28 +539,8 @@ let
 
   mergeModules' = prefix: options: configs:
     let
-     /* byName is like foldAttrs, but will look for attributes to merge in the
-        specified attribute name.
-
-        byName "foo" (module: value: ["module.hidden=${module.hidden},value=${value}"])
-        [
-          {
-            hidden="baz";
-            foo={qux="bar"; gla="flop";};
-          }
-          {
-            hidden="fli";
-            foo={qux="gne"; gli="flip";};
-          }
-        ]
-        ===>
-        {
-          gla = [ "module.hidden=baz,value=flop" ];
-          gli = [ "module.hidden=fli,value=flip" ];
-          qux = [ "module.hidden=baz,value=bar" "module.hidden=fli,value=gne" ];
-        }
-      */
-      byName = attr: f: modules:
+      # an attrset 'name' => list of submodules that declare ‘name’.
+      declsByName = (attr: f: modules:
         zipAttrsWith (n: concatLists)
           (map (module: let subtree = module.${attr}; in
               if !(builtins.isAttrs subtree) then
@@ -579,17 +559,53 @@ let
                 '')
               else
                 mapAttrs (n: f module) subtree
-              ) modules);
-      # an attrset 'name' => list of submodules that declare ‘name’.
-      declsByName = byName "options" (module: option:
+              ) modules)) "options" (module: option:
           [{ inherit (module) _file; options = option; }]
         ) options;
       # an attrset 'name' => list of submodules that define ‘name’.
-      defnsByName = byName "config" (module: value:
+      defnsByName = (attr: f: modules:
+        zipAttrsWith (n: concatLists)
+          (map (module: let subtree = module.${attr}; in
+              if !(builtins.isAttrs subtree) then
+                throw (if attr == "config" then ''
+                  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 ''
+                  An option declaration for `${builtins.concatStringsSep "." prefix}' has type
+                  `${builtins.typeOf subtree}' rather than an attribute set.
+                  Did you mean to define this outside of `options'?
+                '')
+              else
+                mapAttrs (n: f module) subtree
+              ) modules)) "config" (module: value:
           map (config: { inherit (module) file; inherit config; }) (pushDownProperties value)
         ) configs;
       # extract the definitions for each loc
-      defnsByName' = byName "config" (module: value:
+      defnsByName' = (attr: f: modules:
+        zipAttrsWith (n: concatLists)
+          (map (module: let subtree = module.${attr}; in
+              if !(builtins.isAttrs subtree) then
+                throw (if attr == "config" then ''
+                  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 ''
+                  An option declaration for `${builtins.concatStringsSep "." prefix}' has type
+                  `${builtins.typeOf subtree}' rather than an attribute set.
+                  Did you mean to define this outside of `options'?
+                '')
+              else
+                mapAttrs (n: f module) subtree
+              ) modules)) "config" (module: value:
           [{ inherit (module) file; inherit value; }]
         ) configs;