symlinkJoin: ability to strip prefix from tree

Allows it to use symlinkJoin for dropin paths like sysctl.d
This commit is contained in:
Yaroslav Bolyukin
2025-02-13 21:22:58 +01:00
parent 2ee154072b
commit 788eb078aa
@@ -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}
'';