From 36ea2bbfe81ccf1118fd0ef66b13868fa3cc27e4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 16 Jun 2023 21:43:12 +0200 Subject: 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. --- lib/modules.nix | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'lib/modules.nix') 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 -- cgit 1.4.1