summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ma/mathematica/generic.nix
blob: c331971aafd7dfd3017ab14821404e66cad14e3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{
  addDriverRunpath,
  autoPatchelfHook,
  lib,
  makeWrapper,
  stdenv,
  symlinkJoin,
  # arguments from default.nix
  lang,
  meta,
  pname,
  src,
  version,
  # dependencies
  alsa-lib,
  cudaPackages,
  cups,
  dbus,
  flite,
  fontconfig,
  freetype,
  gcc-unwrapped,
  glib,
  gmpxx,
  keyutils,
  libGL,
  libGLU,
  libpcap,
  libtins,
  libuuid,
  libxkbcommon,
  libxml2,
  llvmPackages,
  matio,
  mpfr,
  ncurses,
  opencv4,
  openjdk11,
  openssl,
  pciutils,
  tre,
  unixodbc,
  libxcb-image,
  libxcb-keysyms,
  xkeyboard_config,
  libxtst,
  libxscrnsaver,
  libxrender,
  libxrandr,
  libxmu,
  libxi,
  libxinerama,
  libxfixes,
  libxext,
  libxdamage,
  libxcursor,
  libxcomposite,
  libx11,
  libsm,
  libice,
  libxcb,
  zlib,
  # options
  cudaSupport,
}:

let
  cudaEnv = symlinkJoin {
    name = "mathematica-cuda-env";
    paths = with cudaPackages; [
      cuda_cudart
      cuda_nvcc
      libcublas
      libcufft
      libcurand
      libcusparse
    ];
    postBuild = ''
      if [ ! -e $out/lib/libcuda.so ]; then
          ln -s ${addDriverRunpath.driverLink}/lib/libcuda.so $out/lib
      fi
      ln -s lib $out/lib64
    '';
  };

in
stdenv.mkDerivation {
  inherit
    meta
    pname
    src
    ;

  version =
    version # a comment to trick nixfmt
    + lib.optionalString cudaSupport "-cuda"
    + lib.optionalString (lang != "en") "-${lang}";

  nativeBuildInputs = [
    autoPatchelfHook
    makeWrapper
  ]
  ++ lib.optional cudaSupport addDriverRunpath;

  buildInputs = [
    alsa-lib
    cups.lib
    dbus
    flite
    fontconfig
    freetype
    glib
    gmpxx
    keyutils.lib
    libGL
    libGLU
    libpcap
    libtins
    libuuid
    libxkbcommon
    libxml2
    llvmPackages.libllvm.lib
    matio
    mpfr
    ncurses
    opencv4
    openjdk11
    openssl
    pciutils
    tre
    unixodbc
    libxcb-image
    libxcb-keysyms
    xkeyboard_config
    libice
    libsm
    libx11
    libxscrnsaver
    libxcomposite
    libxcursor
    libxdamage
    libxext
    libxfixes
    libxi
    libxinerama
    libxmu
    libxrandr
    libxrender
    libxtst
    libxcb
  ]
  ++ lib.optional cudaSupport cudaEnv;

  wrapProgramFlags = [
    "--prefix LD_LIBRARY_PATH : ${
      lib.makeLibraryPath [
        dbus
        gcc-unwrapped.lib
        zlib
      ]
    }"
    "--prefix PATH : ${lib.makeBinPath [ stdenv.cc ]}"
    # Fix libQt errors - #96490
    "--set USE_WOLFRAM_LD_LIBRARY_PATH 1"
    # Fix xkeyboard config path for Qt
    "--set QT_XKB_CONFIG_ROOT ${xkeyboard_config}/share/X11/xkb"
    # if wayland isn't supported we fail over to xcb
    # see https://github.com/qt/qtbase/blob/35d0f012ee9b95e8cf3563a41d710ff3c023d841/src/gui/kernel/qguiapplication.cpp#L1218
    "--set QT_QPA_PLATFORM wayland;xcb"
  ]
  ++ lib.optionals cudaSupport [
    "--set CUDA_PATH ${cudaEnv}"
    "--set NVIDIA_DRIVER_LIBRARY_PATH ${addDriverRunpath.driverLink}/lib/libnvidia-tls.so"
    "--set CUDA_LIBRARY_PATH ${addDriverRunpath.driverLink}/lib/libcuda.so"
  ];

  unpackPhase = ''
    runHook preUnpack

    # Find offset from file
    offset=$(${stdenv.shell} -c "$(grep -axm1 -e 'offset=.*' $src); echo \$offset" $src)
    tail -c +$(($offset + 1)) $src | tar -xf -

    runHook postUnpack
  '';

  installPhase = ''
    runHook preInstall

    cd "$TMPDIR/Unix/Installer"

    mkdir -p "$out/lib/udev/rules.d"

    # Set name of installer file
    if [ -f "MathInstaller" ]; then
      INSTALLER="MathInstaller"
    else
      INSTALLER="WolframInstaller"
    fi
    # Patch Installer's shebangs and udev rules dir
    patchShebangs $INSTALLER
    substituteInPlace $INSTALLER \
      --replace /etc/udev/rules.d $out/lib/udev/rules.d

    # Remove PATH restriction, root and avahi daemon checks, and hostname call
    sed -i '
      s/^\s*PATH=/# &/
      s/isRoot="false"/# &/
      s/^\s*checkAvahiDaemon$/:/
      s/^\s*installBundledInstall$/:/
      s/`hostname`/""/
    ' $INSTALLER

    # NOTE: some files placed under HOME may be useful
    XDG_DATA_HOME="$out/share" HOME="$TMPDIR/home" vernierLink=y \
      ./$INSTALLER -execdir="$out/bin" -targetdir="$out/libexec/Mathematica" -auto -verbose -createdir=y

    # Check if Installer produced any errors
    errLog="$out/libexec/Mathematica/InstallErrors"
    if [ -f "$errLog" ]; then
      echo "Installation errors:"
      cat "$errLog"
      return 1
    fi

    runHook postInstall
  '';

  preFixup = ''
    for bin in $out/libexec/Mathematica/Executables/*; do
      wrapProgram "$bin" ''${wrapProgramFlags[@]}
    done
  '';

  dontConfigure = true;
  dontBuild = true;

  # This is primarily an IO bound build; there's little benefit to building remotely
  preferLocalBuild = true;

  # All binaries are already stripped
  dontStrip = true;

  # NOTE: Some deps are still not found; ignore for now
  autoPatchelfIgnoreMissingDeps = true;
}