diff options
-rw-r--r-- | lib/attrsets.nix | 11 | ||||
-rw-r--r-- | lib/tests/misc.nix | 14 |
2 files changed, 21 insertions, 4 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 1e4142562fa69..2a1b866dbc5e1 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -435,12 +435,15 @@ rec { useful for deep-overriding. Example: - x = { a = { b = 4; c = 3; }; } - overrideExisting x { a = { b = 6; d = 2; }; } - => { a = { b = 6; d = 2; }; } + overrideExisting {} { a = 1; } + => {} + overrideExisting { b = 2; } { a = 1; } + => { b = 2; } + overrideExisting { a = 3; b = 2; } { a = 1; } + => { a = 1; b = 2; } */ overrideExisting = old: new: - old // listToAttrs (map (attr: nameValuePair attr (attrByPath [attr] old.${attr} new)) (attrNames old)); + mapAttrs (name: value: new.${name} or value) old; /* Get a package output. If no output is found, fallback to `.out` and then to the default. diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index d3bd7746d89c2..d89bcfde48196 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -236,6 +236,20 @@ runTests { }; }; + testOverrideExistingEmpty = { + expr = overrideExisting {} { a = 1; }; + expected = {}; + }; + + testOverrideExistingDisjoint = { + expr = overrideExisting { b = 2; } { a = 1; }; + expected = { b = 2; }; + }; + + testOverrideExistingOverride = { + expr = overrideExisting { a = 3; b = 2; } { a = 1; }; + expected = { a = 1; b = 2; }; + }; # GENERATORS # these tests assume attributes are converted to lists |