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 ghc761OrLater = isGhcjs || isHaLVM || lib.versionOlder "7.6.1" ghc.version;
51 packageDBFlag = if ghc761OrLater then "--global-package-db" else "--global-conf";
52 ghcCommand' = if isGhcjs then "ghcjs" else "ghc";
53 ghcCommand = "${ghc.targetPrefix}${ghcCommand'}";
54 ghcCommandCaps= lib.toUpper ghcCommand';
55 libDir = if isHaLVM then "$out/lib/HaLVM-${ghc.version}"
56 else "$out/lib/${ghc.targetPrefix}${ghc.haskellCompilerName}"
57 + lib.optionalString (ghc ? hadrian) "/lib";
58 # Boot libraries for GHC are present in a separate directory.
59 bootLibDir = let arch = if stdenv.targetPlatform.isAarch64
62 platform = if stdenv.targetPlatform.isDarwin then "osx" else "linux";
63 in "${ghc}/lib/${ghc.haskellCompilerName}/lib/${arch}-${platform}-${ghc.haskellCompilerName}";
64 docDir = "$out/share/doc/ghc/html";
65 packageCfgDir = "${libDir}/package.conf.d";
66 paths = lib.concatLists (
68 (pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ])
69 (lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages))
71 hasLibraries = lib.any (x: x.isHaskellLibrary) paths;
72 # CLang is needed on Darwin for -fllvm to work:
73 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
74 llvm = lib.makeBinPath
75 ([ llvmPackages.llvm ]
76 ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang);
79 assert ghcLibdir != null -> (ghc.isGhcjs or false);
81 if paths == [] && !useLLVM then ghc else
83 # this makes computing paths from the name attribute impossible;
84 # if such a feature is needed, the real compiler name should be saved
85 # as a dedicated drv attribute, like `compiler-name`
86 name = ghc.name + "-with-packages";
89 ++ lib.optionals installDocumentation [ (lib.getOutput "doc" ghc) ];
90 nativeBuildInputs = [ makeWrapper ];
92 # wrap compiler executables with correct env variables
94 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
95 if [[ -x "${ghc}/bin/$prg" ]]; then
97 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
98 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
99 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
100 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
101 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
102 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \
103 ${lib.optionalString (ghc.isGhcjs or false)
104 ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
106 ${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''}
110 for prg in runghc runhaskell; do
111 if [[ -x "${ghc}/bin/$prg" ]]; then
113 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
114 --add-flags "-f $out/bin/${ghcCommand}" \
115 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
116 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
117 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
118 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
122 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
123 if [[ -x "${ghc}/bin/$prg" ]]; then
125 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "${packageDBFlag}=${packageCfgDir}"
129 # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976
130 if [[ -x "${ghc}/bin/haddock" ]]; then
131 rm -f $out/bin/haddock
132 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \
133 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
134 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
137 '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !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
144 rm -f $dynamicLinksDir/*
146 # Boot libraries are located differently than other libraries since GHC 9.6, so handle them separately.
147 if [[ -x "${bootLibDir}" ]]; then
148 ln -s "${bootLibDir}"/*.dylib $dynamicLinksDir
151 for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
152 ln -s $d/*.dylib $dynamicLinksDir
154 for f in $packageConfDir/*.conf; do
155 # Initially, $f is a symlink to a read-only file in one of the inputs
156 # (as a result of this symlinkJoin derivation).
157 # Replace it with a copy whose dynamic-library-dirs points to
161 sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f
165 ${lib.optionalString hasLibraries ''
167 # Instead of replacing package.cache[.lock] with the new file,
168 # ghc-pkg is now trying to open the file. These file are symlink
169 # to another nix derivation, so they are not writable. Removing
170 # them allow the correct behavior of ghc-pkg recache
171 # See: https://github.com/NixOS/nixpkgs/issues/79441
172 rm ${packageCfgDir}/package.cache.lock
173 rm ${packageCfgDir}/package.cache
175 $out/bin/${ghcCommand}-pkg recache
177 ${# ghcjs will read the ghc_libdir file when resolving plugins.
178 lib.optionalString (isGhcjs && ghcLibdir != null) ''
180 rm -f "${libDir}/ghc_libdir"
181 printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
183 $out/bin/${ghcCommand}-pkg check
185 preferLocalBuild = true;
187 inherit (ghc) version meta;
189 # Inform users about backwards incompatibilities with <= 21.05
190 override = _: throw ''
191 The ghc.withPackages wrapper itself can now be overridden, but no longer
192 the result of calling it (as before). Consequently overrides need to be
195 (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; }
199 (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ])
201 Also note that withLLVM has been renamed to useLLVM for consistency with
202 the GHC Nix expressions.'';