nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at 21.11 153 lines 6.7 kB view raw
1{ lib, stdenv, ghc, llvmPackages, packages, symlinkJoin, makeWrapper 2, withLLVM ? !(stdenv.hostPlatform.isx86_64 || stdenv.hostPlatform.isPowerPC) 3, postBuild ? "" 4, ghcLibdir ? null # only used by ghcjs, when resolving plugins 5}: 6 7assert ghcLibdir != null -> (ghc.isGhcjs or false); 8 9# This wrapper works only with GHC 6.12 or later. 10assert lib.versionOlder "6.12" ghc.version || ghc.isGhcjs || ghc.isHaLVM; 11 12# It's probably a good idea to include the library "ghc-paths" in the 13# compiler environment, because we have a specially patched version of 14# that package in Nix that honors these environment variables 15# 16# NIX_GHC 17# NIX_GHCPKG 18# NIX_GHC_DOCDIR 19# NIX_GHC_LIBDIR 20# 21# instead of hard-coding the paths. The wrapper sets these variables 22# appropriately to configure ghc-paths to point back to the wrapper 23# instead of to the pristine GHC package, which doesn't know any of the 24# additional libraries. 25# 26# A good way to import the environment set by the wrapper below into 27# your shell is to add the following snippet to your ~/.bashrc: 28# 29# if [ -e ~/.nix-profile/bin/ghc ]; then 30# eval $(grep export ~/.nix-profile/bin/ghc) 31# fi 32 33let 34 isGhcjs = ghc.isGhcjs or false; 35 isHaLVM = ghc.isHaLVM or false; 36 ghc761OrLater = isGhcjs || isHaLVM || lib.versionOlder "7.6.1" ghc.version; 37 packageDBFlag = if ghc761OrLater then "--global-package-db" else "--global-conf"; 38 ghcCommand' = if isGhcjs then "ghcjs" else "ghc"; 39 ghcCommand = "${ghc.targetPrefix}${ghcCommand'}"; 40 ghcCommandCaps= lib.toUpper ghcCommand'; 41 libDir = if isHaLVM then "$out/lib/HaLVM-${ghc.version}" 42 else "$out/lib/${ghcCommand}-${ghc.version}"; 43 docDir = "$out/share/doc/ghc/html"; 44 packageCfgDir = "${libDir}/package.conf.d"; 45 paths = lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages); 46 hasLibraries = lib.any (x: x.isHaskellLibrary) paths; 47 # CLang is needed on Darwin for -fllvm to work: 48 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm 49 llvm = lib.makeBinPath 50 ([ llvmPackages.llvm ] 51 ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang); 52in 53if paths == [] && !withLLVM then ghc else 54symlinkJoin { 55 # this makes computing paths from the name attribute impossible; 56 # if such a feature is needed, the real compiler name should be saved 57 # as a dedicated drv attribute, like `compiler-name` 58 name = ghc.name + "-with-packages"; 59 paths = paths ++ [ghc]; 60 nativeBuildInputs = [ makeWrapper ]; 61 postBuild = '' 62 # wrap compiler executables with correct env variables 63 64 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do 65 if [[ -x "${ghc}/bin/$prg" ]]; then 66 rm -f $out/bin/$prg 67 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \ 68 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ 69 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \ 70 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \ 71 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \ 72 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \ 73 ${lib.optionalString (ghc.isGhcjs or false) 74 ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"'' 75 } \ 76 ${lib.optionalString withLLVM ''--prefix "PATH" ":" "${llvm}"''} 77 fi 78 done 79 80 for prg in runghc runhaskell; do 81 if [[ -x "${ghc}/bin/$prg" ]]; then 82 rm -f $out/bin/$prg 83 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \ 84 --add-flags "-f $out/bin/${ghcCommand}" \ 85 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \ 86 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \ 87 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \ 88 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" 89 fi 90 done 91 92 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do 93 if [[ -x "${ghc}/bin/$prg" ]]; then 94 rm -f $out/bin/$prg 95 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "${packageDBFlag}=${packageCfgDir}" 96 fi 97 done 98 99 # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976 100 if [[ -x "${ghc}/bin/haddock" ]]; then 101 rm -f $out/bin/haddock 102 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \ 103 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ 104 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" 105 fi 106 107 '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) '' 108 # Work around a linker limit in macOS Sierra (see generic-builder.nix): 109 local packageConfDir="$out/lib/${ghc.name}/package.conf.d"; 110 local dynamicLinksDir="$out/lib/links" 111 mkdir -p $dynamicLinksDir 112 # Clean up the old links that may have been (transitively) included by 113 # symlinkJoin: 114 rm -f $dynamicLinksDir/* 115 for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do 116 ln -s $d/*.dylib $dynamicLinksDir 117 done 118 for f in $packageConfDir/*.conf; do 119 # Initially, $f is a symlink to a read-only file in one of the inputs 120 # (as a result of this symlinkJoin derivation). 121 # Replace it with a copy whose dynamic-library-dirs points to 122 # $dynamicLinksDir 123 cp $f $f-tmp 124 rm $f 125 sed "N;s,dynamic-library-dirs:\s*.*,dynamic-library-dirs: $dynamicLinksDir," $f-tmp > $f 126 rm $f-tmp 127 done 128 '') + '' 129 ${lib.optionalString hasLibraries '' 130 # GHC 8.10 changes. 131 # Instead of replacing package.cache[.lock] with the new file, 132 # ghc-pkg is now trying to open the file. These file are symlink 133 # to another nix derivation, so they are not writable. Removing 134 # them allow the correct behavior of ghc-pkg recache 135 # See: https://github.com/NixOS/nixpkgs/issues/79441 136 rm $out/lib/${ghc.name}/package.conf.d/package.cache.lock 137 rm $out/lib/${ghc.name}/package.conf.d/package.cache 138 139 $out/bin/${ghcCommand}-pkg recache 140 ''} 141 ${# ghcjs will read the ghc_libdir file when resolving plugins. 142 lib.optionalString (isGhcjs && ghcLibdir != null) '' 143 mkdir -p "${libDir}" 144 rm -f "${libDir}/ghc_libdir" 145 printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir" 146 ''} 147 $out/bin/${ghcCommand}-pkg check 148 '' + postBuild; 149 passthru = { 150 preferLocalBuild = true; 151 inherit (ghc) version meta; 152 }; 153}