-diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
-index 89ec193..54f1d2e 100755
 --- a/giscanner/scannermain.py
 +++ b/giscanner/scannermain.py
-@@ -94,6 +94,39 @@ def get_windows_option_group(parser):
+@@ -100,6 +100,39 @@
      return group
  
  

 
     group = get_preprocessor_option_group(parser)
     parser.add_option_group(group)
diff --git a/giscanner/shlibs.py b/giscanner/shlibs.py
index 838d343..ca7fc0d 100644
--- a/giscanner/shlibs.py
+++ b/giscanner/shlibs.py
@@ -53,10 +53,27 @@ def _resolve_libtool(options, binary, libraries):
 # Match absolute paths on OS X to conform to how libraries are usually
 # referenced on OS X systems.
 def _ldd_library_pattern(library_name):
+    nix_store_dir = re.escape('@nixStoreDir@'.rstrip('/'))
     pattern = "(?<![A-Za-z0-9_-])(lib*%s[^A-Za-z0-9_-][^\s\(\)]*)"
     if platform.system() == 'Darwin':
         pattern = "([^\s]*lib*%s[^A-Za-z0-9_-][^\s\(\)]*)"
-    return re.compile(pattern % re.escape(library_name))
+        return re.compile(pattern % re.escape(library_name))
+    pattern = r'''
+        (
+          (?:
+            # First match Nix store paths because they need to be absolute.
+            (?:%s(?:/[^/]*)+)
+            # Everything else not a store path remains relative, because we
+            # would end up with temporary paths that are only valid during
+            # build time in the resulting GIR file.
+            | (?<=/)
+          )
+          # And finally the library itself:
+          (?:lib%s[^A-Za-z0-9_-][^\s\(\)]*)
+        )
+    '''
+    return re.compile(pattern % (nix_store_dir, re.escape(library_name)),
+                      re.VERBOSE)
 
 
 # This is a what we do for non-la files. We assume that we are on an
@@ -115,7 +132,11 @@ def _resolve_non_libtool(options, binary, libraries):
                 m = pattern.search(line)
                 if m:
                     del patterns[library]
-                    shlibs.append(m.group(1))
+                    match = m.group(1)
+                    if not match.startswith('/') \
+                       and len(options.fallback_libpath) > 0:
+                        match = os.path.join(options.fallback_libpath, match)
+                    shlibs.append(match)
                     break
 
         if len(patterns) > 0:
diff --git a/giscanner/utils.py b/giscanner/utils.py
index 660081e..c9c767a 100644
--- a/giscanner/utils.py
+++ b/giscanner/utils.py
@@ -109,17 +109,11 @@ def extract_libtool_shlib(la_file):
     if dlname is None:
         return None
 

 def _get_option_parser():
     parser = optparse.OptionParser('%prog [options] sources')
     parser.add_option('', "--quiet",
@@ -209,6 +242,10 @@
     parser.add_option("", "--filelist",
                       action="store", dest="filelist", default=[],
                       help="file containing headers and sources to be scanned")
