1{
2 addDriverRunpath,
3 autoPatchelfHook,
4 lib,
5 makeWrapper,
6 requireFile,
7 runCommand,
8 stdenv,
9 symlinkJoin,
10 # arguments from default.nix
11 lang,
12 meta,
13 name,
14 src,
15 version,
16 # dependencies
17 alsa-lib,
18 cudaPackages,
19 cups,
20 dbus,
21 flite,
22 fontconfig,
23 freetype,
24 gcc-unwrapped,
25 glib,
26 gmpxx,
27 keyutils,
28 libGL,
29 libGLU,
30 libpcap,
31 libtins,
32 libuuid,
33 libxkbcommon,
34 libxml2,
35 llvmPackages_13,
36 matio,
37 mpfr,
38 ncurses,
39 opencv4,
40 openjdk11,
41 openssl,
42 pciutils,
43 tre,
44 unixODBC,
45 xcbutilimage,
46 xcbutilkeysyms,
47 xkeyboard_config,
48 xorg,
49 zlib,
50 # options
51 cudaSupport,
52}:
53
54let
55 cudaEnv = symlinkJoin {
56 name = "mathematica-cuda-env";
57 paths = with cudaPackages; [
58 cuda_cudart
59 cuda_nvcc
60 libcublas
61 libcufft
62 libcurand
63 libcusparse
64 ];
65 postBuild = ''
66 if [ ! -e $out/lib/libcuda.so ]; then
67 ln -s ${addDriverRunpath.driverLink}/lib/libcuda.so $out/lib
68 fi
69 ln -s lib $out/lib64
70 '';
71 };
72
73in
74stdenv.mkDerivation {
75 inherit
76 meta
77 name
78 src
79 version
80 ;
81
82 nativeBuildInputs = [
83 autoPatchelfHook
84 makeWrapper
85 ]
86 ++ lib.optional cudaSupport addDriverRunpath;
87
88 buildInputs = [
89 alsa-lib
90 cups.lib
91 dbus
92 flite
93 fontconfig
94 freetype
95 glib
96 gmpxx
97 keyutils.lib
98 libGL
99 libGLU
100 libpcap
101 libtins
102 libuuid
103 libxkbcommon
104 libxml2
105 llvmPackages_13.libllvm.lib
106 matio
107 mpfr
108 ncurses
109 opencv4
110 openjdk11
111 openssl
112 pciutils
113 tre
114 unixODBC
115 xcbutilimage
116 xcbutilkeysyms
117 xkeyboard_config
118 ]
119 ++ (with xorg; [
120 libICE
121 libSM
122 libX11
123 libXScrnSaver
124 libXcomposite
125 libXcursor
126 libXdamage
127 libXext
128 libXfixes
129 libXi
130 libXinerama
131 libXmu
132 libXrandr
133 libXrender
134 libXtst
135 libxcb
136 ])
137 ++ lib.optional cudaSupport cudaEnv;
138
139 wrapProgramFlags = [
140 "--prefix LD_LIBRARY_PATH : ${
141 lib.makeLibraryPath [
142 dbus
143 gcc-unwrapped.lib
144 zlib
145 ]
146 }"
147 "--prefix PATH : ${lib.makeBinPath [ stdenv.cc ]}"
148 # Fix libQt errors - #96490
149 "--set USE_WOLFRAM_LD_LIBRARY_PATH 1"
150 # Fix xkeyboard config path for Qt
151 "--set QT_XKB_CONFIG_ROOT ${xkeyboard_config}/share/X11/xkb"
152 # if wayland isn't supported we fail over to xcb
153 # see https://github.com/qt/qtbase/blob/35d0f012ee9b95e8cf3563a41d710ff3c023d841/src/gui/kernel/qguiapplication.cpp#L1218
154 "--set QT_QPA_PLATFORM wayland;xcb"
155 ]
156 ++ lib.optionals cudaSupport [
157 "--set CUDA_PATH ${cudaEnv}"
158 "--set NVIDIA_DRIVER_LIBRARY_PATH ${addDriverRunpath.driverLink}/lib/libnvidia-tls.so"
159 "--set CUDA_LIBRARY_PATH ${addDriverRunpath.driverLink}/lib/libcuda.so"
160 ];
161
162 unpackPhase = ''
163 runHook preUnpack
164
165 # Find offset from file
166 offset=$(${stdenv.shell} -c "$(grep -axm1 -e 'offset=.*' $src); echo \$offset" $src)
167 tail -c +$(($offset + 1)) $src | tar -xf -
168
169 runHook postUnpack
170 '';
171
172 installPhase = ''
173 runHook preInstall
174
175 cd "$TMPDIR/Unix/Installer"
176
177 mkdir -p "$out/lib/udev/rules.d"
178
179 # Set name of installer file
180 if [ -f "MathInstaller" ]; then
181 INSTALLER="MathInstaller"
182 else
183 INSTALLER="WolframInstaller"
184 fi
185 # Patch Installer's shebangs and udev rules dir
186 patchShebangs $INSTALLER
187 substituteInPlace $INSTALLER \
188 --replace /etc/udev/rules.d $out/lib/udev/rules.d
189
190 # Remove PATH restriction, root and avahi daemon checks, and hostname call
191 sed -i '
192 s/^\s*PATH=/# &/
193 s/isRoot="false"/# &/
194 s/^\s*checkAvahiDaemon$/:/
195 s/^\s*installBundledInstall$/:/
196 s/`hostname`/""/
197 ' $INSTALLER
198
199 # NOTE: some files placed under HOME may be useful
200 XDG_DATA_HOME="$out/share" HOME="$TMPDIR/home" vernierLink=y \
201 ./$INSTALLER -execdir="$out/bin" -targetdir="$out/libexec/Mathematica" -auto -verbose -createdir=y
202
203 # Check if Installer produced any errors
204 errLog="$out/libexec/Mathematica/InstallErrors"
205 if [ -f "$errLog" ]; then
206 echo "Installation errors:"
207 cat "$errLog"
208 return 1
209 fi
210
211 runHook postInstall
212 '';
213
214 preFixup = ''
215 for bin in $out/libexec/Mathematica/Executables/*; do
216 wrapProgram "$bin" ''${wrapProgramFlags[@]}
217 done
218 '';
219
220 dontConfigure = true;
221 dontBuild = true;
222
223 # This is primarily an IO bound build; there's little benefit to building remotely
224 preferLocalBuild = true;
225
226 # All binaries are already stripped
227 dontStrip = true;
228
229 # NOTE: Some deps are still not found; ignore for now
230 autoPatchelfIgnoreMissingDeps = true;
231}