1diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
2index 64575557..1eb0a2b4 100644
3--- a/giscanner/scannermain.py
4+++ b/giscanner/scannermain.py
5@@ -95,6 +95,39 @@ def get_windows_option_group(parser):
6 return group
7
8
9+def _get_default_fallback_libpath():
10+ # Newer multiple-output-optimized stdenv has an environment variable
11+ # $outputLib which in turn specifies another variable which then is used as
12+ # the destination for the library contents (${!outputLib}/lib).
13+ store_path = os.environ.get(os.environ.get("outputLib")) if "outputLib" in os.environ else None
14+ if store_path is None:
15+ outputs = os.environ.get("outputs", "out").split()
16+ if "lib" in outputs:
17+ # For multiple output derivations let's try whether there is a $lib
18+ # environment variable and use that as the base store path.
19+ store_path = os.environ.get("lib")
20+ elif "out" in outputs:
21+ # Otherwise we have a single output derivation, so the libraries
22+ # most certainly will end up in "$out/lib".
23+ store_path = os.environ.get("out")
24+
25+ if store_path is not None:
26+ # Even if we have a $lib as output, there still should be a $lib/lib
27+ # directory.
28+ return os.path.join(store_path, 'lib')
29+ else:
30+ # If we haven't found a possible scenario, let's return an empty string
31+ # so that the shared library won't be prepended with a path.
32+ #
33+ # Note that this doesn't mean that all hope is lost, because after all
34+ # we can still use --fallback-library-path to set one.
35+ #
36+ # Also, we're not returning None, because that would make it very
37+ # difficult to disable adding fallback paths altogether using something
38+ # like: --fallback-library-path=""
39+ return ""
40+
41+
42 def _get_option_parser():
43 parser = optparse.OptionParser('%prog [options] sources',
44 version='%prog ' + giscanner.__version__)
45@@ -214,6 +247,10 @@ match the namespace prefix.""")
46 parser.add_option("", "--c-include",
47 action="append", dest="c_includes", default=[],
48 help="headers which should be included in C programs")
49+ parser.add_option("", "--fallback-library-path",
50+ action="store", dest="fallback_libpath",
51+ default=_get_default_fallback_libpath(),
52+ help="Path to prepend to unknown shared libraries")
53 parser.add_option("", "--filelist",
54 action="store", dest="filelist", default=[],
55 help="file containing headers and sources to be scanned")
56diff --git a/giscanner/shlibs.py b/giscanner/shlibs.py
57index 9f8ab5df..af325022 100644
58--- a/giscanner/shlibs.py
59+++ b/giscanner/shlibs.py
60@@ -57,6 +57,12 @@ def _ldd_library_pattern(library_name):
61 $""" % re.escape(library_name), re.VERBOSE)
62
63
64+def _ldd_library_nix_pattern(library_name):
65+ nix_store_dir = re.escape('@nixStoreDir@'.rstrip('/'))
66+ pattern = r'(%s(?:/[^/]*)+lib%s[^A-Za-z0-9_-][^\s\(\)]*)'
67+ return re.compile(pattern % (nix_store_dir, re.escape(library_name)))
68+
69+
70 # This is a what we do for non-la files. We assume that we are on an
71 # ELF-like system where ldd exists and the soname extracted with ldd is
72 # a filename that can be opened with dlopen().
73@@ -108,7 +114,8 @@ def _resolve_non_libtool(options, binary, libraries):
74 output = output.decode("utf-8", "replace")
75
76 shlibs = resolve_from_ldd_output(libraries, output)
77- return list(map(sanitize_shlib_path, shlibs))
78+ fallback_libpath = options.fallback_libpath or "";
79+ return list(map(lambda p: os.path.join(fallback_libpath, p), map(sanitize_shlib_path, shlibs)))
80
81
82 def sanitize_shlib_path(lib):
83@@ -117,19 +124,18 @@ def sanitize_shlib_path(lib):
84 # In case we get relative paths on macOS (like @rpath) then we fall
85 # back to the basename as well:
86 # https://gitlab.gnome.org/GNOME/gobject-introspection/issues/222
87- if sys.platform == "darwin":
88- if not os.path.isabs(lib):
89- return os.path.basename(lib)
90- return lib
91- else:
92+
93+ # Always use absolute paths if available
94+ if not os.path.isabs(lib):
95 return os.path.basename(lib)
96+ return lib
97
98
99 def resolve_from_ldd_output(libraries, output):
100 patterns = {}
101 for library in libraries:
102 if not os.path.isfile(library):
103- patterns[library] = _ldd_library_pattern(library)
104+ patterns[library] = (_ldd_library_pattern(library), _ldd_library_nix_pattern(library))
105 if len(patterns) == 0:
106 return []
107
108@@ -141,8 +147,11 @@ def resolve_from_ldd_output(libraries, output):
109 if line.endswith(':'):
110 continue
111 for word in line.split():
112- for library, pattern in patterns.items():
113- m = pattern.match(word)
114+ for library, (pattern, nix_pattern) in patterns.items():
115+ if line.find('@nixStoreDir@') != -1:
116+ m = nix_pattern.match(word)
117+ else:
118+ m = pattern.match(word)
119 if m:
120 del patterns[library]
121 shlibs.append(m.group())
122diff --git a/giscanner/utils.py b/giscanner/utils.py
123index 31c7ea48..630002a8 100644
124--- a/giscanner/utils.py
125+++ b/giscanner/utils.py
126@@ -114,16 +114,11 @@ def extract_libtool_shlib(la_file):
127 if dlname is None:
128 return None
129
130- # Darwin uses absolute paths where possible; since the libtool files never
131- # contain absolute paths, use the libdir field
132- if platform.system() == 'Darwin':
133- dlbasename = os.path.basename(dlname)
134- libdir = _extract_libdir_field(la_file)
135- if libdir is None:
136- return dlbasename
137- return libdir + '/' + dlbasename
138- # Older libtools had a path rather than the raw dlname
139- return os.path.basename(dlname)
140+ dlbasename = os.path.basename(dlname)
141+ libdir = _extract_libdir_field(la_file)
142+ if libdir is None:
143+ return dlbasename
144+ return libdir + '/' + dlbasename
145
146
147 # Returns arguments for invoking libtool, if applicable, otherwise None
148diff --git a/tests/scanner/test_shlibs.py b/tests/scanner/test_shlibs.py
149index a8337c60..72789d76 100644
150--- a/tests/scanner/test_shlibs.py
151+++ b/tests/scanner/test_shlibs.py
152@@ -7,6 +7,30 @@ from giscanner.shlibs import resolve_from_ldd_output, sanitize_shlib_path
153
154 class TestLddParser(unittest.TestCase):
155
156+ def test_resolve_from_ldd_output_nix(self):
157+ output = '''\
158+ libglib-2.0.so.0 => @nixStoreDir@/gmrf09y7sfxrr0mcx90dba7w41jj2kzk-glib-2.58.1/lib/libglib-2.0.so.0 (0x00007f0ee1b28000)
159+ libgobject-2.0.so.0 => @nixStoreDir@/gmrf09y7sfxrr0mcx90dba7w41jj2kzk-glib-2.58.1/lib/libgobject-2.0.so.0 (0x00007f0ee18cf000)
160+ libgio-2.0.so.0 => @nixStoreDir@/gmrf09y7sfxrr0mcx90dba7w41jj2kzk-glib-2.58.1/lib/libgio-2.0.so.0 (0x00007f0ee1502000)
161+ libxml2.so.2 => @nixStoreDir@/72mxkk74cv266snkjpz1kwl1i2rg8rpc-libxml2-2.9.8/lib/libxml2.so.2 (0x00007f0ee119c000)
162+ libsqlite3.so.0 => @nixStoreDir@/ck5ay23hsmlc67pg3m34kzd1k2hhvww0-sqlite-3.24.0/lib/libsqlite3.so.0 (0x00007f0ee0e98000)
163+ libpsl.so.5 => @nixStoreDir@/qn3l2gn7m76f318676wflrs2z6d4rrkj-libpsl-0.20.2-list-2017-02-03/lib/libpsl.so.5 (0x00007f0ee0c88000)
164+ libc.so.6 => @nixStoreDir@/g2yk54hifqlsjiha3szr4q3ccmdzyrdv-glibc-2.27/lib/libc.so.6 (0x00007f0ee08d4000)
165+ libpcre.so.1 => @nixStoreDir@/hxbq8lpc53qsf1bc0dfcsm47wmcxzjvh-pcre-8.42/lib/libpcre.so.1 (0x00007f0ee0662000)
166+ @nixStoreDir@/g2yk54hifqlsjiha3szr4q3ccmdzyrdv-glibc-2.27/lib64/ld-linux-x86-64.so.2 (0x00007f0ee20ff000)
167+ libblkid.so.1 => @nixStoreDir@/q0kgnq21j0l2yd77gdlld371246cwghh-util-linux-2.32.1/lib/libblkid.so.1 (0x00007f0edd0cd000)
168+ libuuid.so.1 => @nixStoreDir@/q0kgnq21j0l2yd77gdlld371246cwghh-util-linux-2.32.1/lib/libuuid.so.1 (0x00007f0edcec5000)
169+ librt.so.1 => @nixStoreDir@/g2yk54hifqlsjiha3szr4q3ccmdzyrdv-glibc-2.27/lib/librt.so.1 (0x00007f0edccbd000)
170+ libstdc++.so.6 => @nixStoreDir@/3v5r7fkrbkw2qajadvjbf6p6qriz9p1i-gcc-7.3.0-lib/lib/libstdc++.so.6 (0x00007f0edc936000)
171+ libgcc_s.so.1 => @nixStoreDir@/g2yk54hifqlsjiha3szr4q3ccmdzyrdv-glibc-2.27/lib/libgcc_s.so.1 (0x00007f0edc720000)
172+ '''
173+ libraries = ['glib-2.0', 'gio-2.0']
174+
175+ self.assertEqual(
176+ ['@nixStoreDir@/gmrf09y7sfxrr0mcx90dba7w41jj2kzk-glib-2.58.1/lib/libglib-2.0.so.0',
177+ '@nixStoreDir@/gmrf09y7sfxrr0mcx90dba7w41jj2kzk-glib-2.58.1/lib/libgio-2.0.so.0'],
178+ resolve_from_ldd_output(libraries, output))
179+
180 def test_resolve_from_ldd_output(self):
181 output = '''\
182 libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fbe12d68000)
183@@ -40,7 +64,8 @@ class TestLddParser(unittest.TestCase):
184
185 self.assertEqual(
186 sanitize_shlib_path('/foo/bar'),
187- '/foo/bar' if sys.platform == 'darwin' else 'bar')
188+ # NixOS always want the absolute path
189+ '/foo/bar')
190
191 def test_unresolved_library(self):
192 output = ''