nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv
2, fetchurl, perl, gcc
3, ncurses5
4, ncurses6, gmp, libiconv, numactl
5, llvmPackages
6
7 # minimal = true; will remove files that aren't strictly necessary for
8 # regular builds and GHC bootstrapping.
9 # This is "useful" for staying within hydra's output limits for at least the
10 # aarch64-linux architecture.
11, minimal ? false
12}:
13
14# Prebuilt only does native
15assert stdenv.targetPlatform == stdenv.hostPlatform;
16
17let
18 downloadsUrl = "https://downloads.haskell.org/ghc";
19
20 # Copy sha256 from https://downloads.haskell.org/~ghc/8.10.7/SHA256SUMS
21 version = "8.10.7";
22
23 # Information about available bindists that we use in the build.
24 #
25 # # Bindist library checking
26 #
27 # The field `archSpecificLibraries` also provides a way for us get notified
28 # early when the upstream bindist changes its dependencies (e.g. because a
29 # newer Debian version is used that uses a new `ncurses` version).
30 #
31 # Usage:
32 #
33 # * You can find the `fileToCheckFor` of libraries by running `readelf -d`
34 # on the compiler binary (`exePathForLibraryCheck`).
35 # * To skip library checking for an architecture,
36 # set `exePathForLibraryCheck = null`.
37 # * To skip file checking for a specific arch specfic library,
38 # set `fileToCheckFor = null`.
39 ghcBinDists = {
40 # Binary distributions for the default libc (e.g. glibc, or libSystem on Darwin)
41 # nixpkgs uses for the respective system.
42 defaultLibc = {
43 i686-linux = {
44 variantSuffix = "";
45 src = {
46 url = "${downloadsUrl}/${version}/ghc-${version}-i386-deb9-linux.tar.xz";
47 sha256 = "fbfc1ef194f4e7a4c0da8c11cc69b17458a4b928b609b3622c97acc4acd5c5ab";
48 };
49 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
50 archSpecificLibraries = [
51 { nixPackage = gmp; fileToCheckFor = null; }
52 # The i686-linux bindist provided by GHC HQ is currently built on Debian 9,
53 # which link it against `libtinfo.so.5` (ncurses 5).
54 # Other bindists are linked `libtinfo.so.6` (ncurses 6).
55 { nixPackage = ncurses5; fileToCheckFor = "libtinfo.so.5"; }
56 ];
57 };
58 x86_64-linux = {
59 variantSuffix = "";
60 src = {
61 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-deb10-linux.tar.xz";
62 sha256 = "a13719bca87a0d3ac0c7d4157a4e60887009a7f1a8dbe95c4759ec413e086d30";
63 };
64 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
65 archSpecificLibraries = [
66 { nixPackage = gmp; fileToCheckFor = null; }
67 { nixPackage = ncurses6; fileToCheckFor = "libtinfo.so.6"; }
68 ];
69 };
70 armv7l-linux = {
71 variantSuffix = "";
72 src = {
73 url = "${downloadsUrl}/${version}/ghc-${version}-armv7-deb10-linux.tar.xz";
74 sha256 = "3949c31bdf7d3b4afb765ea8246bca4ca9707c5d988d9961a244f0da100956a2";
75 };
76 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
77 archSpecificLibraries = [
78 { nixPackage = gmp; fileToCheckFor = null; }
79 { nixPackage = ncurses6; fileToCheckFor = "libtinfo.so.6"; }
80 ];
81 };
82 aarch64-linux = {
83 variantSuffix = "";
84 src = {
85 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-deb10-linux.tar.xz";
86 sha256 = "fad2417f9b295233bf8ade79c0e6140896359e87be46cb61cd1d35863d9d0e55";
87 };
88 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
89 archSpecificLibraries = [
90 { nixPackage = gmp; fileToCheckFor = null; }
91 { nixPackage = ncurses6; fileToCheckFor = "libtinfo.so.6"; }
92 { nixPackage = numactl; fileToCheckFor = null; }
93 ];
94 };
95 x86_64-darwin = {
96 variantSuffix = "";
97 src = {
98 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-apple-darwin.tar.xz";
99 sha256 = "287db0f9c338c9f53123bfa8731b0996803ee50f6ee847fe388092e5e5132047";
100 };
101 exePathForLibraryCheck = null; # we don't have a library check for darwin yet
102 archSpecificLibraries = [
103 { nixPackage = gmp; fileToCheckFor = null; }
104 { nixPackage = ncurses6; fileToCheckFor = null; }
105 { nixPackage = libiconv; fileToCheckFor = null; }
106 ];
107 };
108 aarch64-darwin = {
109 variantSuffix = "";
110 src = {
111 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-apple-darwin.tar.xz";
112 sha256 = "dc469fc3c35fd2a33a5a575ffce87f13de7b98c2d349a41002e200a56d9bba1c";
113 };
114 exePathForLibraryCheck = null; # we don't have a library check for darwin yet
115 archSpecificLibraries = [
116 { nixPackage = gmp; fileToCheckFor = null; }
117 { nixPackage = ncurses6; fileToCheckFor = null; }
118 { nixPackage = libiconv; fileToCheckFor = null; }
119 ];
120 };
121 };
122 # Binary distributions for the musl libc for the respective system.
123 musl = {
124 x86_64-linux = {
125 variantSuffix = "-musl-integer-simple";
126 src = {
127 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-alpine3.10-linux-integer-simple.tar.xz";
128 sha256 = "16903df850ef73d5246f2ff169cbf57ecab76c2ac5acfa9928934282cfad575c";
129 };
130 exePathForLibraryCheck = "bin/ghc";
131 archSpecificLibraries = [
132 # No `gmp` here, since this is an `integer-simple` bindist.
133
134 # In contrast to glibc builds, the musl-bindist uses `libncursesw.so.*`
135 # instead of `libtinfo.so.*.`
136 { nixPackage = ncurses6; fileToCheckFor = "libncursesw.so.6"; }
137 ];
138 };
139 };
140 };
141
142 distSetName = if stdenv.hostPlatform.isMusl then "musl" else "defaultLibc";
143
144 binDistUsed = ghcBinDists.${distSetName}.${stdenv.hostPlatform.system}
145 or (throw "cannot bootstrap GHC on this platform ('${stdenv.hostPlatform.system}' with libc '${distSetName}')");
146
147 useLLVM = !stdenv.targetPlatform.isx86;
148
149 libPath =
150 lib.makeLibraryPath (
151 # Add arch-specific libraries.
152 map ({ nixPackage, ... }: nixPackage) binDistUsed.archSpecificLibraries
153 );
154
155 libEnvVar = lib.optionalString stdenv.hostPlatform.isDarwin "DY"
156 + "LD_LIBRARY_PATH";
157
158in
159
160stdenv.mkDerivation rec {
161 inherit version;
162 pname = "ghc-binary${binDistUsed.variantSuffix}";
163
164 src = fetchurl binDistUsed.src;
165
166 # Note that for GHC 8.10 versions >= 8.10.6, the GHC HQ musl bindist
167 # uses `integer-simple` and has no `gmp` dependency:
168 # https://gitlab.haskell.org/ghc/ghc/-/commit/8306501020cd66f683ad9c215fa8e16c2d62357d
169 # Related nixpkgs issues:
170 # * https://github.com/NixOS/nixpkgs/pull/130441#issuecomment-922452843
171 # TODO: When this file is copied to `ghc-9.*-binary.nix`, determine whether
172 # the GHC 9 branch also switched from `gmp` to `integer-simple` via the
173 # currently-open issue:
174 # https://gitlab.haskell.org/ghc/ghc/-/issues/20059
175 # and update this comment accordingly.
176
177 nativeBuildInputs = [ perl ];
178 propagatedBuildInputs =
179 lib.optionals useLLVM [ llvmPackages.llvm ]
180 ;
181
182 # Set LD_LIBRARY_PATH or equivalent so that the programs running as part
183 # of the bindist installer can find the libraries they expect.
184 # Cannot patchelf beforehand due to relative RPATHs that anticipate
185 # the final install location.
186 ${libEnvVar} = libPath;
187
188 postUnpack =
189 # Verify our assumptions of which `libtinfo.so` (ncurses) version is used,
190 # so that we know when ghc bindists upgrade that and we need to update the
191 # version used in `libPath`.
192 lib.optionalString
193 (binDistUsed.exePathForLibraryCheck != null)
194 # Note the `*` glob because some GHCs have a suffix when unpacked, e.g.
195 # the musl bindist has dir `ghc-VERSION-x86_64-unknown-linux/`.
196 # As a result, don't shell-quote this glob when splicing the string.
197 (let buildExeGlob = ''ghc-${version}*/"${binDistUsed.exePathForLibraryCheck}"''; in
198 lib.concatStringsSep "\n" [
199 (''
200 echo "Checking that ghc binary exists in bindist at ${buildExeGlob}"
201 if ! test -e ${buildExeGlob}; then
202 echo >&2 "GHC binary ${binDistUsed.exePathForLibraryCheck} could not be found in the bindist build directory (at ${buildExeGlob}) for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1;
203 fi
204 '')
205 (lib.concatMapStringsSep
206 "\n"
207 ({ fileToCheckFor, nixPackage }:
208 lib.optionalString (fileToCheckFor != null) ''
209 echo "Checking bindist for ${fileToCheckFor} to ensure that is still used"
210 if ! readelf -d ${buildExeGlob} | grep "${fileToCheckFor}"; then
211 echo >&2 "File ${fileToCheckFor} could not be found in ${binDistUsed.exePathForLibraryCheck} for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1;
212 fi
213
214 echo "Checking that the nix package ${nixPackage} contains ${fileToCheckFor}"
215 if ! test -e "${lib.getLib nixPackage}/lib/${fileToCheckFor}"; then
216 echo >&2 "Nix package ${nixPackage} did not contain ${fileToCheckFor} for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1;
217 fi
218 ''
219 )
220 binDistUsed.archSpecificLibraries
221 )
222 ])
223 # GHC has dtrace probes, which causes ld to try to open /usr/lib/libdtrace.dylib
224 # during linking
225 + lib.optionalString stdenv.isDarwin ''
226 export NIX_LDFLAGS+=" -no_dtrace_dof"
227 # not enough room in the object files for the full path to libiconv :(
228 for exe in $(find . -type f -executable); do
229 isScript $exe && continue
230 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib
231 install_name_tool -change /usr/lib/libiconv.2.dylib @executable_path/libiconv.dylib -change /usr/local/lib/gcc/6/libgcc_s.1.dylib ${gcc.cc.lib}/lib/libgcc_s.1.dylib $exe
232 done
233 '' +
234
235 # Some scripts used during the build need to have their shebangs patched
236 ''
237 patchShebangs ghc-${version}/utils/
238 patchShebangs ghc-${version}/configure
239 '' +
240 # We have to patch the GMP paths for the integer-gmp package.
241 # Note that musl bindists do not contain them,
242 # see: https://gitlab.haskell.org/ghc/ghc/-/issues/20073#note_363231
243 # However, musl bindists >= 8.10.6 use `integer-simple`, not `gmp`.
244 ''
245 find . -name integer-gmp.buildinfo \
246 -exec sed -i "s@extra-lib-dirs: @extra-lib-dirs: ${gmp.out}/lib@" {} \;
247 '' + lib.optionalString stdenv.isDarwin ''
248 find . -name base.buildinfo \
249 -exec sed -i "s@extra-lib-dirs: @extra-lib-dirs: ${libiconv}/lib@" {} \;
250 '' +
251 # aarch64 does HAVE_NUMA so -lnuma requires it in library-dirs in rts/package.conf.in
252 # FFI_LIB_DIR is a good indication of places it must be needed.
253 lib.optionalString (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64) ''
254 find . -name package.conf.in \
255 -exec sed -i "s@FFI_LIB_DIR@FFI_LIB_DIR ${numactl.out}/lib@g" {} \;
256 '' +
257 # Rename needed libraries and binaries, fix interpreter
258 lib.optionalString stdenv.isLinux ''
259 find . -type f -executable -exec patchelf \
260 --interpreter ${stdenv.cc.bintools.dynamicLinker} {} \;
261 '';
262
263 # fix for `configure: error: Your linker is affected by binutils #16177`
264 preConfigure = lib.optionalString
265 stdenv.targetPlatform.isAarch32
266 "LD=ld.gold";
267
268 configurePlatforms = [ ];
269 configureFlags = [
270 "--with-gmp-includes=${lib.getDev gmp}/include"
271 # Note `--with-gmp-libraries` does nothing for GHC bindists:
272 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6124
273 ] ++ lib.optional stdenv.isDarwin "--with-gcc=${./gcc-clang-wrapper.sh}"
274 # From: https://github.com/NixOS/nixpkgs/pull/43369/commits
275 ++ lib.optional stdenv.hostPlatform.isMusl "--disable-ld-override";
276
277 # No building is necessary, but calling make without flags ironically
278 # calls install-strip ...
279 dontBuild = true;
280
281 # Apparently necessary for the ghc Alpine (musl) bindist:
282 # When we strip, and then run the
283 # patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p
284 # below, running ghc (e.g. during `installCheckPhase)` gives some apparently
285 # corrupted rpath or whatever makes the loader work on nonsensical strings:
286 # running install tests
287 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: : symbol not found
288 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: ir6zf6c9f86pfx8sr30n2vjy-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/../lib/x86_64-linux-ghc-8.10.5/libHSexceptions-0.10.4-ghc8.10.5.so: symbol not found
289 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: y/lib/ghc-8.10.5/bin/../lib/x86_64-linux-ghc-8.10.5/libHStemplate-haskell-2.16.0.0-ghc8.10.5.so: symbol not found
290 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: 8.10.5/libHStemplate-haskell-2.16.0.0-ghc8.10.5.so: symbol not found
291 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �: symbol not found
292 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �?: symbol not found
293 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: 64-linux-ghc-8.10.5/libHSexceptions-0.10.4-ghc8.10.5.so: symbol not found
294 # This is extremely bogus and should be investigated.
295 dontStrip = if stdenv.hostPlatform.isMusl then true else false; # `if` for explicitness
296
297 # On Linux, use patchelf to modify the executables so that they can
298 # find editline/gmp.
299 postFixup = lib.optionalString stdenv.isLinux
300 (if stdenv.hostPlatform.isAarch64 then
301 # Keep rpath as small as possible on aarch64 for patchelf#244. All Elfs
302 # are 2 directories deep from $out/lib, so pooling symlinks there makes
303 # a short rpath.
304 ''
305 (cd $out/lib; ln -s ${ncurses6.out}/lib/libtinfo.so.6)
306 (cd $out/lib; ln -s ${gmp.out}/lib/libgmp.so.10)
307 (cd $out/lib; ln -s ${numactl.out}/lib/libnuma.so.1)
308 for p in $(find "$out/lib" -type f -name "*\.so*"); do
309 (cd $out/lib; ln -s $p)
310 done
311
312 for p in $(find "$out/lib" -type f -executable); do
313 if isELF "$p"; then
314 echo "Patchelfing $p"
315 patchelf --set-rpath "\$ORIGIN:\$ORIGIN/../.." $p
316 fi
317 done
318 ''
319 else
320 ''
321 for p in $(find "$out" -type f -executable); do
322 if isELF "$p"; then
323 echo "Patchelfing $p"
324 patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p
325 fi
326 done
327 '') + lib.optionalString stdenv.isDarwin ''
328 # not enough room in the object files for the full path to libiconv :(
329 for exe in $(find "$out" -type f -executable); do
330 isScript $exe && continue
331 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib
332 install_name_tool -change /usr/lib/libiconv.2.dylib @executable_path/libiconv.dylib -change /usr/local/lib/gcc/6/libgcc_s.1.dylib ${gcc.cc.lib}/lib/libgcc_s.1.dylib $exe
333 done
334
335 for file in $(find "$out" -name setup-config); do
336 substituteInPlace $file --replace /usr/bin/ranlib "$(type -P ranlib)"
337 done
338 '' +
339 lib.optionalString minimal ''
340 # Remove profiling files
341 find $out -type f -name '*.p_o' -delete
342 find $out -type f -name '*.p_hi' -delete
343 find $out -type f -name '*_p.a' -delete
344 # `-f` because e.g. musl bindist does not have this file.
345 rm -f $out/lib/ghc-*/bin/ghc-iserv-prof
346 # Hydra will redistribute this derivation, so we have to keep the docs for
347 # legal reasons (retaining the legal notices etc)
348 # As a last resort we could unpack the docs separately and symlink them in.
349 # They're in $out/share/{doc,man}.
350 '';
351
352 # In nixpkgs, musl based builds currently enable `pie` hardening by default
353 # (see `defaultHardeningFlags` in `make-derivation.nix`).
354 # But GHC cannot currently produce outputs that are ready for `-pie` linking.
355 # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear.
356 # See:
357 # * https://github.com/NixOS/nixpkgs/issues/129247
358 # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580
359 hardeningDisable = lib.optional stdenv.targetPlatform.isMusl "pie";
360
361 doInstallCheck = true;
362 installCheckPhase = ''
363 unset ${libEnvVar}
364 # Sanity check, can ghc create executables?
365 cd $TMP
366 mkdir test-ghc; cd test-ghc
367 cat > main.hs << EOF
368 {-# LANGUAGE TemplateHaskell #-}
369 module Main where
370 main = putStrLn \$([|"yes"|])
371 EOF
372 $out/bin/ghc --make main.hs || exit 1
373 echo compilation ok
374 [ $(./main) == "yes" ]
375 '';
376
377 passthru = {
378 targetPrefix = "";
379 enableShared = true;
380
381 # Our Cabal compiler name
382 haskellCompilerName = "ghc-${version}";
383 };
384
385 meta = rec {
386 homepage = "http://haskell.org/ghc";
387 description = "The Glasgow Haskell Compiler";
388 license = lib.licenses.bsd3;
389 # HACK: since we can't encode the libc / abi in platforms, we need
390 # to make the platform list dependent on the evaluation platform
391 # in order to avoid eval errors with musl which supports less
392 # platforms than the default libcs (i. e. glibc / libSystem).
393 # This is done for the benefit of Hydra, so `packagePlatforms`
394 # won't return any platforms that would cause an evaluation
395 # failure for `pkgsMusl.haskell.compiler.ghc8102Binary`, as
396 # long as the evaluator runs on a platform that supports
397 # `pkgsMusl`.
398 platforms = builtins.attrNames ghcBinDists.${distSetName};
399 hydraPlatforms = builtins.filter (p: minimal || p != "aarch64-linux") platforms;
400 maintainers = with lib.maintainers; [
401 prusnak
402 domenkozar
403 ] ++ lib.teams.haskell.members;
404 };
405}