Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ 2 lib, 3 stdenv, 4 fetchurl, 5 perl, 6 gcc, 7 ncurses5, 8 ncurses6, 9 gmp, 10 libiconv, 11 numactl, 12 libffi, 13 llvmPackages, 14 coreutils, 15 targetPackages, 16 17 # minimal = true; will remove files that aren't strictly necessary for 18 # regular builds and GHC bootstrapping. 19 # This is "useful" for staying within hydra's output limits for at least the 20 # aarch64-linux architecture. 21 minimal ? false, 22}: 23 24# Prebuilt only does native 25assert stdenv.targetPlatform == stdenv.hostPlatform; 26 27let 28 downloadsUrl = "https://downloads.haskell.org/ghc"; 29 30 # Copy sha256 from https://downloads.haskell.org/~ghc/9.0.2/SHA256SUMS 31 version = "9.0.2"; 32 33 # Information about available bindists that we use in the build. 34 # 35 # # Bindist library checking 36 # 37 # The field `archSpecificLibraries` also provides a way for us get notified 38 # early when the upstream bindist changes its dependencies (e.g. because a 39 # newer Debian version is used that uses a new `ncurses` version). 40 # 41 # Usage: 42 # 43 # * You can find the `fileToCheckFor` of libraries by running `readelf -d` 44 # on the compiler binary (`exePathForLibraryCheck`). 45 # * To skip library checking for an architecture, 46 # set `exePathForLibraryCheck = null`. 47 # * To skip file checking for a specific arch specific library, 48 # set `fileToCheckFor = null`. 49 ghcBinDists = { 50 # Binary distributions for the default libc (e.g. glibc, or libSystem on Darwin) 51 # nixpkgs uses for the respective system. 52 defaultLibc = { 53 i686-linux = { 54 variantSuffix = ""; 55 src = { 56 url = "${downloadsUrl}/${version}/ghc-${version}-i386-deb9-linux.tar.xz"; 57 sha256 = "fdeb9f8928fbe994064778a8e1e85bb1a58a6cd3dd7b724fcc2a1dcfda6cad47"; 58 }; 59 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2"; 60 archSpecificLibraries = [ 61 { 62 nixPackage = gmp; 63 fileToCheckFor = null; 64 } 65 # The i686-linux bindist provided by GHC HQ is currently built on Debian 9, 66 # which link it against `libtinfo.so.5` (ncurses 5). 67 # Other bindists are linked `libtinfo.so.6` (ncurses 6). 68 { 69 nixPackage = ncurses5; 70 fileToCheckFor = "libtinfo.so.5"; 71 } 72 ]; 73 }; 74 x86_64-linux = { 75 variantSuffix = ""; 76 src = { 77 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-deb10-linux.tar.xz"; 78 sha256 = "5d0b9414b10cfb918453bcd01c5ea7a1824fe95948b08498d6780f20ba247afc"; 79 }; 80 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2"; 81 archSpecificLibraries = [ 82 { 83 nixPackage = gmp; 84 fileToCheckFor = null; 85 } 86 { 87 nixPackage = ncurses6; 88 fileToCheckFor = "libtinfo.so.6"; 89 } 90 { 91 nixPackage = numactl; 92 fileToCheckFor = null; 93 } 94 ]; 95 }; 96 aarch64-linux = { 97 variantSuffix = ""; 98 src = { 99 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-deb10-linux.tar.xz"; 100 sha256 = "cb016344c70a872738a24af60bd15d3b18749087b9905c1b3f1b1549dc01f46d"; 101 }; 102 exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2"; 103 archSpecificLibraries = [ 104 { 105 nixPackage = gmp; 106 fileToCheckFor = null; 107 } 108 { 109 nixPackage = ncurses6; 110 fileToCheckFor = "libtinfo.so.6"; 111 } 112 { 113 nixPackage = numactl; 114 fileToCheckFor = null; 115 } 116 ]; 117 }; 118 x86_64-darwin = { 119 variantSuffix = ""; 120 src = { 121 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-apple-darwin.tar.xz"; 122 sha256 = "e1fe990eb987f5c4b03e0396f9c228a10da71769c8a2bc8fadbc1d3b10a0f53a"; 123 }; 124 exePathForLibraryCheck = null; # we don't have a library check for darwin yet 125 archSpecificLibraries = [ 126 { 127 nixPackage = gmp; 128 fileToCheckFor = null; 129 } 130 { 131 nixPackage = ncurses6; 132 fileToCheckFor = null; 133 } 134 { 135 nixPackage = libiconv; 136 fileToCheckFor = null; 137 } 138 ]; 139 isHadrian = true; 140 }; 141 aarch64-darwin = { 142 variantSuffix = ""; 143 src = { 144 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-apple-darwin.tar.xz"; 145 sha256 = "b1fcab17fe48326d2ff302d70c12bc4cf4d570dfbbce68ab57c719cfec882b05"; 146 }; 147 exePathForLibraryCheck = null; # we don't have a library check for darwin yet 148 archSpecificLibraries = [ 149 { 150 nixPackage = gmp; 151 fileToCheckFor = null; 152 } 153 { 154 nixPackage = ncurses6; 155 fileToCheckFor = null; 156 } 157 { 158 nixPackage = libiconv; 159 fileToCheckFor = null; 160 } 161 ]; 162 isHadrian = true; 163 }; 164 }; 165 # Binary distributions for the musl libc for the respective system. 166 musl = { 167 x86_64-linux = { 168 variantSuffix = "-musl"; 169 src = { 170 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-alpine3.12-linux-gmp.tar.xz"; 171 sha256 = "5bb1e7192c2b9fcff68930dbdc65509d345138e9a43c5d447056a68decc05ec8"; 172 }; 173 exePathForLibraryCheck = "bin/ghc"; 174 archSpecificLibraries = [ 175 { 176 nixPackage = gmp; 177 fileToCheckFor = null; 178 } 179 { 180 nixPackage = ncurses6; 181 fileToCheckFor = "libncursesw.so.6"; 182 } 183 ]; 184 isHadrian = true; 185 }; 186 }; 187 }; 188 189 distSetName = if stdenv.hostPlatform.isMusl then "musl" else "defaultLibc"; 190 191 binDistUsed = 192 ghcBinDists.${distSetName}.${stdenv.hostPlatform.system} 193 or (throw "cannot bootstrap GHC on this platform ('${stdenv.hostPlatform.system}' with libc '${distSetName}')"); 194 195 gmpUsed = 196 (builtins.head ( 197 builtins.filter ( 198 drv: lib.hasPrefix "gmp" (drv.nixPackage.name or "") 199 ) binDistUsed.archSpecificLibraries 200 )).nixPackage; 201 202 useLLVM = !(import ./common-have-ncg.nix { inherit lib stdenv version; }); 203 204 libPath = lib.makeLibraryPath ( 205 # Add arch-specific libraries. 206 map ({ nixPackage, ... }: nixPackage) binDistUsed.archSpecificLibraries 207 ); 208 209 libEnvVar = lib.optionalString stdenv.hostPlatform.isDarwin "DY" + "LD_LIBRARY_PATH"; 210 211 runtimeDeps = [ 212 targetPackages.stdenv.cc 213 targetPackages.stdenv.cc.bintools 214 coreutils # for cat 215 ] 216 ++ lib.optionals useLLVM [ 217 (lib.getBin llvmPackages.llvm) 218 ] 219 # On darwin, we need unwrapped bintools as well (for otool) 220 ++ lib.optionals (stdenv.targetPlatform.linker == "cctools") [ 221 targetPackages.stdenv.cc.bintools.bintools 222 ]; 223 224in 225 226stdenv.mkDerivation { 227 inherit version; 228 pname = "ghc-binary${binDistUsed.variantSuffix}"; 229 230 src = fetchurl binDistUsed.src; 231 232 nativeBuildInputs = [ perl ]; 233 234 # Set LD_LIBRARY_PATH or equivalent so that the programs running as part 235 # of the bindist installer can find the libraries they expect. 236 # Cannot patchelf beforehand due to relative RPATHs that anticipate 237 # the final install location. 238 ${libEnvVar} = libPath; 239 240 postUnpack = 241 # Verify our assumptions of which `libtinfo.so` (ncurses) version is used, 242 # so that we know when ghc bindists upgrade that and we need to update the 243 # version used in `libPath`. 244 lib.optionalString (binDistUsed.exePathForLibraryCheck != null) 245 # Note the `*` glob because some GHCs have a suffix when unpacked, e.g. 246 # the musl bindist has dir `ghc-VERSION-x86_64-unknown-linux/`. 247 # As a result, don't shell-quote this glob when splicing the string. 248 ( 249 let 250 buildExeGlob = ''ghc-${version}*/"${binDistUsed.exePathForLibraryCheck}"''; 251 in 252 lib.concatStringsSep "\n" [ 253 ('' 254 shopt -u nullglob 255 echo "Checking that ghc binary exists in bindist at ${buildExeGlob}" 256 if ! test -e ${buildExeGlob}; then 257 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; 258 fi 259 '') 260 (lib.concatMapStringsSep "\n" ( 261 { fileToCheckFor, nixPackage }: 262 lib.optionalString (fileToCheckFor != null) '' 263 echo "Checking bindist for ${fileToCheckFor} to ensure that is still used" 264 if ! readelf -d ${buildExeGlob} | grep "${fileToCheckFor}"; then 265 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; 266 fi 267 268 echo "Checking that the nix package ${nixPackage} contains ${fileToCheckFor}" 269 if ! test -e "${lib.getLib nixPackage}/lib/${fileToCheckFor}"; then 270 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; 271 fi 272 '' 273 ) binDistUsed.archSpecificLibraries) 274 ] 275 ) 276 # GHC has dtrace probes, which causes ld to try to open /usr/lib/libdtrace.dylib 277 # during linking 278 + lib.optionalString stdenv.hostPlatform.isDarwin '' 279 export NIX_LDFLAGS+=" -no_dtrace_dof" 280 # not enough room in the object files for the full path to libiconv :( 281 for exe in $(find . -type f -executable); do 282 isScript $exe && continue 283 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib 284 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 285 done 286 '' 287 + 288 289 # Some scripts used during the build need to have their shebangs patched 290 '' 291 patchShebangs ghc-${version}/utils/ 292 patchShebangs ghc-${version}/configure 293 test -d ghc-${version}/inplace/bin && \ 294 patchShebangs ghc-${version}/inplace/bin 295 '' 296 + 297 # We have to patch the GMP paths for the integer-gmp package. 298 '' 299 find . -name ghc-bignum.buildinfo \ 300 -exec sed -i "s@extra-lib-dirs: @extra-lib-dirs: ${lib.getLib gmpUsed}/lib@" {} \; 301 302 # we need to modify the package db directly for hadrian bindists 303 find . -name 'ghc-bignum*.conf' \ 304 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib gmpUsed}/lib' -i {} \; 305 '' 306 + lib.optionalString stdenv.hostPlatform.isDarwin '' 307 # we need to modify the package db directly for hadrian bindists 308 # (all darwin bindists are hadrian-based for 9.2.2) 309 find . -name 'base*.conf' \ 310 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib libiconv}/lib' -i {} \; 311 312 # To link RTS in the end we also need libffi now 313 find . -name 'rts*.conf' \ 314 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib libffi}/lib' \ 315 -e 's@/.*/Developer/.*/usr/include/ffi@${lib.getDev libffi}/include@' \ 316 -i {} \; 317 '' 318 + 319 # Some platforms do HAVE_NUMA so -lnuma requires it in library-dirs in rts/package.conf.in 320 # FFI_LIB_DIR is a good indication of places it must be needed. 321 lib.optionalString 322 ( 323 lib.meta.availableOn stdenv.hostPlatform numactl 324 && builtins.any ({ nixPackage, ... }: nixPackage == numactl) binDistUsed.archSpecificLibraries 325 ) 326 '' 327 find . -name package.conf.in \ 328 -exec sed -i "s@FFI_LIB_DIR@FFI_LIB_DIR ${numactl.out}/lib@g" {} \; 329 '' 330 + 331 # Rename needed libraries and binaries, fix interpreter 332 lib.optionalString stdenv.hostPlatform.isLinux '' 333 find . -type f -executable -exec patchelf \ 334 --interpreter ${stdenv.cc.bintools.dynamicLinker} {} \; 335 ''; 336 337 # fix for `configure: error: Your linker is affected by binutils #16177` 338 preConfigure = lib.optionalString stdenv.targetPlatform.isAarch32 "LD=ld.gold"; 339 340 configurePlatforms = [ ]; 341 configureFlags = [ 342 "--with-gmp-includes=${lib.getDev gmpUsed}/include" 343 # Note `--with-gmp-libraries` does nothing for GHC bindists: 344 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6124 345 ] 346 ++ lib.optional stdenv.hostPlatform.isDarwin "--with-gcc=${./gcc-clang-wrapper.sh}" 347 # From: https://github.com/NixOS/nixpkgs/pull/43369/commits 348 ++ lib.optional stdenv.hostPlatform.isMusl "--disable-ld-override"; 349 350 # No building is necessary, but calling make without flags ironically 351 # calls install-strip ... 352 dontBuild = true; 353 354 # GHC tries to remove xattrs when installing to work around Gatekeeper 355 # (see https://gitlab.haskell.org/ghc/ghc/-/issues/17418). This step normally 356 # succeeds in nixpkgs because xattrs are not allowed in the store, but it 357 # can fail when a file has the `com.apple.provenance` xattr, and it can’t be 358 # modified (such as target of the symlink to `libiconv.dylib`). 359 # The `com.apple.provenance` xattr is a new feature of macOS as of macOS 13. 360 # See: https://eclecticlight.co/2023/03/13/ventura-has-changed-app-quarantine-with-a-new-xattr/ 361 makeFlags = lib.optionals stdenv.buildPlatform.isDarwin [ "XATTR=/does-not-exist" ]; 362 363 # Patch scripts to include runtime dependencies in $PATH. 364 postInstall = '' 365 for i in "$out/bin/"*; do 366 test ! -h "$i" || continue 367 isScript "$i" || continue 368 sed -i -e '2i export PATH="${lib.makeBinPath runtimeDeps}:$PATH"' "$i" 369 done 370 '' 371 # On Darwin, GHC doesn't install a bundled libffi.so, but instead uses the 372 # system one (see postUnpack). Due to a bug in Hadrian, the (bundled) libffi 373 # headers are installed anyways. This problem has been fixed in GHC 9.2: 374 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/8189. While the system 375 # header should shadow the GHC installed ones, remove them to be safe. 376 + lib.optionalString (stdenv.hostPlatform.isDarwin && binDistUsed.isHadrian or false) '' 377 echo Deleting redundant libffi headers: 378 find "$out" '(' -name ffi.h -or -name ffitarget.h ')' -print -delete 379 ''; 380 381 # Apparently necessary for the ghc Alpine (musl) bindist: 382 # When we strip, and then run the 383 # patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p 384 # below, running ghc (e.g. during `installCheckPhase)` gives some apparently 385 # corrupted rpath or whatever makes the loader work on nonsensical strings: 386 # running install tests 387 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: : symbol not found 388 # 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 389 # 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 390 # 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 391 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �: symbol not found 392 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �?: symbol not found 393 # 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 394 # This is extremely bogus and should be investigated. 395 dontStrip = if stdenv.hostPlatform.isMusl then true else false; # `if` for explicitness 396 397 # On Linux, use patchelf to modify the executables so that they can 398 # find editline/gmp. 399 postFixup = 400 lib.optionalString (stdenv.hostPlatform.isLinux && !(binDistUsed.isStatic or false)) ( 401 if stdenv.hostPlatform.isAarch64 then 402 # Keep rpath as small as possible on aarch64 for patchelf#244. All Elfs 403 # are 2 directories deep from $out/lib, so pooling symlinks there makes 404 # a short rpath. 405 '' 406 (cd $out/lib; ln -s ${ncurses6.out}/lib/libtinfo.so.6) 407 (cd $out/lib; ln -s ${lib.getLib gmpUsed}/lib/libgmp.so.10) 408 (cd $out/lib; ln -s ${numactl.out}/lib/libnuma.so.1) 409 for p in $(find "$out/lib" -type f -name "*\.so*"); do 410 (cd $out/lib; ln -s $p) 411 done 412 413 for p in $(find "$out/lib" -type f -executable); do 414 if isELF "$p"; then 415 echo "Patchelfing $p" 416 patchelf --set-rpath "\$ORIGIN:\$ORIGIN/../.." $p 417 fi 418 done 419 '' 420 else 421 '' 422 for p in $(find "$out" -type f -executable); do 423 if isELF "$p"; then 424 echo "Patchelfing $p" 425 patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p 426 fi 427 done 428 '' 429 ) 430 + lib.optionalString stdenv.hostPlatform.isDarwin '' 431 # not enough room in the object files for the full path to libiconv :( 432 for exe in $(find "$out" -type f -executable); do 433 isScript $exe && continue 434 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib 435 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 436 done 437 438 for file in $(find "$out" -name setup-config); do 439 substituteInPlace $file --replace /usr/bin/ranlib "$(type -P ranlib)" 440 done 441 '' 442 + lib.optionalString minimal '' 443 # Remove profiling files 444 find $out -type f -name '*.p_o' -delete 445 find $out -type f -name '*.p_hi' -delete 446 find $out -type f -name '*_p.a' -delete 447 # `-f` because e.g. musl bindist does not have this file. 448 rm -f $out/lib/ghc-*/bin/ghc-iserv-prof 449 # Hydra will redistribute this derivation, so we have to keep the docs for 450 # legal reasons (retaining the legal notices etc) 451 # As a last resort we could unpack the docs separately and symlink them in. 452 # They're in $out/share/{doc,man}. 453 '' 454 # Recache package db which needs to happen for Hadrian bindists 455 # where we modify the package db before installing 456 + '' 457 shopt -s nullglob 458 package_db=("$out"/lib/ghc-*/lib/package.conf.d "$out"/lib/ghc-*/package.conf.d) 459 "$out/bin/ghc-pkg" --package-db="$package_db" recache 460 ''; 461 462 # GHC cannot currently produce outputs that are ready for `-pie` linking. 463 # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear. 464 # See: 465 # * https://github.com/NixOS/nixpkgs/issues/129247 466 # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580 467 hardeningDisable = [ "pie" ]; 468 469 doInstallCheck = true; 470 installCheckPhase = '' 471 # Sanity check, can ghc create executables? 472 cd $TMP 473 mkdir test-ghc; cd test-ghc 474 cat > main.hs << EOF 475 {-# LANGUAGE TemplateHaskell #-} 476 module Main where 477 main = putStrLn \$([|"yes"|]) 478 EOF 479 env -i $out/bin/ghc --make main.hs || exit 1 480 echo compilation ok 481 [ $(./main) == "yes" ] 482 ''; 483 484 passthru = { 485 targetPrefix = ""; 486 enableShared = true; 487 488 inherit llvmPackages; 489 490 # Our Cabal compiler name 491 haskellCompilerName = "ghc-${version}"; 492 } 493 # We duplicate binDistUsed here since we have a sensible default even if no bindist is available, 494 # this makes sure that getting the `meta` attribute doesn't throw even on unsupported platforms. 495 // lib.optionalAttrs (ghcBinDists.${distSetName}.${stdenv.hostPlatform.system}.isHadrian or false) { 496 # Normal GHC derivations expose the hadrian derivation used to build them 497 # here. In the case of bindists we just make sure that the attribute exists, 498 # as it is used for checking if a GHC derivation has been built with hadrian. 499 # The isHadrian mechanism will become obsolete with GHCs that use hadrian 500 # exclusively, i.e. 9.6 (and 9.4?). 501 hadrian = null; 502 }; 503 504 meta = rec { 505 homepage = "http://haskell.org/ghc"; 506 description = "Glasgow Haskell Compiler"; 507 license = lib.licenses.bsd3; 508 # HACK: since we can't encode the libc / abi in platforms, we need 509 # to make the platform list dependent on the evaluation platform 510 # in order to avoid eval errors with musl which supports less 511 # platforms than the default libcs (i. e. glibc / libSystem). 512 # This is done for the benefit of Hydra, so `packagePlatforms` 513 # won't return any platforms that would cause an evaluation 514 # failure for `pkgsMusl.haskell.compiler.ghc922Binary`, as 515 # long as the evaluator runs on a platform that supports 516 # `pkgsMusl`. 517 platforms = builtins.attrNames ghcBinDists.${distSetName}; 518 teams = [ lib.teams.haskell ]; 519 }; 520}