1 { lib, stdenv, haskellPackages, symlinkJoin, makeWrapper
2 # GHC will have LLVM available if necessary for the respective target,
3 # so useLLVM only needs to be changed if -fllvm is to be used for a
4 # platform that has NCG support
7 # Whether to install `doc` outputs for GHC and all included libraries.
8 , installDocumentation ? true
11 , ghcLibdir ? null # only used by ghcjs, when resolving plugins
14 # This argument is a function which selects a list of Haskell packages from any
15 # passed Haskell package set.
18 # (hpkgs: [ hpkgs.mtl hpkgs.lens ])
21 # It's probably a good idea to include the library "ghc-paths" in the
22 # compiler environment, because we have a specially patched version of
23 # that package in Nix that honors these environment variables
30 # instead of hard-coding the paths. The wrapper sets these variables
31 # appropriately to configure ghc-paths to point back to the wrapper
32 # instead of to the pristine GHC package, which doesn't know any of the
33 # additional libraries.
35 # A good way to import the environment set by the wrapper below into
36 # your shell is to add the following snippet to your ~/.bashrc:
38 # if [ -e ~/.nix-profile/bin/ghc ]; then
39 # eval $(grep export ~/.nix-profile/bin/ghc)
43 inherit (haskellPackages) llvmPackages ghc;
45 packages = selectPackages haskellPackages
46 ++ lib.optional withHoogle (hoogleWithPackages selectPackages);
48 isGhcjs = ghc.isGhcjs or false;
49 isHaLVM = ghc.isHaLVM or false;
50 ghcCommand' = if isGhcjs then "ghcjs" else "ghc";
51 ghcCommand = "${ghc.targetPrefix}${ghcCommand'}";
52 ghcCommandCaps= lib.toUpper ghcCommand';
53 libDir = if isHaLVM then "$out/lib/HaLVM-${ghc.version}"
54 else "$out/lib/${ghc.targetPrefix}${ghc.haskellCompilerName}"
55 + lib.optionalString (ghc ? hadrian) "/lib";
56 # Boot libraries for GHC are present in a separate directory.
57 bootLibDir = let arch = if stdenv.targetPlatform.isAarch64
60 platform = if stdenv.targetPlatform.isDarwin then "osx" else "linux";
61 in "${ghc}/lib/${ghc.haskellCompilerName}/lib/${arch}-${platform}-${ghc.haskellCompilerName}";
62 docDir = "$out/share/doc/ghc/html";
63 packageCfgDir = "${libDir}/package.conf.d";
64 paths = lib.concatLists (
66 (pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ])
67 (lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages))
69 hasLibraries = lib.any (x: x.isHaskellLibrary) paths;
70 # CLang is needed on Darwin for -fllvm to work:
71 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
72 llvm = lib.makeBinPath
73 ([ llvmPackages.llvm ]
74 ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang);
77 assert ghcLibdir != null -> (ghc.isGhcjs or false);
79 if paths == [] && !useLLVM then ghc else
81 # this makes computing paths from the name attribute impossible;
82 # if such a feature is needed, the real compiler name should be saved
83 # as a dedicated drv attribute, like `compiler-name`
84 name = ghc.name + "-with-packages";
87 ++ lib.optionals installDocumentation [ (lib.getOutput "doc" ghc) ];
88 nativeBuildInputs = [ makeWrapper ];
90 # wrap compiler executables with correct env variables
92 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
93 if [[ -x "${ghc}/bin/$prg" ]]; then
95 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
96 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
97 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
98 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
99 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
100 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \
101 ${lib.optionalString (ghc.isGhcjs or false)
102 ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
104 ${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''}
108 for prg in runghc runhaskell; do
109 if [[ -x "${ghc}/bin/$prg" ]]; then
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}"
120 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
121 if [[ -x "${ghc}/bin/$prg" ]]; then
123 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "--global-package-db=${packageCfgDir}"
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}"
135 '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) ''
136 # Work around a linker limit in macOS Sierra (see generic-builder.nix):
137 local packageConfDir="${packageCfgDir}";
138 local dynamicLinksDir="$out/lib/links";
139 mkdir -p $dynamicLinksDir
140 # Clean up the old links that may have been (transitively) included by
142 rm -f $dynamicLinksDir/*
144 # Boot libraries are located differently than other libraries since GHC 9.6, so handle them separately.
145 if [[ -x "${bootLibDir}" ]]; then
146 ln -s "${bootLibDir}"/*.dylib $dynamicLinksDir
149 for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
150 ln -s $d/*.dylib $dynamicLinksDir
152 for f in $packageConfDir/*.conf; do
153 # Initially, $f is a symlink to a read-only file in one of the inputs
154 # (as a result of this symlinkJoin derivation).
155 # Replace it with a copy whose dynamic-library-dirs points to
159 sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f
163 ${lib.optionalString hasLibraries ''
165 # Instead of replacing package.cache[.lock] with the new file,
166 # ghc-pkg is now trying to open the file. These file are symlink
167 # to another nix derivation, so they are not writable. Removing
168 # them allow the correct behavior of ghc-pkg recache
169 # See: https://github.com/NixOS/nixpkgs/issues/79441
170 rm ${packageCfgDir}/package.cache.lock
171 rm ${packageCfgDir}/package.cache
173 $out/bin/${ghcCommand}-pkg recache
175 ${# ghcjs will read the ghc_libdir file when resolving plugins.
176 lib.optionalString (isGhcjs && ghcLibdir != null) ''
178 rm -f "${libDir}/ghc_libdir"
179 printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
181 $out/bin/${ghcCommand}-pkg check
183 preferLocalBuild = true;
185 inherit (ghc) version meta;
187 # Inform users about backwards incompatibilities with <= 21.05
188 override = _: throw ''
189 The ghc.withPackages wrapper itself can now be overridden, but no longer
190 the result of calling it (as before). Consequently overrides need to be
193 (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; }
197 (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ])
199 Also note that withLLVM has been renamed to useLLVM for consistency with
200 the GHC Nix expressions.'';