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