2018-11-13 15:56:46 +01:00
|
|
|
{ stdenv, lib, coreutils }:
|
2014-05-27 01:00:04 +02:00
|
|
|
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
name = "libredirect-0";
|
|
|
|
|
2018-11-12 10:48:15 +01:00
|
|
|
unpackPhase = ''
|
|
|
|
cp ${./libredirect.c} libredirect.c
|
|
|
|
cp ${./test.c} test.c
|
|
|
|
'';
|
2014-05-27 01:00:04 +02:00
|
|
|
|
2018-11-13 12:26:13 +01:00
|
|
|
libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary;
|
2018-11-12 09:59:39 +01:00
|
|
|
|
2018-11-13 12:35:10 +01:00
|
|
|
outputs = ["out" "hook"];
|
|
|
|
|
2018-11-11 19:29:12 +01:00
|
|
|
buildPhase = ''
|
2018-11-13 15:56:46 +01:00
|
|
|
$CC -Wall -std=c99 -O3 -fPIC -ldl -shared \
|
|
|
|
${lib.optionalString stdenv.isDarwin "-Wl,-install_name,$out/lib/$libName"} \
|
|
|
|
-o "$libName" \
|
|
|
|
libredirect.c
|
2018-11-12 10:48:15 +01:00
|
|
|
|
|
|
|
if [ -n "$doInstallCheck" ]; then
|
|
|
|
$CC -Wall -std=c99 -O3 test.c -o test
|
|
|
|
fi
|
2018-11-11 19:29:12 +01:00
|
|
|
'';
|
2014-05-27 01:00:04 +02:00
|
|
|
|
2018-11-12 09:59:39 +01:00
|
|
|
installPhase = ''
|
2018-11-13 12:26:13 +01:00
|
|
|
install -vD "$libName" "$out/lib/$libName"
|
2018-11-13 12:35:10 +01: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"
|
|
|
|
export DYLD_FORCE_FLAT_NAMESPACE=1
|
|
|
|
'' else ''
|
|
|
|
export LD_PRELOAD="$out/lib/$libName"
|
|
|
|
''}
|
|
|
|
SETUP_HOOK
|
2018-11-12 09:59:39 +01:00
|
|
|
'';
|
2016-08-02 18:06:29 +02:00
|
|
|
|
2018-11-12 10:48:15 +01:00
|
|
|
doInstallCheck = true;
|
|
|
|
|
2018-11-13 12:35:10 +01:00
|
|
|
installCheckPhase = ''
|
|
|
|
(
|
|
|
|
source "$hook/nix-support/setup-hook"
|
|
|
|
NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" ./test
|
|
|
|
)
|
2018-11-12 10:48:15 +01:00
|
|
|
'';
|
|
|
|
|
2016-08-02 18:06:29 +02:00
|
|
|
meta = {
|
2018-11-12 00:09:36 +01:00
|
|
|
platforms = stdenv.lib.platforms.unix;
|
2017-12-14 20:03:35 +01: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 18:06:29 +02:00
|
|
|
};
|
2014-05-27 01:00:04 +02:00
|
|
|
}
|