aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/pkgs/development/compilers/nim/nixbuild.patch
blob: bdfd04744e16db27d2a5f40a4c45e4dc74e67c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
commit 164ba50fc74b980f77047080b2ae1ea099ae9b27
Author: Emery Hemingway <ehmry@posteo.net>
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`