From e62db105c4bc1837cff46702d760c3e04403b6b5 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 13 Nov 2018 12:26:13 +0100 Subject: [PATCH 1/3] libredirect: specify libName reduces a bit of duplication and can also be used from the outside: export LD_PRELOAD=${libredirect}/lib/${libredirect.libName} --- pkgs/build-support/libredirect/default.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkgs/build-support/libredirect/default.nix b/pkgs/build-support/libredirect/default.nix index 09a3da6729a..e92945a4030 100644 --- a/pkgs/build-support/libredirect/default.nix +++ b/pkgs/build-support/libredirect/default.nix @@ -8,11 +8,11 @@ stdenv.mkDerivation { cp ${./test.c} test.c ''; - shlibext = stdenv.targetPlatform.extensions.sharedLibrary; + libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary; buildPhase = '' $CC -Wall -std=c99 -O3 -shared libredirect.c \ - -o "libredirect$shlibext" -fPIC -ldl + -o "$libName" -fPIC -ldl if [ -n "$doInstallCheck" ]; then $CC -Wall -std=c99 -O3 test.c -o test @@ -20,18 +20,18 @@ stdenv.mkDerivation { ''; installPhase = '' - install -vD "libredirect$shlibext" "$out/lib/libredirect$shlibext" + install -vD "$libName" "$out/lib/$libName" ''; doInstallCheck = true; installCheckPhase = if stdenv.isDarwin then '' NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \ - DYLD_INSERT_LIBRARIES="$out/lib/libredirect$shlibext" \ + DYLD_INSERT_LIBRARIES="$out/lib/$libName" \ DYLD_FORCE_FLAT_NAMESPACE=1 ./test '' else '' NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \ - LD_PRELOAD="$out/lib/libredirect$shlibext" ./test + LD_PRELOAD="$out/lib/$libName" ./test ''; meta = { From 91c130e2f5d6702e195dd6c25abafc5f16b7e505 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 13 Nov 2018 12:35:10 +0100 Subject: [PATCH 2/3] libredirect: introduce optional setup-hook This allows to simplify the usage of libredirect inside of nix build sandboxes. Add "libredirect.hook" to the build inputs to get everything linked in automaticall. All that's left is to set NIX_REDIRECTS and call the target program. --- pkgs/build-support/libredirect/default.nix | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pkgs/build-support/libredirect/default.nix b/pkgs/build-support/libredirect/default.nix index e92945a4030..7caca2fc003 100644 --- a/pkgs/build-support/libredirect/default.nix +++ b/pkgs/build-support/libredirect/default.nix @@ -10,6 +10,8 @@ stdenv.mkDerivation { libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary; + outputs = ["out" "hook"]; + buildPhase = '' $CC -Wall -std=c99 -O3 -shared libredirect.c \ -o "$libName" -fPIC -ldl @@ -21,17 +23,25 @@ stdenv.mkDerivation { installPhase = '' install -vD "$libName" "$out/lib/$libName" + + mkdir -p "$hook/nix-support" + cat < "$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 ''; doInstallCheck = true; - installCheckPhase = if stdenv.isDarwin then '' - NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \ - DYLD_INSERT_LIBRARIES="$out/lib/$libName" \ - DYLD_FORCE_FLAT_NAMESPACE=1 ./test - '' else '' - NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \ - LD_PRELOAD="$out/lib/$libName" ./test + installCheckPhase = '' + ( + source "$hook/nix-support/setup-hook" + NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" ./test + ) ''; meta = { From d04a1265a118b0348511549af659c6cbb9b5315d Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 13 Nov 2018 15:56:46 +0100 Subject: [PATCH 3/3] libredirect: set install_name on Darwin fixes https://github.com/NixOS/nixpkgs/pull/50246#issuecomment-437975038 --- pkgs/build-support/libredirect/default.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/libredirect/default.nix b/pkgs/build-support/libredirect/default.nix index 7caca2fc003..6e54e2a696c 100644 --- a/pkgs/build-support/libredirect/default.nix +++ b/pkgs/build-support/libredirect/default.nix @@ -1,4 +1,4 @@ -{ stdenv, coreutils }: +{ stdenv, lib, coreutils }: stdenv.mkDerivation { name = "libredirect-0"; @@ -13,8 +13,10 @@ stdenv.mkDerivation { outputs = ["out" "hook"]; buildPhase = '' - $CC -Wall -std=c99 -O3 -shared libredirect.c \ - -o "$libName" -fPIC -ldl + $CC -Wall -std=c99 -O3 -fPIC -ldl -shared \ + ${lib.optionalString stdenv.isDarwin "-Wl,-install_name,$out/lib/$libName"} \ + -o "$libName" \ + libredirect.c if [ -n "$doInstallCheck" ]; then $CC -Wall -std=c99 -O3 test.c -o test