1commit 164ba50fc74b980f77047080b2ae1ea099ae9b27
2Author: Emery Hemingway <ehmry@posteo.net>
3Date: Mon Sep 7 14:09:22 2020 +0200
4
5 Load libaries by absolute path on NixOS
6
7 If "nixbuild" is defined then choose dynamic runtime libraries by
8 searching $NIX_LDFLAGS at compile-time.
9
10 Fix #15194
11
12diff --git a/lib/pure/dynlib.nim b/lib/pure/dynlib.nim
13index f31ae94dd..debed9c07 100644
14--- a/lib/pure/dynlib.nim
15+++ b/lib/pure/dynlib.nim
16@@ -56,6 +56,9 @@
17
18 import strutils
19
20+when defined(nixbuild):
21+ import os
22+
23 type
24 LibHandle* = pointer ## a handle to a dynamically loaded library
25
26@@ -95,6 +98,25 @@ proc libCandidates*(s: string, dest: var seq[string]) =
27 libCandidates(prefix & middle & suffix, dest)
28 else:
29 add(dest, s)
30+ when defined(nixbuild):
31+ # Nix doesn't have a global library directory so
32+ # load libraries using an absolute path if one
33+ # can be derived from NIX_LDFLAGS.
34+ #
35+ # During Nix/NixOS packaging the line "define:nixbuild"
36+ # should be appended to the ../../config/nim.cfg file
37+ # to enable this behavior by default.
38+ #
39+ var libDirs = split(getEnv("LD_LIBRARY_PATH"), ':')
40+ for flag in split(getEnv("NIX_LDFLAGS")):
41+ if flag.startsWith("-L"):
42+ libDirs.add(flag[2..flag.high])
43+ for lib in dest:
44+ for dir in libDirs:
45+ let abs = dir / lib
46+ if existsFile(abs):
47+ dest = @[abs]
48+ return
49
50 proc loadLibPattern*(pattern: string, globalSymbols = false): LibHandle =
51 ## loads a library with name matching `pattern`, similar to what `dlimport`