From 6382598677548d5b483dce4ab380067cd91af6d2 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Mon, 25 Apr 2022 16:17:21 -0700 Subject: [PATCH 1/2] linkFarm: make last entry win in case of list repeats --- pkgs/build-support/trivial-builders.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix index f29027aa1c68..39c5787e3823 100644 --- a/pkgs/build-support/trivial-builders.nix +++ b/pkgs/build-support/trivial-builders.nix @@ -489,7 +489,8 @@ rec { let entries' = if (lib.isAttrs entries) then entries - else if (lib.isList entries) then lib.listToAttrs (map (x: lib.nameValuePair x.name x.path) entries) + # We do this foldl to have last-wins semantics in case of repeated entries + else if (lib.isList entries) then lib.foldl (a: b: a // { "${b.name}" = b.path; }) { } entries else throw "linkFarm entries must be either attrs or a list!"; linkCommands = lib.mapAttrsToList (name: path: '' From 43bf542ccd8f439964a049f1586689de20bbadb1 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Tue, 15 Nov 2022 11:24:34 -0500 Subject: [PATCH 2/2] tests.trivial-builders.linkFarm: init --- .../trivial-builders/test/link-farm.nix | 45 +++++++++++++++++++ pkgs/test/default.nix | 1 + 2 files changed, 46 insertions(+) create mode 100644 pkgs/build-support/trivial-builders/test/link-farm.nix diff --git a/pkgs/build-support/trivial-builders/test/link-farm.nix b/pkgs/build-support/trivial-builders/test/link-farm.nix new file mode 100644 index 000000000000..1ebfc707632f --- /dev/null +++ b/pkgs/build-support/trivial-builders/test/link-farm.nix @@ -0,0 +1,45 @@ +{ linkFarm, hello, writeTextFile, runCommand }: +let + foo = writeTextFile { + name = "foo"; + text = "foo"; + }; + + linkFarmFromList = linkFarm "linkFarmFromList" [ + { name = "foo"; path = foo; } + { name = "hello"; path = hello; } + ]; + + linkFarmWithRepeats = linkFarm "linkFarmWithRepeats" [ + { name = "foo"; path = foo; } + { name = "hello"; path = hello; } + { name = "foo"; path = hello; } + ]; + + linkFarmFromAttrs = linkFarm "linkFarmFromAttrs" { + inherit foo hello; + }; +in +runCommand "test-linkFarm" { } '' + function assertPathEquals() { + local a b; + a="$(realpath "$1")" + b="$(realpath "$2")" + if [ "$a" != "$b" ]; then + echo "path mismatch!" + echo "a: $1 -> $a" + echo "b: $2 -> $b" + exit 1 + fi + } + + assertPathEquals "${linkFarmFromList}/foo" "${foo}" + assertPathEquals "${linkFarmFromList}/hello" "${hello}" + + assertPathEquals "${linkFarmWithRepeats}/foo" "${hello}" + assertPathEquals "${linkFarmWithRepeats}/hello" "${hello}" + + assertPathEquals "${linkFarmFromAttrs}/foo" "${foo}" + assertPathEquals "${linkFarmFromAttrs}/hello" "${hello}" + touch $out +'' diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index 2638bbc37e6b..ca9e1da9be4b 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -67,6 +67,7 @@ with pkgs; references = callPackage ../build-support/trivial-builders/test/references.nix {}; overriding = callPackage ../build-support/trivial-builders/test-overriding.nix {}; concat = callPackage ../build-support/trivial-builders/test/concat-test.nix {}; + linkFarm = callPackage ../build-support/trivial-builders/test/link-farm.nix {}; }; writers = callPackage ../build-support/writers/test.nix {};