nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 207 lines 8.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}: 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 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 needs to be wrapped like GHC, see 128 # https://github.com/NixOS/nixpkgs/issues/36976 krank:ignore-line 129 if [[ -x "${ghc}/bin/haddock" ]]; then 130 rm -f $out/bin/haddock 131 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \ 132 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ 133 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" 134 fi 135 136 '' 137 + (lib.optionalString (stdenv.targetPlatform.isDarwin && !stdenv.targetPlatform.isiOS) '' 138 # Work around a linker limit in macOS Sierra (see generic-builder.nix): 139 local packageConfDir="${packageCfgDir}"; 140 local dynamicLinksDir="$out/lib/links"; 141 mkdir -p $dynamicLinksDir 142 # Clean up the old links that may have been (transitively) included by 143 # symlinkJoin: 144 rm -f $dynamicLinksDir/* 145 146 dynamicLibraryDirs=() 147 148 for pkg in $($out/bin/ghc-pkg list --simple-output); do 149 dynamicLibraryDirs+=($($out/bin/ghc-pkg --simple-output field "$pkg" dynamic-library-dirs)) 150 done 151 152 for dynamicLibraryDir in $(echo "''${dynamicLibraryDirs[@]}" | tr ' ' '\n' | sort -u); do 153 echo "Linking $dynamicLibraryDir/*.dylib from $dynamicLinksDir" 154 find "$dynamicLibraryDir" -name '*.dylib' -exec ln -s {} "$dynamicLinksDir" \; 155 done 156 157 for f in $packageConfDir/*.conf; do 158 # Initially, $f is a symlink to a read-only file in one of the inputs 159 # (as a result of this symlinkJoin derivation). 160 # Replace it with a copy whose dynamic-library-dirs points to 161 # $dynamicLinksDir 162 cp $f $f-tmp 163 rm $f 164 sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f 165 rm $f-tmp 166 done 167 '') 168 + '' 169 ${lib.optionalString hasLibraries '' 170 # GHC 8.10 changes. 171 # Instead of replacing package.cache[.lock] with the new file, 172 # ghc-pkg is now trying to open the file. These file are symlink 173 # to another nix derivation, so they are not writable. Removing 174 # them allow the correct behavior of ghc-pkg recache 175 # See: https://github.com/NixOS/nixpkgs/issues/79441 krank:ignore-line 176 rm ${packageCfgDir}/package.cache.lock 177 rm ${packageCfgDir}/package.cache 178 179 $out/bin/${ghcCommand}-pkg recache 180 ''} 181 $out/bin/${ghcCommand}-pkg check 182 '' 183 + postBuild; 184 preferLocalBuild = true; 185 passthru = { 186 inherit (ghc) version meta targetPrefix; 187 188 hoogle = hoogleWithPackages'; 189 190 # Inform users about backwards incompatibilities with <= 21.05 191 override = 192 _: 193 throw '' 194 The ghc.withPackages wrapper itself can now be overridden, but no longer 195 the result of calling it (as before). Consequently overrides need to be 196 adjusted: Instead of 197 198 (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; } 199 200 use 201 202 (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ]) 203 204 Also note that withLLVM has been renamed to useLLVM for consistency with 205 the GHC Nix expressions.''; 206 }; 207 }