summary refs log tree commit diff
path: root/lib/modules.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-06-16 21:43:12 +0200
committerRobert Hensing <robert@roberthensing.nl>2023-06-16 22:08:16 +0200
commit36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4 (patch)
treecce1f4b7a5e559b6daec93ad083c6dc27b55e54f /lib/modules.nix
parenta742767bafecf0644585309b744e24f90cdff207 (diff)
downloadnixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.tar
nixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.tar.gz
nixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.tar.bz2
nixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.tar.lz
nixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.tar.xz
nixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.tar.zst
nixpkgs-36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4.zip
lib.modules: Add mergeAttrDefinitionsWithPrio
This will let us make assertions involving _module.args.pkgs, which
is not an option but a value attribute, and therefore doesn't have
its own highestPrio to inspect. The new function gives us that info.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 4dc8c663b2f..c7dcccd7a1d 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -910,6 +910,47 @@ let
     else opt // { type = opt.type.substSubModules opt.options; options = []; };
 
 
+  /*
+    Merge an option's definitions in a way that preserves the priority of the
+    individual attributes in the option value.
+
+    This does not account for all option semantics, such as readOnly.
+
+    Type:
+      option -> attrsOf { highestPrio, value }
+  */
+  mergeAttrDefinitionsWithPrio = opt:
+        let subAttrDefs =
+              lib.concatMap
+                ({ value, ... }@def:
+                  map
+                    (value: def // { inherit value; })
+                    (lib.pushDownProperties value)
+                )
+                opt.definitionsWithLocations;
+            defsByAttr =
+              lib.zipAttrs (
+                lib.concatLists (
+                  lib.concatMap
+                    ({ value, ... }@def:
+                      map
+                        (lib.mapAttrsToList (k: value: { ${k} = def // { inherit value; }; }))
+                        (lib.pushDownProperties value)
+                    )
+                    opt.definitionsWithLocations
+                )
+              );
+        in
+          assert opt.type.name == "attrsOf" || opt.type.name == "lazyAttrsOf";
+          lib.mapAttrs
+                (k: v:
+                  let merging = lib.mergeDefinitions (opt.loc ++ [k]) opt.type.nestedTypes.elemType v;
+                  in {
+                    value = merging.mergedValue;
+                    inherit (merging.defsFinal') highestPrio;
+                  })
+                defsByAttr;
+
   /* Properties. */
 
   mkIf = condition: content:
@@ -1256,6 +1297,7 @@ private //
     importJSON
     importTOML
     mergeDefinitions
+    mergeAttrDefinitionsWithPrio
     mergeOptionDecls  # should be private?
     mkAfter
     mkAliasAndWrapDefinitions