From 06ac3c1d2a4d0f9476baa660574110f64b6dbf72 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Mon, 17 Aug 2020 16:37:29 +0200 Subject: [PATCH] nim: patch to fix dynamic library loading --- pkgs/applications/misc/nimmm/default.nix | 2 - .../science/biology/mosdepth/default.nix | 4 +- pkgs/development/compilers/nim/default.nix | 4 ++ pkgs/development/compilers/nim/nixbuild.patch | 51 +++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 pkgs/development/compilers/nim/nixbuild.patch diff --git a/pkgs/applications/misc/nimmm/default.nix b/pkgs/applications/misc/nimmm/default.nix index 16400d76f64..383057ca655 100644 --- a/pkgs/applications/misc/nimmm/default.nix +++ b/pkgs/applications/misc/nimmm/default.nix @@ -36,8 +36,6 @@ in stdenv.mkDerivation rec { nativeBuildInputs = [ nim ]; buildInputs = [ termbox pcre ]; - NIX_LDFLAGS = "-lpcre"; - buildPhase = '' export HOME=$TMPDIR; nim -p:${noise} -p:${nimbox} -p:${lscolors}/src c -d:release src/nimmm.nim diff --git a/pkgs/applications/science/biology/mosdepth/default.nix b/pkgs/applications/science/biology/mosdepth/default.nix index 21af5b82207..96aed307f0b 100644 --- a/pkgs/applications/science/biology/mosdepth/default.nix +++ b/pkgs/applications/science/biology/mosdepth/default.nix @@ -26,14 +26,14 @@ in stdenv.mkDerivation rec { sha256 = "01gm9gj2x2zs4yx6wk761fi1papi7qr3gp4ln1kkn8n2f9y9h849"; }; - buildInputs = [ nim ]; + buildInputs = [ nim htslib pcre ]; buildPhase = '' HOME=$TMPDIR nim -p:${hts-nim}/src -p:${docopt}/src c --nilseqs:on -d:release mosdepth.nim ''; + installPhase = "install -Dt $out/bin mosdepth"; - fixupPhase = "patchelf --set-rpath ${stdenv.lib.makeLibraryPath [ stdenv.cc.cc htslib pcre ]} $out/bin/mosdepth"; meta = with stdenv.lib; { description = "fast BAM/CRAM depth calculation for WGS, exome, or targeted sequencing."; diff --git a/pkgs/development/compilers/nim/default.nix b/pkgs/development/compilers/nim/default.nix index 543a6120577..d497cc76e9b 100644 --- a/pkgs/development/compilers/nim/default.nix +++ b/pkgs/development/compilers/nim/default.nix @@ -28,6 +28,10 @@ stdenv.mkDerivation rec { openssl pcre readline boehmgc sfml sqlite ]; + patches = [ ./nixbuild.patch ]; + + postPatch = "echo define:nixbuild >> config/nim.cfg"; + buildPhase = '' runHook preBuild diff --git a/pkgs/development/compilers/nim/nixbuild.patch b/pkgs/development/compilers/nim/nixbuild.patch new file mode 100644 index 00000000000..bdfd04744e1 --- /dev/null +++ b/pkgs/development/compilers/nim/nixbuild.patch @@ -0,0 +1,51 @@ +commit 164ba50fc74b980f77047080b2ae1ea099ae9b27 +Author: Emery Hemingway +Date: Mon Sep 7 14:09:22 2020 +0200 + + Load libaries by absolute path on NixOS + + If "nixbuild" is defined then choose dynamic runtime libraries by + searching $NIX_LDFLAGS at compile-time. + + Fix #15194 + +diff --git a/lib/pure/dynlib.nim b/lib/pure/dynlib.nim +index f31ae94dd..debed9c07 100644 +--- a/lib/pure/dynlib.nim ++++ b/lib/pure/dynlib.nim +@@ -56,6 +56,9 @@ + + import strutils + ++when defined(nixbuild): ++ import os ++ + type + LibHandle* = pointer ## a handle to a dynamically loaded library + +@@ -95,6 +98,25 @@ proc libCandidates*(s: string, dest: var seq[string]) = + libCandidates(prefix & middle & suffix, dest) + else: + add(dest, s) ++ when defined(nixbuild): ++ # Nix doesn't have a global library directory so ++ # load libraries using an absolute path if one ++ # can be derived from NIX_LDFLAGS. ++ # ++ # During Nix/NixOS packaging the line "define:nixbuild" ++ # should be appended to the ../../config/nim.cfg file ++ # to enable this behavior by default. ++ # ++ var libDirs = split(getEnv("LD_LIBRARY_PATH"), ':') ++ for flag in split(getEnv("NIX_LDFLAGS")): ++ if flag.startsWith("-L"): ++ libDirs.add(flag[2..flag.high]) ++ for lib in dest: ++ for dir in libDirs: ++ let abs = dir / lib ++ if existsFile(abs): ++ dest = @[abs] ++ return + + proc loadLibPattern*(pattern: string, globalSymbols = false): LibHandle = + ## loads a library with name matching `pattern`, similar to what `dlimport`