1{ lib, stdenv, makeWrapper, fetchurl, requireFile, patchelf, perl, ncurses, expat, python27, zlib
2, gcc48, gcc49, gcc5, gcc6
3, xorg, gtk2, 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
44 runtimeDependencies = [
45 ncurses 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 phases = [ "unpackPhase" "installPhase" "fixupPhase" ];
53
54 unpackPhase = ''
55 sh $src --keep --noexec
56
57 cd pkg/run_files
58 sh cuda-linux*.run --keep --noexec
59 sh cuda-samples*.run --keep --noexec
60 mv pkg ../../$(basename $src)
61 cd ../..
62 rm -rf pkg
63
64 for patch in $runPatches; do
65 sh $patch --keep --noexec
66 mv pkg $(basename $patch)
67 done
68 '';
69
70 installPhase = ''
71 mkdir $out
72 cd $(basename $src)
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 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 '' + lib.optionalString (lib.versionOlder version "8.0") ''
116 # Hack to fix building against recent Glibc/GCC.
117 echo "NIX_CFLAGS_COMPILE+=' -D_FORCE_INLINES'" >> $out/nix-support/setup-hook
118 '';
119
120 preFixup = ''
121 while IFS= read -r -d ''$'\0' i; do
122 if ! isELF "$i"; then continue; fi
123 echo "patching $i..."
124 if [[ ! $i =~ \.so ]]; then
125 patchelf \
126 --set-interpreter "''$(cat $NIX_CC/nix-support/dynamic-linker)" $i
127 fi
128 if [[ $i =~ libcudart ]]; then
129 rpath2=
130 else
131 rpath2=$rpath:$lib/lib:$out/jre/lib/amd64/jli:$out/lib:$out/lib64:$out/nvvm/lib:$out/nvvm/lib64
132 fi
133 patchelf --set-rpath $rpath2 --force-rpath $i
134 done < <(find $out $lib $doc -type f -print0)
135 '';
136
137 passthru = {
138 cc = gcc;
139 majorVersion =
140 let versionParts = lib.splitString "." version;
141 in "${lib.elemAt versionParts 0}.${lib.elemAt versionParts 1}";
142 };
143
144 meta = with stdenv.lib; {
145 description = "A compiler for NVIDIA GPUs, math libraries, and tools";
146 homepage = "https://developer.nvidia.com/cuda-toolkit";
147 platforms = [ "x86_64-linux" ];
148 license = licenses.unfree;
149 };
150 };
151
152in {
153
154 cudatoolkit6 = common {
155 version = "6.0.37";
156 url = "http://developer.download.nvidia.com/compute/cuda/6_0/rel/installers/cuda_6.0.37_linux_64.run";
157 sha256 = "991e436c7a6c94ec67cf44204d136adfef87baa3ded270544fa211179779bc40";
158 gcc = gcc48;
159 };
160
161 cudatoolkit65 = common {
162 version = "6.5.19";
163 url = "http://developer.download.nvidia.com/compute/cuda/6_5/rel/installers/cuda_6.5.19_linux_64.run";
164 sha256 = "1x9zdmk8z784d3d35vr2ak1l4h5v4jfjhpxfi9fl9dvjkcavqyaj";
165 gcc = gcc48;
166 };
167
168 cudatoolkit7 = common {
169 version = "7.0.28";
170 url = "http://developer.download.nvidia.com/compute/cuda/7_0/Prod/local_installers/cuda_7.0.28_linux.run";
171 sha256 = "1km5hpiimx11jcazg0h3mjzk220klwahs2vfqhjavpds5ff2wafi";
172 gcc = gcc49;
173 };
174
175 cudatoolkit75 = common {
176 version = "7.5.18";
177 url = "http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda_7.5.18_linux.run";
178 sha256 = "1v2ylzp34ijyhcxyh5p6i0cwawwbbdhni2l5l4qm21s1cx9ish88";
179 gcc = gcc49;
180 };
181
182 cudatoolkit8 = common {
183 version = "8.0.61.2";
184 url = "https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run";
185 sha256 = "1i4xrsqbad283qffvysn88w2pmxzxbbby41lw0j1113z771akv4w";
186 runPatches = [
187 (fetchurl {
188 url = "https://developer.nvidia.com/compute/cuda/8.0/Prod2/patches/2/cuda_8.0.61.2_linux-run";
189 sha256 = "1iaz5rrsnsb1p99qiqvxn6j3ksc7ry8xlr397kqcjzxqbljbqn9d";
190 })
191 ];
192 gcc = gcc5;
193 };
194
195 cudatoolkit9 = common {
196 version = "9.1.85.1";
197 url = "https://developer.nvidia.com/compute/cuda/9.1/Prod/local_installers/cuda_9.1.85_387.26_linux";
198 sha256 = "0lz9bwhck1ax4xf1fyb5nicb7l1kssslj518z64iirpy2qmwg5l4";
199 runPatches = [
200 (fetchurl {
201 url = "https://developer.nvidia.com/compute/cuda/9.1/Prod/patches/1/cuda_9.1.85.1_linux";
202 sha256 = "1f53ij5nb7g0vb5pcpaqvkaj1x4mfq3l0mhkfnqbk8sfrvby775g";
203 })
204 ];
205 gcc = gcc6;
206 };
207
208}
209