From 788eb078aa78576f395bb43e0c93d5b06cd18d36 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 13 Feb 2025 21:22:58 +0100 Subject: [PATCH] symlinkJoin: ability to strip prefix from tree Allows it to use symlinkJoin for dropin paths like sysctl.d --- .../trivial-builders/default.nix | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkgs/build-support/trivial-builders/default.nix b/pkgs/build-support/trivial-builders/default.nix index be67b7b177ac..c39cd17e4d99 100644 --- a/pkgs/build-support/trivial-builders/default.nix +++ b/pkgs/build-support/trivial-builders/default.nix @@ -3,7 +3,11 @@ let inherit (lib) optionalAttrs + optionalString + hasPrefix warn + map + isList ; in @@ -484,23 +488,36 @@ rec { "symlinkJoin requires either a `name` OR `pname` and `version`"; "${args_.pname}-${args_.version}" , paths + , stripPrefix ? "" , preferLocalBuild ? true , allowSubstitutes ? false , postBuild ? "" + , failOnMissing ? stripPrefix == "" , ... }: + # Ensure no partial paths used, it will be confusing considering + # most of the time symlinkJoin is used with packages. + assert (stripPrefix == "" || (hasPrefix "/" stripPrefix) && stripPrefix != "/"); let - args = removeAttrs args_ [ "name" "postBuild" ] + mapPaths = f: paths: map (path: + if path == null then null + else if isList path then mapPaths f path + else f path + ) paths; + args = removeAttrs args_ [ "name" "postBuild" "stripPrefix" "paths" "failOnMissing" ] // { inherit preferLocalBuild allowSubstitutes; + paths = mapPaths (path: "${path}${stripPrefix}") paths; passAsFile = [ "paths" ]; }; # pass the defaults + ignoreMissing = optionalString (!failOnMissing) "test -d $i && "; + ignoreError = optionalString (!failOnMissing) " || true"; in runCommand name args '' mkdir -p $out for i in $(cat $pathsPath); do - ${lndir}/bin/lndir -silent $i $out + ${ignoreMissing}${lndir}/bin/lndir -silent $i $out${ignoreError} done ${postBuild} '';