1--- a/mesonbuild/backend/backends.py
2+++ b/mesonbuild/backend/backends.py
3@@ -723,6 +723,21 @@
4 @staticmethod
5 def get_rpath_dirs_from_link_args(args: T.List[str]) -> T.Set[str]:
6 dirs: T.Set[str] = set()
7+
8+ nix_ldflags = os.environ.get('NIX_LDFLAGS', '').split()
9+ next_is_path = False
10+ # Try to add rpaths set by user or ld-wrapper so that they are not removed.
11+ # Based on https://github.com/NixOS/nixpkgs/blob/69711a2f5ffe8cda208163be5258266172ff527f/pkgs/build-support/bintools-wrapper/ld-wrapper.sh#L148-L177
12+ for flag in nix_ldflags:
13+ if flag == '-rpath' or flag == '-L':
14+ next_is_path = True
15+ elif next_is_path or flag.startswith('-L/'):
16+ if flag.startswith('-L/'):
17+ flag = flag[2:]
18+ if flag.startswith('@storeDir@'):
19+ dirs.add(flag)
20+ next_is_path = False
21+
22 # Match rpath formats:
23 # -Wl,-rpath=
24 # -Wl,-rpath,