2022-01-07 23:32:21 +00:00
|
|
|
{ lib, stdenv, bintools-unwrapped, llvmPackages_13, coreutils }:
|
2014-05-26 23:00:04 +00:00
|
|
|
|
2022-01-08 14:14:35 +00:00
|
|
|
if stdenv.hostPlatform.isStatic
|
|
|
|
then throw ''
|
|
|
|
libredirect is not available on static builds.
|
|
|
|
|
|
|
|
Please fix your derivation to not depend on libredirect on static
|
|
|
|
builds, using something like following:
|
|
|
|
|
|
|
|
nativeBuildInputs =
|
|
|
|
lib.optional (!stdenv.buildPlatform.isStatic) libredirect;
|
|
|
|
|
|
|
|
and disable tests as necessary, although fixing tests to work without
|
|
|
|
libredirect is even better.
|
|
|
|
|
|
|
|
libredirect uses LD_PRELOAD feature of dynamic loader and does not
|
|
|
|
work on static builds where dynamic loader is not used.
|
|
|
|
''
|
|
|
|
else stdenv.mkDerivation rec {
|
2021-06-20 10:34:30 +00:00
|
|
|
pname = "libredirect";
|
|
|
|
version = "0";
|
2014-05-26 23:00:04 +00:00
|
|
|
|
2018-11-12 09:48:15 +00:00
|
|
|
unpackPhase = ''
|
|
|
|
cp ${./libredirect.c} libredirect.c
|
|
|
|
cp ${./test.c} test.c
|
|
|
|
'';
|
2014-05-26 23:00:04 +00:00
|
|
|
|
2018-11-13 11:35:10 +00:00
|
|
|
outputs = ["out" "hook"];
|
|
|
|
|
2022-01-07 23:32:21 +00:00
|
|
|
libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary;
|
|
|
|
|
2018-11-11 18:29:12 +00:00
|
|
|
buildPhase = ''
|
2021-05-23 14:59:43 +00:00
|
|
|
runHook preBuild
|
|
|
|
|
2022-01-07 23:32:21 +00:00
|
|
|
${if stdenv.isDarwin && stdenv.isAarch64 then ''
|
|
|
|
# We need the unwrapped binutils and clang:
|
|
|
|
# We also want to build a fat library with x86_64, arm64, arm64e in there.
|
|
|
|
# Because we use the unwrapped tools, we need to provide -isystem for headers
|
|
|
|
# and the library search directory for libdl.
|
|
|
|
# We can't build this on x86_64, because the libSystem we point to doesn't
|
|
|
|
# like arm64(e).
|
|
|
|
PATH=${bintools-unwrapped}/bin:${llvmPackages_13.clang-unwrapped}/bin:$PATH \
|
|
|
|
clang -arch x86_64 -arch arm64 -arch arm64e \
|
|
|
|
-isystem ${llvmPackages_13.clang.libc}/include \
|
|
|
|
-isystem ${llvmPackages_13.libclang.lib}/lib/clang/*/include \
|
|
|
|
-L${llvmPackages_13.clang.libc}/lib \
|
2022-01-24 11:20:51 +00:00
|
|
|
-Wl,-install_name,$libName \
|
2022-01-07 23:32:21 +00:00
|
|
|
-Wall -std=c99 -O3 -fPIC libredirect.c \
|
|
|
|
-ldl -shared -o "$libName"
|
|
|
|
'' else if stdenv.isDarwin then ''
|
|
|
|
$CC -Wall -std=c99 -O3 -fPIC libredirect.c \
|
|
|
|
-Wl,-install_name,$out/lib/$libName \
|
|
|
|
-ldl -shared -o "$libName"
|
|
|
|
'' else ''
|
|
|
|
$CC -Wall -std=c99 -O3 -fPIC libredirect.c \
|
|
|
|
-ldl -shared -o "$libName"
|
|
|
|
''}
|
2018-11-12 09:48:15 +00:00
|
|
|
|
|
|
|
if [ -n "$doInstallCheck" ]; then
|
2022-01-16 21:41:41 +00:00
|
|
|
$CC -Wall -std=c99 \
|
|
|
|
${lib.optionalString (!stdenv.isDarwin) "-D_GNU_SOURCE"} \
|
|
|
|
-O3 test.c -o test
|
2018-11-12 09:48:15 +00:00
|
|
|
fi
|
2021-05-23 14:59:43 +00:00
|
|
|
|
|
|
|
runHook postBuild
|
2018-11-11 18:29:12 +00:00
|
|
|
'';
|
2014-05-26 23:00:04 +00:00
|
|
|
|
2021-05-23 14:25:52 +00:00
|
|
|
# We want to retain debugging info to be able to use GDB on libredirect.so
|
|
|
|
# to more easily investigate which function overrides are missing or why
|
|
|
|
# existing ones do not have the intended effect.
|
|
|
|
dontStrip = true;
|
|
|
|
|
2018-11-12 08:59:39 +00:00
|
|
|
installPhase = ''
|
2021-05-23 14:59:43 +00:00
|
|
|
runHook preInstall
|
|
|
|
|
2018-11-13 11:26:13 +00:00
|
|
|
install -vD "$libName" "$out/lib/$libName"
|
2018-11-13 11:35:10 +00:00
|
|
|
|
2022-01-24 11:20:51 +00:00
|
|
|
'' + lib.optionalString (stdenv.isDarwin && stdenv.isAarch64) ''
|
|
|
|
# dylib will be rejected unless dylib rpath gets explictly set
|
|
|
|
install_name_tool \
|
|
|
|
-change $libName $out/lib/$libName \
|
|
|
|
$out/lib/$libName
|
|
|
|
'' + ''
|
2021-11-04 20:04:08 +00:00
|
|
|
# Provide a setup hook that injects our library into every process.
|
2018-11-13 11:35:10 +00:00
|
|
|
mkdir -p "$hook/nix-support"
|
|
|
|
cat <<SETUP_HOOK > "$hook/nix-support/setup-hook"
|
|
|
|
${if stdenv.isDarwin then ''
|
|
|
|
export DYLD_INSERT_LIBRARIES="$out/lib/$libName"
|
|
|
|
'' else ''
|
|
|
|
export LD_PRELOAD="$out/lib/$libName"
|
|
|
|
''}
|
|
|
|
SETUP_HOOK
|
2021-05-23 14:59:43 +00:00
|
|
|
|
|
|
|
runHook postInstall
|
2018-11-12 08:59:39 +00:00
|
|
|
'';
|
2016-08-02 16:06:29 +00:00
|
|
|
|
2018-11-12 09:48:15 +00:00
|
|
|
doInstallCheck = true;
|
|
|
|
|
2018-11-13 11:35:10 +00:00
|
|
|
installCheckPhase = ''
|
|
|
|
(
|
|
|
|
source "$hook/nix-support/setup-hook"
|
2022-01-16 15:42:07 +00:00
|
|
|
NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true:/bar/baz=$(mktemp -d)" ./test
|
2018-11-13 11:35:10 +00:00
|
|
|
)
|
2018-11-12 09:48:15 +00:00
|
|
|
'';
|
|
|
|
|
2021-01-24 00:40:18 +00:00
|
|
|
meta = with lib; {
|
|
|
|
platforms = platforms.unix;
|
2017-12-14 19:03:35 +00:00
|
|
|
description = "An LD_PRELOAD library to intercept and rewrite the paths in glibc calls";
|
|
|
|
longDescription = ''
|
|
|
|
libredirect is an LD_PRELOAD library to intercept and rewrite the paths in
|
|
|
|
glibc calls based on the value of $NIX_REDIRECTS, a colon-separated list
|
|
|
|
of path prefixes to be rewritten, e.g. "/src=/dst:/usr/=/nix/store/".
|
|
|
|
'';
|
2016-08-02 16:06:29 +00:00
|
|
|
};
|
2014-05-26 23:00:04 +00:00
|
|
|
}
|