From d7d489674f3bd4ec97bf3837953f4bd49db2e5af Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Wed, 18 Sep 2024 20:47:47 +0200 Subject: [PATCH] [Lib/Attrsets]: add support for merging with priority overrides --- lib/attrsets.nix | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index c46e853..b0fe540 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -1,7 +1,28 @@ {lib, myLib, ...}: with lib; rec { - mergeAttrsRecursive = a: b: foldAttrs (item: acc: + getPriority = x: + if isAttrs x && (attrByPath [ "_type" ] "" x) == "override" then + getAttr "priority" x + else + lib.modules.defaultOverridePriority + ; + comparePriority = a: b: (getPriority a) - (getPriority b); + mergeAttrsRecursiveWithPriority = a: b: + let + _prio = comparePriority a b; + in + if _prio == 0 then + _mergeAttrsRecursive mergeAttrsRecursiveWithPriority a b + else if _prio > 0 then + a + else + b + ; + + mergeAttrsRecursive = a: b: _mergeAttrsRecursive _mergeAttrsRecursive a b; + + _mergeAttrsRecursive = self: a: b: foldAttrs (item: acc: if (isNull acc) then item else if (isList item) then @@ -11,7 +32,7 @@ rec { else if (isString item) then acc + item else if (isAttrs item) then - mergeAttrsRecursive acc item + self acc item else item ) null [ b a ]; }