1# This is a test for autoPatchelfHook. To test it, we just need a simple binary
2# which uses the hook. We took the derivation from tonelib-jam, which sounds
3# like a good candidate with a small closure, and trimmed it down.
4
5{
6 stdenv,
7 lib,
8 fetchurl,
9 autoPatchelfHook,
10 dpkg,
11 freetype,
12 curl,
13 # This test checks that the behavior of autoPatchelfHook is correct whether
14 # __structuredAttrs
15 # (https://nixos.org/manual/nix/stable/language/advanced-attributes#adv-attr-structuredAttrs)
16 # is set or not. Hence __structuredAttrs is provided as a parameter.
17 __structuredAttrs,
18}:
19
20let
21 runtimeDependencies = [
22 (lib.getLib curl)
23 "/some/dep"
24 "/some/other/dep"
25 ]
26 # A dependency with space only works with __structuredAttrs set to true.
27 ++ lib.lists.optional __structuredAttrs "/some/dep with space";
28in
29
30stdenv.mkDerivation {
31 name = "auto-patchelf-test";
32
33 src = fetchurl {
34 url = "https://tonelib.net/download/221222/ToneLib-Jam-amd64.deb";
35 sha256 = "sha256-c6At2lRPngQPpE7O+VY/Hsfw+QfIb3COIuHfbqqIEuM=";
36 };
37
38 unpackCmd = ''
39 dpkg -x $curSrc source
40 '';
41
42 nativeBuildInputs = [
43 dpkg
44 autoPatchelfHook
45 ];
46
47 installPhase = ''
48 mv usr $out
49 '';
50
51 buildInputs = [
52 freetype
53 ];
54
55 autoPatchelfIgnoreMissingDeps = [
56 "libGL.so.1"
57 "libasound.so.2"
58 ];
59
60 inherit runtimeDependencies;
61
62 # Additional phase performing the actual test.
63 installCheckPhase =
64 let
65 allDeps = runtimeDependencies ++ [
66 (lib.getLib freetype)
67 ];
68 in
69 ''
70 local binary="$out/bin/ToneLib-Jam"
71 local interpreter=$(patchelf --print-interpreter $binary)
72 local runpath=$(patchelf --print-rpath $binary)
73 local glibcStorePath="${stdenv.cc.libc}"
74
75 # Check that the glibc path is a prefix of the interpreter. If
76 # autoPatchelfHook ran correctly, the binary should have set the interpreter
77 # to point to the store.
78 echo "[auto-patchelf-hook-test]: Check that the interpreter is in the store"
79 test "''${interpreter#$glibcStorePath}" != "$interpreter"
80
81 readarray -td':' runpathArray < <(echo -n "$runpath")
82
83 echo "[auto-patchelf-hook-test]: Check that the runpath has the right number of entries"
84 test "''${#runpathArray[@]}" -eq ${builtins.toString (builtins.length allDeps)}
85
86 echo "[auto-patchelf-hook-test]: Check that the runpath contains the expected runtime deps"
87 ''
88 + lib.strings.concatStringsSep "\n" (
89 lib.lists.imap0 (
90 i: path:
91 let
92 iAsStr = builtins.toString i;
93 in
94 ''
95 echo "[auto-patchelf-hook-test]: Check that entry ${iAsStr} is ${path}"
96 test "''${paths[${iAsStr}]}" = "$path"
97 ''
98 ) allDeps
99 );
100
101 doInstallCheck = true;
102 inherit __structuredAttrs;
103 meta = {
104 # Downloads an x86_64-linux only binary
105 platforms = [ "x86_64-linux" ];
106 };
107}