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
5, useLLVM ? false
6, withHoogle ? false
7# Whether to install `doc` outputs for GHC and all included libraries.
8, installDocumentation ? true
9, hoogleWithPackages
10, postBuild ? ""
11, ghcLibdir ? null # only used by ghcjs, when resolving plugins
12}:
13
14# This argument is a function which selects a list of Haskell packages from any
15# passed Haskell package set.
16#
17# Example:
18# (hpkgs: [ hpkgs.mtl hpkgs.lens ])
19selectPackages:
20
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
24#
25# NIX_GHC
26# NIX_GHCPKG
27# NIX_GHC_DOCDIR
28# NIX_GHC_LIBDIR
29#
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.
34#
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:
37#
38# if [ -e ~/.nix-profile/bin/ghc ]; then
39# eval $(grep export ~/.nix-profile/bin/ghc)
40# fi
41
42let
43 inherit (haskellPackages) llvmPackages ghc;
44
45 packages = selectPackages haskellPackages
46 ++ lib.optional withHoogle (hoogleWithPackages selectPackages);
47
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 docDir = "$out/share/doc/ghc/html";
59 packageCfgDir = "${libDir}/package.conf.d";
60 paths = lib.concatLists (
61 builtins.map
62 (pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ])
63 (lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages))
64 );
65 hasLibraries = lib.any (x: x.isHaskellLibrary) paths;
66 # CLang is needed on Darwin for -fllvm to work:
67 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
68 llvm = lib.makeBinPath
69 ([ llvmPackages.llvm ]
70 ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang);
71in
72
73assert ghcLibdir != null -> (ghc.isGhcjs or false);
74
75if paths == [] && !useLLVM then ghc else
76symlinkJoin {
77 # this makes computing paths from the name attribute impossible;
78 # if such a feature is needed, the real compiler name should be saved
79 # as a dedicated drv attribute, like `compiler-name`
80 name = ghc.name + "-with-packages";
81 paths = paths
82 ++ [ ghc ]
83 ++ lib.optionals installDocumentation [ (lib.getOutput "doc" ghc) ];
84 nativeBuildInputs = [ makeWrapper ];
85 postBuild = ''
86 # wrap compiler executables with correct env variables
87
88 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
89 if [[ -x "${ghc}/bin/$prg" ]]; then
90 rm -f $out/bin/$prg
91 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
92 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
93 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
94 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
95 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
96 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \
97 ${lib.optionalString (ghc.isGhcjs or false)
98 ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
99 } \
100 ${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''}
101 fi
102 done
103
104 for prg in runghc runhaskell; do
105 if [[ -x "${ghc}/bin/$prg" ]]; then
106 rm -f $out/bin/$prg
107 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
108 --add-flags "-f $out/bin/${ghcCommand}" \
109 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
110 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
111 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
112 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
113 fi
114 done
115
116 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
117 if [[ -x "${ghc}/bin/$prg" ]]; then
118 rm -f $out/bin/$prg
119 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "${packageDBFlag}=${packageCfgDir}"
120 fi
121 done
122
123 # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976
124 if [[ -x "${ghc}/bin/haddock" ]]; then
125 rm -f $out/bin/haddock
126 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \
127 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
128 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
129 fi
130
131 '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) ''
132 # Work around a linker limit in macOS Sierra (see generic-builder.nix):
133 local packageConfDir="${packageCfgDir}";
134 local dynamicLinksDir="$out/lib/links"
135 mkdir -p $dynamicLinksDir
136 # Clean up the old links that may have been (transitively) included by
137 # symlinkJoin:
138 rm -f $dynamicLinksDir/*
139 for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
140 ln -s $d/*.dylib $dynamicLinksDir
141 done
142 for f in $packageConfDir/*.conf; do
143 # Initially, $f is a symlink to a read-only file in one of the inputs
144 # (as a result of this symlinkJoin derivation).
145 # Replace it with a copy whose dynamic-library-dirs points to
146 # $dynamicLinksDir
147 cp $f $f-tmp
148 rm $f
149 sed "N;s,dynamic-library-dirs:\s*.*,dynamic-library-dirs: $dynamicLinksDir," $f-tmp > $f
150 rm $f-tmp
151 done
152 '') + ''
153 ${lib.optionalString hasLibraries ''
154 # GHC 8.10 changes.
155 # Instead of replacing package.cache[.lock] with the new file,
156 # ghc-pkg is now trying to open the file. These file are symlink
157 # to another nix derivation, so they are not writable. Removing
158 # them allow the correct behavior of ghc-pkg recache
159 # See: https://github.com/NixOS/nixpkgs/issues/79441
160 rm ${packageCfgDir}/package.cache.lock
161 rm ${packageCfgDir}/package.cache
162
163 $out/bin/${ghcCommand}-pkg recache
164 ''}
165 ${# ghcjs will read the ghc_libdir file when resolving plugins.
166 lib.optionalString (isGhcjs && ghcLibdir != null) ''
167 mkdir -p "${libDir}"
168 rm -f "${libDir}/ghc_libdir"
169 printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
170 ''}
171 $out/bin/${ghcCommand}-pkg check
172 '' + postBuild;
173 preferLocalBuild = true;
174 passthru = {
175 inherit (ghc) version meta;
176
177 # Inform users about backwards incompatibilities with <= 21.05
178 override = _: throw ''
179 The ghc.withPackages wrapper itself can now be overridden, but no longer
180 the result of calling it (as before). Consequently overrides need to be
181 adjusted: Instead of
182
183 (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; }
184
185 use
186
187 (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ])
188
189 Also note that withLLVM has been renamed to useLLVM for consistency with
190 the GHC Nix expressions.'';
191 };
192}