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.6.3/SHA256SUMS 31 version = "9.6.3"; 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 = "58be26f8b8f6b5bd8baf5c32abb03e2c4621646b2142fab10e5c7de5af5c50f8"; 58 }; 59 exePathForLibraryCheck = "bin/ghc"; 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-deb11-linux.tar.xz"; 78 sha256 = "c4c0124857265926f1cf22a09d950d7ba989ff94053a4ddf3dcdab5359f4cab7"; 79 }; 80 exePathForLibraryCheck = "bin/ghc"; 81 archSpecificLibraries = [ 82 { 83 nixPackage = gmp; 84 fileToCheckFor = null; 85 } 86 { 87 nixPackage = ncurses6; 88 fileToCheckFor = "libtinfo.so.6"; 89 } 90 ]; 91 }; 92 aarch64-linux = { 93 variantSuffix = ""; 94 src = { 95 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-deb10-linux.tar.xz"; 96 sha256 = "03c389859319f09452081310fc13af7525063ea8930830ef76be2a14b312271e"; 97 }; 98 exePathForLibraryCheck = "bin/ghc"; 99 archSpecificLibraries = [ 100 { 101 nixPackage = gmp; 102 fileToCheckFor = null; 103 } 104 { 105 nixPackage = ncurses6; 106 fileToCheckFor = "libtinfo.so.6"; 107 } 108 { 109 nixPackage = numactl; 110 fileToCheckFor = null; 111 } 112 ]; 113 }; 114 x86_64-darwin = { 115 variantSuffix = ""; 116 src = { 117 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-apple-darwin.tar.xz"; 118 sha256 = "dde46118ab8388fb1066312c097123e93b1dcf6ae366e3370f88ea456382c9db"; 119 }; 120 exePathForLibraryCheck = null; # we don't have a library check for darwin yet 121 archSpecificLibraries = [ 122 { 123 nixPackage = gmp; 124 fileToCheckFor = null; 125 } 126 { 127 nixPackage = ncurses6; 128 fileToCheckFor = null; 129 } 130 { 131 nixPackage = libiconv; 132 fileToCheckFor = null; 133 } 134 ]; 135 }; 136 aarch64-darwin = { 137 variantSuffix = ""; 138 src = { 139 url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-apple-darwin.tar.xz"; 140 sha256 = "e1cdf458926b2eaf52d2a8287d99a965040ff9051171f5c3b7467049cf0eb213"; 141 }; 142 exePathForLibraryCheck = null; # we don't have a library check for darwin yet 143 archSpecificLibraries = [ 144 { 145 nixPackage = gmp; 146 fileToCheckFor = null; 147 } 148 { 149 nixPackage = ncurses6; 150 fileToCheckFor = null; 151 } 152 { 153 nixPackage = libiconv; 154 fileToCheckFor = null; 155 } 156 ]; 157 }; 158 }; 159 # Binary distributions for the musl libc for the respective system. 160 musl = { 161 x86_64-linux = { 162 variantSuffix = "-musl"; 163 src = { 164 url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-alpine3_12-linux.tar.xz"; 165 sha256 = "8f457af0aa40127049c11134c8793f64351a446e87da1f8ec256e1279b5ab61f"; 166 }; 167 exePathForLibraryCheck = "bin/ghc"; 168 archSpecificLibraries = [ 169 { 170 nixPackage = gmp; 171 fileToCheckFor = null; 172 } 173 { 174 nixPackage = ncurses6; 175 fileToCheckFor = "libncursesw.so.6"; 176 } 177 ]; 178 }; 179 }; 180 }; 181 182 distSetName = if stdenv.hostPlatform.isMusl then "musl" else "defaultLibc"; 183 184 binDistUsed = 185 ghcBinDists.${distSetName}.${stdenv.hostPlatform.system} 186 or (throw "cannot bootstrap GHC on this platform ('${stdenv.hostPlatform.system}' with libc '${distSetName}')"); 187 188 gmpUsed = 189 (builtins.head ( 190 builtins.filter ( 191 drv: lib.hasPrefix "gmp" (drv.nixPackage.name or "") 192 ) binDistUsed.archSpecificLibraries 193 )).nixPackage; 194 195 useLLVM = !(import ./common-have-ncg.nix { inherit lib stdenv version; }); 196 197 libPath = lib.makeLibraryPath ( 198 # Add arch-specific libraries. 199 map ({ nixPackage, ... }: nixPackage) binDistUsed.archSpecificLibraries 200 ); 201 202 libEnvVar = lib.optionalString stdenv.hostPlatform.isDarwin "DY" + "LD_LIBRARY_PATH"; 203 204 runtimeDeps = [ 205 targetPackages.stdenv.cc 206 targetPackages.stdenv.cc.bintools 207 coreutils # for cat 208 ] 209 ++ lib.optionals useLLVM [ 210 (lib.getBin llvmPackages.llvm) 211 ] 212 # On darwin, we need unwrapped bintools as well (for otool) 213 ++ lib.optionals (stdenv.targetPlatform.linker == "cctools") [ 214 targetPackages.stdenv.cc.bintools.bintools 215 ]; 216 217in 218 219stdenv.mkDerivation { 220 inherit version; 221 pname = "ghc-binary${binDistUsed.variantSuffix}"; 222 223 src = fetchurl binDistUsed.src; 224 225 nativeBuildInputs = [ perl ]; 226 227 # Set LD_LIBRARY_PATH or equivalent so that the programs running as part 228 # of the bindist installer can find the libraries they expect. 229 # Cannot patchelf beforehand due to relative RPATHs that anticipate 230 # the final install location. 231 ${libEnvVar} = libPath; 232 233 postUnpack = 234 # Verify our assumptions of which `libtinfo.so` (ncurses) version is used, 235 # so that we know when ghc bindists upgrade that and we need to update the 236 # version used in `libPath`. 237 lib.optionalString (binDistUsed.exePathForLibraryCheck != null) 238 # Note the `*` glob because some GHCs have a suffix when unpacked, e.g. 239 # the musl bindist has dir `ghc-VERSION-x86_64-unknown-linux/`. 240 # As a result, don't shell-quote this glob when splicing the string. 241 ( 242 let 243 buildExeGlob = ''ghc-${version}*/"${binDistUsed.exePathForLibraryCheck}"''; 244 in 245 lib.concatStringsSep "\n" [ 246 ('' 247 shopt -u nullglob 248 echo "Checking that ghc binary exists in bindist at ${buildExeGlob}" 249 if ! test -e ${buildExeGlob}; then 250 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; 251 fi 252 '') 253 (lib.concatMapStringsSep "\n" ( 254 { fileToCheckFor, nixPackage }: 255 lib.optionalString (fileToCheckFor != null) '' 256 echo "Checking bindist for ${fileToCheckFor} to ensure that is still used" 257 if ! readelf -d ${buildExeGlob} | grep "${fileToCheckFor}"; then 258 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; 259 fi 260 261 echo "Checking that the nix package ${nixPackage} contains ${fileToCheckFor}" 262 if ! test -e "${lib.getLib nixPackage}/lib/${fileToCheckFor}"; then 263 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; 264 fi 265 '' 266 ) binDistUsed.archSpecificLibraries) 267 ] 268 ) 269 # GHC has dtrace probes, which causes ld to try to open /usr/lib/libdtrace.dylib 270 # during linking 271 + lib.optionalString stdenv.hostPlatform.isDarwin '' 272 export NIX_LDFLAGS+=" -no_dtrace_dof" 273 # not enough room in the object files for the full path to libiconv :( 274 for exe in $(find . -type f -executable); do 275 isMachO $exe || continue 276 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib 277 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 278 done 279 '' 280 281 # We have to patch the GMP paths for the ghc-bignum package, for hadrian by 282 # modifying the package-db directly 283 + '' 284 find . -name 'ghc-bignum*.conf' \ 285 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib gmpUsed}/lib' -i {} \; 286 '' 287 # Similar for iconv and libffi on darwin 288 + lib.optionalString stdenv.hostPlatform.isDarwin '' 289 find . -name 'base*.conf' \ 290 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib libiconv}/lib' -i {} \; 291 292 # To link RTS in the end we also need libffi now 293 find . -name 'rts*.conf' \ 294 -exec sed -e '/^[a-z-]*library-dirs/a \ ${lib.getLib libffi}/lib' \ 295 -e 's@/Library/Developer/.*/usr/include/ffi@${lib.getDev libffi}/include@' \ 296 -i {} \; 297 '' 298 + 299 # Some platforms do HAVE_NUMA so -lnuma requires it in library-dirs in rts/package.conf.in 300 # FFI_LIB_DIR is a good indication of places it must be needed. 301 lib.optionalString 302 ( 303 lib.meta.availableOn stdenv.hostPlatform numactl 304 && builtins.any ({ nixPackage, ... }: nixPackage == numactl) binDistUsed.archSpecificLibraries 305 ) 306 '' 307 find . -name package.conf.in \ 308 -exec sed -i "s@FFI_LIB_DIR@FFI_LIB_DIR ${numactl.out}/lib@g" {} \; 309 '' 310 + 311 # Rename needed libraries and binaries, fix interpreter 312 lib.optionalString stdenv.hostPlatform.isLinux '' 313 find . -type f -executable -exec patchelf \ 314 --interpreter ${stdenv.cc.bintools.dynamicLinker} {} \; 315 ''; 316 317 # fix for `configure: error: Your linker is affected by binutils #16177` 318 preConfigure = lib.optionalString stdenv.targetPlatform.isAarch32 "LD=ld.gold"; 319 320 # GHC has a patched config.sub and bindists' platforms should always work 321 dontUpdateAutotoolsGnuConfigScripts = true; 322 323 configurePlatforms = [ ]; 324 configureFlags = 325 lib.optional stdenv.hostPlatform.isDarwin "--with-gcc=${./gcc-clang-wrapper.sh}" 326 # From: https://github.com/NixOS/nixpkgs/pull/43369/commits 327 ++ lib.optional stdenv.hostPlatform.isMusl "--disable-ld-override"; 328 329 # No building is necessary, but calling make without flags ironically 330 # calls install-strip ... 331 dontBuild = true; 332 333 # GHC tries to remove xattrs when installing to work around Gatekeeper 334 # (see https://gitlab.haskell.org/ghc/ghc/-/issues/17418). This step normally 335 # succeeds in nixpkgs because xattrs are not allowed in the store, but it 336 # can fail when a file has the `com.apple.provenance` xattr, and it can’t be 337 # modified (such as target of the symlink to `libiconv.dylib`). 338 # The `com.apple.provenance` xattr is a new feature of macOS as of macOS 13. 339 # See: https://eclecticlight.co/2023/03/13/ventura-has-changed-app-quarantine-with-a-new-xattr/ 340 makeFlags = lib.optionals stdenv.buildPlatform.isDarwin [ "XATTR=/does-not-exist" ]; 341 342 # Patch scripts to include runtime dependencies in $PATH. 343 postInstall = '' 344 for i in "$out/bin/"*; do 345 test ! -h "$i" || continue 346 isScript "$i" || continue 347 sed -i -e '2i export PATH="${lib.makeBinPath runtimeDeps}:$PATH"' "$i" 348 done 349 '' 350 + lib.optionalString stdenv.targetPlatform.isDarwin '' 351 # Work around building with binary GHC on Darwin due to GHCs use of `ar -L` when it 352 # detects `llvm-ar` even though the resulting archives are not supported by ld64. 353 # https://gitlab.haskell.org/ghc/ghc/-/issues/23188 354 # https://github.com/haskell/cabal/issues/8882 355 sed -i -e 's/,("ar supports -L", "YES")/,("ar supports -L", "NO")/' "$out/lib/ghc-${version}/lib/settings" 356 ''; 357 358 # Apparently necessary for the ghc Alpine (musl) bindist: 359 # When we strip, and then run the 360 # patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p 361 # below, running ghc (e.g. during `installCheckPhase)` gives some apparently 362 # corrupted rpath or whatever makes the loader work on nonsensical strings: 363 # running install tests 364 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: : symbol not found 365 # 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 366 # 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 367 # 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 368 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �: symbol not found 369 # Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �?: symbol not found 370 # 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 371 # This is extremely bogus and should be investigated. 372 dontStrip = if stdenv.hostPlatform.isMusl then true else false; # `if` for explicitness 373 374 # On Linux, use patchelf to modify the executables so that they can 375 # find editline/gmp. 376 postFixup = 377 lib.optionalString (stdenv.hostPlatform.isLinux && !(binDistUsed.isStatic or false)) ( 378 if stdenv.hostPlatform.isAarch64 then 379 # Keep rpath as small as possible on aarch64 for patchelf#244. All Elfs 380 # are 2 directories deep from $out/lib, so pooling symlinks there makes 381 # a short rpath. 382 '' 383 (cd $out/lib; ln -s ${ncurses6.out}/lib/libtinfo.so.6) 384 (cd $out/lib; ln -s ${lib.getLib gmpUsed}/lib/libgmp.so.10) 385 (cd $out/lib; ln -s ${numactl.out}/lib/libnuma.so.1) 386 for p in $(find "$out/lib" -type f -name "*\.so*"); do 387 (cd $out/lib; ln -s $p) 388 done 389 390 for p in $(find "$out/lib" -type f -executable); do 391 if isELF "$p"; then 392 echo "Patchelfing $p" 393 patchelf --set-rpath "\$ORIGIN:\$ORIGIN/../.." $p 394 fi 395 done 396 '' 397 else 398 '' 399 for p in $(find "$out" -type f -executable); do 400 if isELF "$p"; then 401 echo "Patchelfing $p" 402 patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p 403 fi 404 done 405 '' 406 ) 407 + lib.optionalString stdenv.hostPlatform.isDarwin '' 408 # not enough room in the object files for the full path to libiconv :( 409 for exe in $(find "$out" -type f -executable); do 410 isMachO $exe || continue 411 ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib 412 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 413 done 414 415 for file in $(find "$out" -name setup-config); do 416 substituteInPlace $file --replace /usr/bin/ranlib "$(type -P ranlib)" 417 done 418 '' 419 # Recache package db which needs to happen for Hadrian bindists 420 # where we modify the package db before installing 421 + '' 422 package_db=("$out"/lib/ghc-*/lib/package.conf.d) 423 "$out/bin/ghc-pkg" --package-db="$package_db" recache 424 ''; 425 426 # GHC cannot currently produce outputs that are ready for `-pie` linking. 427 # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear. 428 # See: 429 # * https://github.com/NixOS/nixpkgs/issues/129247 430 # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580 431 hardeningDisable = [ "pie" ]; 432 433 doInstallCheck = true; 434 installCheckPhase = '' 435 # Sanity check, can ghc create executables? 436 cd $TMP 437 mkdir test-ghc; cd test-ghc 438 cat > main.hs << EOF 439 {-# LANGUAGE TemplateHaskell #-} 440 module Main where 441 main = putStrLn \$([|"yes"|]) 442 EOF 443 env -i $out/bin/ghc --make main.hs || exit 1 444 echo compilation ok 445 [ $(./main) == "yes" ] 446 ''; 447 448 passthru = { 449 targetPrefix = ""; 450 enableShared = true; 451 452 inherit llvmPackages; 453 454 # Our Cabal compiler name 455 haskellCompilerName = "ghc-${version}"; 456 457 # Normal GHC derivations expose the hadrian derivation used to build them 458 # here. In the case of bindists we just make sure that the attribute exists, 459 # as it is used for checking if a GHC derivation has been built with hadrian. 460 hadrian = null; 461 }; 462 463 meta = rec { 464 homepage = "http://haskell.org/ghc"; 465 description = "Glasgow Haskell Compiler"; 466 license = lib.licenses.bsd3; 467 # HACK: since we can't encode the libc / abi in platforms, we need 468 # to make the platform list dependent on the evaluation platform 469 # in order to avoid eval errors with musl which supports less 470 # platforms than the default libcs (i. e. glibc / libSystem). 471 # This is done for the benefit of Hydra, so `packagePlatforms` 472 # won't return any platforms that would cause an evaluation 473 # failure for `pkgsMusl.haskell.compiler.ghc922Binary`, as 474 # long as the evaluator runs on a platform that supports 475 # `pkgsMusl`. 476 platforms = builtins.attrNames ghcBinDists.${distSetName}; 477 teams = [ lib.teams.haskell ]; 478 }; 479}