at master 8.1 kB view raw
1{ 2 lib, 3 stdenv, 4 haskellPackages, 5 symlinkJoin, 6 makeWrapper, 7 # GHC will have LLVM available if necessary for the respective target, 8 # so useLLVM only needs to be changed if -fllvm is to be used for a 9 # platform that has NCG support 10 useLLVM ? false, 11 withHoogle ? false, 12 # Whether to install `doc` outputs for GHC and all included libraries. 13 installDocumentation ? true, 14 hoogleWithPackages, 15 postBuild ? "", 16}: 17 18# This argument is a function which selects a list of Haskell packages from any 19# passed Haskell package set. 20# 21# Example: 22# (hpkgs: [ hpkgs.mtl hpkgs.lens ]) 23selectPackages: 24 25# It's probably a good idea to include the library "ghc-paths" in the 26# compiler environment, because we have a specially patched version of 27# that package in Nix that honors these environment variables 28# 29# NIX_GHC 30# NIX_GHCPKG 31# NIX_GHC_DOCDIR 32# NIX_GHC_LIBDIR 33# 34# instead of hard-coding the paths. The wrapper sets these variables 35# appropriately to configure ghc-paths to point back to the wrapper 36# instead of to the pristine GHC package, which doesn't know any of the 37# additional libraries. 38# 39# A good way to import the environment set by the wrapper below into 40# your shell is to add the following snippet to your ~/.bashrc: 41# 42# if [ -e ~/.nix-profile/bin/ghc ]; then 43# eval $(grep export ~/.nix-profile/bin/ghc) 44# fi 45 46let 47 inherit (haskellPackages) ghc; 48 49 hoogleWithPackages' = if withHoogle then hoogleWithPackages selectPackages else null; 50 51 packages = selectPackages haskellPackages ++ [ hoogleWithPackages' ]; 52 53 isHaLVM = ghc.isHaLVM or false; 54 ghcCommand' = "ghc"; 55 ghcCommand = "${ghc.targetPrefix}${ghcCommand'}"; 56 ghcCommandCaps = lib.toUpper ghcCommand'; 57 libDir = 58 if isHaLVM then 59 "$out/lib/HaLVM-${ghc.version}" 60 else 61 "$out/lib/${ghc.targetPrefix}${ghc.haskellCompilerName}" 62 + lib.optionalString (ghc ? hadrian) "/lib"; 63 docDir = "$out/share/doc/ghc/html"; 64 packageCfgDir = "${libDir}/package.conf.d"; 65 paths = lib.concatLists ( 66 builtins.map (pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ]) ( 67 lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages) 68 ) 69 ); 70 hasLibraries = lib.any (x: x.isHaskellLibrary) paths; 71 # Clang is needed on Darwin for -fllvm to work. 72 # GHC >= 9.10 needs an LLVM specific assembler which we use clang for. 73 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm 74 llvm = lib.makeBinPath ( 75 [ ghc.llvmPackages.llvm ] 76 ++ lib.optionals (lib.versionAtLeast ghc.version "9.10" || stdenv.targetPlatform.isDarwin) [ 77 ghc.llvmPackages.clang 78 ] 79 ); 80in 81 82if paths == [ ] && !useLLVM then 83 ghc 84else 85 symlinkJoin { 86 # this makes computing paths from the name attribute impossible; 87 # if such a feature is needed, the real compiler name should be saved 88 # as a dedicated drv attribute, like `compiler-name` 89 name = ghc.name + "-with-packages"; 90 paths = paths ++ [ ghc ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" ghc) ]; 91 nativeBuildInputs = [ makeWrapper ]; 92 postBuild = '' 93 # wrap compiler executables with correct env variables 94 95 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do 96 if [[ -x "${ghc}/bin/$prg" ]]; then 97 rm -f $out/bin/$prg 98 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \ 99 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ 100 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \ 101 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \ 102 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \ 103 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \ 104 ${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''} 105 fi 106 done 107 108 for prg in runghc runhaskell; do 109 if [[ -x "${ghc}/bin/$prg" ]]; then 110 rm -f $out/bin/$prg 111 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \ 112 --add-flags "-f $out/bin/${ghcCommand}" \ 113 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \ 114 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \ 115 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \ 116 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" 117 fi 118 done 119 120 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do 121 if [[ -x "${ghc}/bin/$prg" ]]; then 122 rm -f $out/bin/$prg 123 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "--global-package-db=${packageCfgDir}" 124 fi 125 done 126 127 # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976 128 if [[ -x "${ghc}/bin/haddock" ]]; then 129 rm -f $out/bin/haddock 130 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \ 131 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ 132 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" 133 fi 134 135 '' 136 + (lib.optionalString (stdenv.targetPlatform.isDarwin && !stdenv.targetPlatform.isiOS) '' 137 # Work around a linker limit in macOS Sierra (see generic-builder.nix): 138 local packageConfDir="${packageCfgDir}"; 139 local dynamicLinksDir="$out/lib/links"; 140 mkdir -p $dynamicLinksDir 141 # Clean up the old links that may have been (transitively) included by 142 # symlinkJoin: 143 rm -f $dynamicLinksDir/* 144 145 dynamicLibraryDirs=() 146 147 for pkg in $($out/bin/ghc-pkg list --simple-output); do 148 dynamicLibraryDirs+=($($out/bin/ghc-pkg --simple-output field "$pkg" dynamic-library-dirs)) 149 done 150 151 for dynamicLibraryDir in $(echo "''${dynamicLibraryDirs[@]}" | tr ' ' '\n' | sort -u); do 152 echo "Linking $dynamicLibraryDir/*.dylib from $dynamicLinksDir" 153 find "$dynamicLibraryDir" -name '*.dylib' -exec ln -s {} "$dynamicLinksDir" \; 154 done 155 156 for f in $packageConfDir/*.conf; do 157 # Initially, $f is a symlink to a read-only file in one of the inputs 158 # (as a result of this symlinkJoin derivation). 159 # Replace it with a copy whose dynamic-library-dirs points to 160 # $dynamicLinksDir 161 cp $f $f-tmp 162 rm $f 163 sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f 164 rm $f-tmp 165 done 166 '') 167 + '' 168 ${lib.optionalString hasLibraries '' 169 # GHC 8.10 changes. 170 # Instead of replacing package.cache[.lock] with the new file, 171 # ghc-pkg is now trying to open the file. These file are symlink 172 # to another nix derivation, so they are not writable. Removing 173 # them allow the correct behavior of ghc-pkg recache 174 # See: https://github.com/NixOS/nixpkgs/issues/79441 175 rm ${packageCfgDir}/package.cache.lock 176 rm ${packageCfgDir}/package.cache 177 178 $out/bin/${ghcCommand}-pkg recache 179 ''} 180 $out/bin/${ghcCommand}-pkg check 181 '' 182 + postBuild; 183 preferLocalBuild = true; 184 passthru = { 185 inherit (ghc) version targetPrefix; 186 187 hoogle = hoogleWithPackages'; 188 189 # Inform users about backwards incompatibilities with <= 21.05 190 override = 191 _: 192 throw '' 193 The ghc.withPackages wrapper itself can now be overridden, but no longer 194 the result of calling it (as before). Consequently overrides need to be 195 adjusted: Instead of 196 197 (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; } 198 199 use 200 201 (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ]) 202 203 Also note that withLLVM has been renamed to useLLVM for consistency with 204 the GHC Nix expressions.''; 205 }; 206 pos = __curPos; 207 meta = ghc.meta // { 208 # To be fixed by <https://github.com/NixOS/nixpkgs/pull/440774>. 209 broken = useLLVM; 210 }; 211 }