Files
nixpkgs/pkgs/build-support/setup-hooks/make-symlinks-relative.sh
DavHau c66b12ff90 make-symlinks-relative.sh: optimize - use multiple cores
This optimizes the performance of the hook on machines with multiple cores.

The previous implementation was slow, as it was launching two extra processes per symlink (readlink and ln) while not making use of multiprocessing.

The following improvements were made:
- don't even execute the hook if dontRewriteSymlinks is set
- replace multiple find calls with single find call over all outputs
- use xargs -P to process symlinks in parallel
- add test to check that the hook operates as expected
2025-12-03 11:39:52 +07:00

36 lines
1.0 KiB
Bash

# symlinks are often created in postFixup
# don't use fixupOutputHooks, it is before postFixup
if [[ -z "${dontRewriteSymlinks-}" ]]; then
postFixupHooks+=(_makeSymlinksRelative)
fi
# For every symlink in $output that refers to another file in $output
# ensure that the symlink is relative.
# This increases the chance that NAR files can be deduplicated.
_makeSymlinksRelative() {
local prefixes
prefixes=()
for output in $(getAllOutputNames); do
[ ! -e "${!output}" ] && continue
prefixes+=( "${!output}" )
done
find "${prefixes[@]}" -type l -printf '%H\0%p\0' \
| xargs -0 -n2 -r -P "$NIX_BUILD_CORES" sh -c '
output="$1"
link="$2"
linkTarget=$(readlink "$link")
# only touch links that point inside the same output tree
[[ $linkTarget == "$output"/* ]] || exit 0
if [ ! -e "$linkTarget" ]; then
echo "the symlink $link is broken, it points to $linkTarget (which is missing)"
fi
echo "making symlink relative: $link"
ln -snrf "$linkTarget" "$link"
' _
}