1{ lib, stdenv, makeWrapper, fetchurl, requireFile, perl, ncurses5, expat, python27, zlib
2, gcc48, gcc49, gcc5, gcc6, gcc7
3, xorg, gtk2, gdk_pixbuf, glib, fontconfig, freetype, unixODBC, alsaLib, glibc
4}:
5
6let
7
8 common =
9 args@{ gcc, version, sha256
10 , url ? ""
11 , name ? ""
12 , developerProgram ? false
13 , python ? python27
14 , runPatches ? []
15 }:
16
17 stdenv.mkDerivation rec {
18 name = "cudatoolkit-${version}";
19 inherit version runPatches;
20
21 dontPatchELF = true;
22 dontStrip = true;
23
24 src =
25 if developerProgram then
26 requireFile {
27 message = ''
28 This nix expression requires that ${args.name} is already part of the store.
29 Register yourself to NVIDIA Accelerated Computing Developer Program, retrieve the CUDA toolkit
30 at https://developer.nvidia.com/cuda-toolkit, and run the following command in the download directory:
31 nix-prefetch-url file://\$PWD/${args.name}
32 '';
33 inherit (args) name sha256;
34 }
35 else
36 fetchurl {
37 inherit (args) url sha256;
38 };
39
40 outputs = [ "out" "lib" "doc" ];
41
42 nativeBuildInputs = [ perl makeWrapper ];
43 buildInputs = [ gdk_pixbuf ]; # To get $GDK_PIXBUF_MODULE_FILE via setup-hook
44 runtimeDependencies = [
45 ncurses5 expat python zlib glibc
46 xorg.libX11 xorg.libXext xorg.libXrender xorg.libXt xorg.libXtst xorg.libXi xorg.libXext
47 gtk2 glib fontconfig freetype unixODBC alsaLib
48 ];
49
50 rpath = "${stdenv.lib.makeLibraryPath runtimeDependencies}:${stdenv.cc.cc.lib}/lib64";
51
52 unpackPhase = ''
53 sh $src --keep --noexec
54
55 cd pkg/run_files
56 sh cuda-linux*.run --keep --noexec
57 sh cuda-samples*.run --keep --noexec
58 mv pkg ../../$(basename $src)
59 cd ../..
60 rm -rf pkg
61
62 for patch in $runPatches; do
63 sh $patch --keep --noexec
64 mv pkg $(basename $patch)
65 done
66 '';
67
68 installPhase = ''
69 runHook preInstall
70 mkdir $out
71 cd $(basename $src)
72 export PERL5LIB=.
73 perl ./install-linux.pl --prefix="$out"
74 cd ..
75 for patch in $runPatches; do
76 cd $(basename $patch)
77 perl ./install_patch.pl --silent --accept-eula --installdir="$out"
78 cd ..
79 done
80
81 rm $out/tools/CUDA_Occupancy_Calculator.xls # FIXME: why?
82
83 # let's remove the 32-bit libraries, they confuse the lib64->lib mover
84 rm -rf $out/lib
85
86 # Remove some cruft.
87 ${lib.optionalString (lib.versionAtLeast version "7.0") "rm $out/bin/uninstall*"}
88
89 # Fixup path to samples (needed for cuda 6.5 or else nsight will not find them)
90 if [ -d "$out"/cuda-samples ]; then
91 mv "$out"/cuda-samples "$out"/samples
92 fi
93
94 # Change the #error on GCC > 4.9 to a #warning.
95 sed -i $out/include/host_config.h -e 's/#error\(.*unsupported GNU version\)/#warning\1/'
96
97 # Fix builds with newer glibc version
98 sed -i "1 i#define _BITS_FLOATN_H" "$out/include/host_defines.h"
99
100 # Ensure that cmake can find CUDA.
101 mkdir -p $out/nix-support
102 echo "cmakeFlags+=' -DCUDA_TOOLKIT_ROOT_DIR=$out'" >> $out/nix-support/setup-hook
103
104 # Move some libraries to the lib output so that programs that
105 # depend on them don't pull in this entire monstrosity.
106 mkdir -p $lib/lib
107 mv -v $out/lib64/libcudart* $lib/lib/
108
109 # Remove OpenCL libraries as they are provided by ocl-icd and driver.
110 rm -f $out/lib64/libOpenCL*
111
112 # Set compiler for NVCC.
113 wrapProgram $out/bin/nvcc \
114 --prefix PATH : ${gcc}/bin
115
116 # nvprof do not find any program to profile if LD_LIBRARY_PATH is not set
117 wrapProgram $out/bin/nvprof \
118 --prefix LD_LIBRARY_PATH : $out/lib
119 '' + lib.optionalString (lib.versionOlder version "8.0") ''
120 # Hack to fix building against recent Glibc/GCC.
121 echo "NIX_CFLAGS_COMPILE+=' -D_FORCE_INLINES'" >> $out/nix-support/setup-hook
122 '' + ''
123 runHook postInstall
124 '';
125
126 postInstall = ''
127 for b in nvvp nsight; do
128 wrapProgram "$out/bin/$b" \
129 --set GDK_PIXBUF_MODULE_FILE "$GDK_PIXBUF_MODULE_FILE"
130 done
131 '';
132
133 preFixup = ''
134 while IFS= read -r -d ''$'\0' i; do
135 if ! isELF "$i"; then continue; fi
136 echo "patching $i..."
137 if [[ ! $i =~ \.so ]]; then
138 patchelf \
139 --set-interpreter "''$(cat $NIX_CC/nix-support/dynamic-linker)" $i
140 fi
141 if [[ $i =~ libcudart ]]; then
142 rpath2=
143 else
144 rpath2=$rpath:$lib/lib:$out/jre/lib/amd64/jli:$out/lib:$out/lib64:$out/nvvm/lib:$out/nvvm/lib64
145 fi
146 patchelf --set-rpath $rpath2 --force-rpath $i
147 done < <(find $out $lib $doc -type f -print0)
148 '';
149
150 # cuda-gdb doesn't run correctly when not using sandboxing, so
151 # temporarily disabling the install check. This should be set to true
152 # when we figure out how to get `cuda-gdb --version` to run correctly
153 # when not using sandboxing.
154 doInstallCheck = false;
155 postInstallCheck = let
156 in ''
157 # Smoke test binaries
158 pushd $out/bin
159 for f in *; do
160 case $f in
161 crt) continue;;
162 nvcc.profile) continue;;
163 nsight_ee_plugins_manage.sh) continue;;
164 uninstall_cuda_toolkit_6.5.pl) continue;;
165 computeprof|nvvp|nsight) continue;; # GUIs don't feature "--version"
166 *) echo "Executing '$f --version':"; ./$f --version;;
167 esac
168 done
169 popd
170 '';
171 passthru = {
172 cc = gcc;
173 majorVersion =
174 let versionParts = lib.splitString "." version;
175 in "${lib.elemAt versionParts 0}.${lib.elemAt versionParts 1}";
176 };
177
178 meta = with stdenv.lib; {
179 description = "A compiler for NVIDIA GPUs, math libraries, and tools";
180 homepage = "https://developer.nvidia.com/cuda-toolkit";
181 platforms = [ "x86_64-linux" ];
182 license = licenses.unfree;
183 };
184 };
185
186in rec {
187 cudatoolkit_6 = common {
188 version = "6.0.37";
189 url = "http://developer.download.nvidia.com/compute/cuda/6_0/rel/installers/cuda_6.0.37_linux_64.run";
190 sha256 = "991e436c7a6c94ec67cf44204d136adfef87baa3ded270544fa211179779bc40";
191 gcc = gcc48;
192 };
193
194 cudatoolkit_6_5 = common {
195 version = "6.5.19";
196 url = "http://developer.download.nvidia.com/compute/cuda/6_5/rel/installers/cuda_6.5.19_linux_64.run";
197 sha256 = "1x9zdmk8z784d3d35vr2ak1l4h5v4jfjhpxfi9fl9dvjkcavqyaj";
198 gcc = gcc48;
199 };
200
201 cudatoolkit_7 = common {
202 version = "7.0.28";
203 url = "http://developer.download.nvidia.com/compute/cuda/7_0/Prod/local_installers/cuda_7.0.28_linux.run";
204 sha256 = "1km5hpiimx11jcazg0h3mjzk220klwahs2vfqhjavpds5ff2wafi";
205 gcc = gcc49;
206 };
207
208 cudatoolkit_7_5 = common {
209 version = "7.5.18";
210 url = "http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda_7.5.18_linux.run";
211 sha256 = "1v2ylzp34ijyhcxyh5p6i0cwawwbbdhni2l5l4qm21s1cx9ish88";
212 gcc = gcc49;
213 };
214
215 cudatoolkit_8 = common {
216 version = "8.0.61.2";
217 url = "https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run";
218 sha256 = "1i4xrsqbad283qffvysn88w2pmxzxbbby41lw0j1113z771akv4w";
219 runPatches = [
220 (fetchurl {
221 url = "https://developer.nvidia.com/compute/cuda/8.0/Prod2/patches/2/cuda_8.0.61.2_linux-run";
222 sha256 = "1iaz5rrsnsb1p99qiqvxn6j3ksc7ry8xlr397kqcjzxqbljbqn9d";
223 })
224 ];
225 gcc = gcc5;
226 };
227
228 cudatoolkit_9_0 = common {
229 version = "9.0.176.1";
230 url = "https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda_9.0.176_384.81_linux-run";
231 sha256 = "0308rmmychxfa4inb1ird9bpgfppgr9yrfg1qp0val5azqik91ln";
232 runPatches = [
233 (fetchurl {
234 url = "https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/1/cuda_9.0.176.1_linux-run";
235 sha256 = "1vbqg97pq9z9c8nqvckiwmq3ljm88m7gaizikzxbvz01izh67gx4";
236 })
237 (fetchurl {
238 url = "https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/2/cuda_9.0.176.2_linux-run";
239 sha256 = "1sz5dijbx9yf7drfipdxav5a5g6sxy4w6vi9xav0lb6m2xnmyd7c";
240 })
241 (fetchurl {
242 url = "https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/3/cuda_9.0.176.3_linux-run";
243 sha256 = "1jm83bxpscpjhzs5q3qijdgjm0r8qrdlgkj7y08fq8c0v8q2r7j2";
244 })
245 (fetchurl {
246 url = "https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/4/cuda_9.0.176.4_linux-run";
247 sha256 = "0pymg3mymsa2n48y0njz3spzlkm15lvjzw8fms1q83zslz4x0lwk";
248 })
249 ];
250 gcc = gcc6;
251 };
252
253 cudatoolkit_9_1 = common {
254 version = "9.1.85.3";
255 url = "https://developer.nvidia.com/compute/cuda/9.1/Prod/local_installers/cuda_9.1.85_387.26_linux";
256 sha256 = "0lz9bwhck1ax4xf1fyb5nicb7l1kssslj518z64iirpy2qmwg5l4";
257 runPatches = [
258 (fetchurl {
259 url = "https://developer.nvidia.com/compute/cuda/9.1/Prod/patches/1/cuda_9.1.85.1_linux";
260 sha256 = "1f53ij5nb7g0vb5pcpaqvkaj1x4mfq3l0mhkfnqbk8sfrvby775g";
261 })
262 (fetchurl {
263 url = "https://developer.nvidia.com/compute/cuda/9.1/Prod/patches/2/cuda_9.1.85.2_linux";
264 sha256 = "16g0w09h3bqmas4hy1m0y6j5ffyharslw52fn25gql57bfihg7ym";
265 })
266 (fetchurl {
267 url = "https://developer.nvidia.com/compute/cuda/9.1/Prod/patches/3/cuda_9.1.85.3_linux";
268 sha256 = "12mcv6f8z33z8y41ja8bv5p5iqhv2vx91mv3b5z6fcj7iqv98422";
269 })
270 ];
271 gcc = gcc6;
272 };
273
274 cudatoolkit_9_2 = common {
275 version = "9.2.148.1";
276 url = "https://developer.nvidia.com/compute/cuda/9.2/Prod2/local_installers/cuda_9.2.148_396.37_linux";
277 sha256 = "04c6v9b50l4awsf9w9zj5vnxvmc0hk0ypcfjksbh4vnzrz14wigm";
278 runPatches = [
279 (fetchurl {
280 url = "https://developer.nvidia.com/compute/cuda/9.2/Prod2/patches/1/cuda_9.2.148.1_linux";
281 sha256 = "1kx6l4yzsamk6q1f4vllcpywhbfr2j5wfl4h5zx8v6dgfpsjm2lw";
282 })
283 ];
284 gcc = gcc7;
285 };
286
287 cudatoolkit_9 = cudatoolkit_9_2;
288
289 cudatoolkit_10_0 = common {
290 version = "10.0.130";
291 url = "https://developer.nvidia.com/compute/cuda/10.0/Prod/local_installers/cuda_10.0.130_410.48_linux";
292 sha256 = "16p3bv1lwmyqpxil8r951h385sy9asc578afrc7lssa68c71ydcj";
293
294 gcc = gcc7;
295 };
296
297 cudatoolkit_10 = cudatoolkit_10_0;
298}