summary refs log tree commit diff
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2019-05-22 18:19:55 +0200
committerAmbroz Bizjak <abizjak.pro@gmail.com>2019-05-22 18:19:55 +0200
commit2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699 (patch)
treeff1733892a8af3d330b063de2da50fe2d9317ad2
parentbae81580a2e6c8d6ba163133b3320339b498bb6b (diff)
downloadnixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.tar
nixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.tar.gz
nixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.tar.bz2
nixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.tar.lz
nixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.tar.xz
nixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.tar.zst
nixpkgs-2874e849d99e4e96154fc2a7ce4b2e5d8ed4e699.zip
addOpenGLRunpath: Add new hook for setting RUNPATH.
This hook allows to add NixOS driver libraries path to given ELF
objects' RUNPATH. We use it instead of settings RUNPATH manually
everywhere. It must be invoked in postFixup so that RUNPATH stripping
does not remove the path.

It puts the path first instead of last so that system-wide drivers
are always preferred.
-rw-r--r--pkgs/build-support/add-opengl-runpath/default.nix12
-rw-r--r--pkgs/build-support/add-opengl-runpath/setup-hook.sh28
-rw-r--r--pkgs/top-level/all-packages.nix2
3 files changed, 42 insertions, 0 deletions
diff --git a/pkgs/build-support/add-opengl-runpath/default.nix b/pkgs/build-support/add-opengl-runpath/default.nix
new file mode 100644
index 00000000000..5cab0937e07
--- /dev/null
+++ b/pkgs/build-support/add-opengl-runpath/default.nix
@@ -0,0 +1,12 @@
+{ lib, stdenv }:
+
+stdenv.mkDerivation {
+  name = "add-opengl-runpath";
+
+  driverLink = "/run/opengl-driver" + lib.optionalString stdenv.isi686 "-32";
+
+  buildCommand = ''
+    mkdir -p $out/nix-support
+    substituteAll ${./setup-hook.sh} $out/nix-support/setup-hook
+  '';
+}
diff --git a/pkgs/build-support/add-opengl-runpath/setup-hook.sh b/pkgs/build-support/add-opengl-runpath/setup-hook.sh
new file mode 100644
index 00000000000..7645033ca20
--- /dev/null
+++ b/pkgs/build-support/add-opengl-runpath/setup-hook.sh
@@ -0,0 +1,28 @@
+# Set RUNPATH so that driver libraries in /run/opengl-driver(-32)/lib can be found.
+# This is needed to not rely on LD_LIBRARY_PATH which does not work with setuid
+# executables. Fixes https://github.com/NixOS/nixpkgs/issues/22760. It must be run
+# in postFixup because RUNPATH stripping in fixup would undo it. Note that patchelf
+# actually sets RUNPATH not RPATH, which applies only to dependencies of the binary
+# it set on (including for dlopen), so the RUNPATH must indeed be set on these
+# libraries and would not work if set only on executables.
+addOpenGLRunpath() {
+    local forceRpath=
+
+    while [ $# -gt 0 ]; do
+        case "$1" in
+            --) shift; break;;
+            --force-rpath) shift; forceRpath=1;;
+            --*)
+                echo "addOpenGLRunpath: ERROR: Invalid command line" \
+                     "argument: $1" >&2
+                return 1;;
+            *) break;;
+        esac
+    done
+
+    for file in "$@"; do
+        local origRpath="$(patchelf --print-rpath "$file")"
+        patchelf --set-rpath "@driverLink@/lib:$origRpath" ${forceRpath:+--force-rpath} "$file"
+    done
+}
+
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index b86fa5c3d08..a60df81e929 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -126,6 +126,8 @@ in
       }
     '');
 
+  addOpenGLRunpath = callPackage ../build-support/add-opengl-runpath { };
+
   # Zip file format only allows times after year 1980, which makes e.g. Python wheel building fail with:
   # ValueError: ZIP does not support timestamps before 1980
   ensureNewerSourcesForZipFilesHook = ensureNewerSourcesHook { year = "1980"; };