Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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 docDir = "$out/share/doc/ghc/html";
66 packageCfgDir = "${libDir}/package.conf.d";
67 paths = lib.concatLists (
68 builtins.map (pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ]) (
69 lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages)
70 )
71 );
72 hasLibraries = lib.any (x: x.isHaskellLibrary) paths;
73 # CLang is needed on Darwin for -fllvm to work:
74 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
75 llvm = lib.makeBinPath (
76 [ llvmPackages.llvm ] ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang
77 );
78in
79
80assert ghcLibdir != null -> (ghc.isGhcjs or false);
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 ${
105 lib.optionalString (ghc.isGhcjs or false) ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
106 } \
107 ${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''}
108 fi
109 done
110
111 for prg in runghc runhaskell; do
112 if [[ -x "${ghc}/bin/$prg" ]]; then
113 rm -f $out/bin/$prg
114 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
115 --add-flags "-f $out/bin/${ghcCommand}" \
116 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
117 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
118 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
119 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
120 fi
121 done
122
123 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
124 if [[ -x "${ghc}/bin/$prg" ]]; then
125 rm -f $out/bin/$prg
126 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "--global-package-db=${packageCfgDir}"
127 fi
128 done
129
130 # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976
131 if [[ -x "${ghc}/bin/haddock" ]]; then
132 rm -f $out/bin/haddock
133 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \
134 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
135 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
136 fi
137
138 ''
139 + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS)
140 ''
141 # Work around a linker limit in macOS Sierra (see generic-builder.nix):
142 local packageConfDir="${packageCfgDir}";
143 local dynamicLinksDir="$out/lib/links";
144 mkdir -p $dynamicLinksDir
145 # Clean up the old links that may have been (transitively) included by
146 # symlinkJoin:
147 rm -f $dynamicLinksDir/*
148
149 dynamicLibraryDirs=()
150
151 for pkg in $($out/bin/ghc-pkg list --simple-output); do
152 dynamicLibraryDirs+=($($out/bin/ghc-pkg --simple-output field "$pkg" dynamic-library-dirs))
153 done
154
155 for dynamicLibraryDir in $(echo "''${dynamicLibraryDirs[@]}" | tr ' ' '\n' | sort -u); do
156 echo "Linking $dynamicLibraryDir/*.dylib from $dynamicLinksDir"
157 find "$dynamicLibraryDir" -name '*.dylib' -exec ln -s {} "$dynamicLinksDir" \;
158 done
159
160 for f in $packageConfDir/*.conf; do
161 # Initially, $f is a symlink to a read-only file in one of the inputs
162 # (as a result of this symlinkJoin derivation).
163 # Replace it with a copy whose dynamic-library-dirs points to
164 # $dynamicLinksDir
165 cp $f $f-tmp
166 rm $f
167 sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f
168 rm $f-tmp
169 done
170 ''
171 )
172 + ''
173 ${lib.optionalString hasLibraries ''
174 # GHC 8.10 changes.
175 # Instead of replacing package.cache[.lock] with the new file,
176 # ghc-pkg is now trying to open the file. These file are symlink
177 # to another nix derivation, so they are not writable. Removing
178 # them allow the correct behavior of ghc-pkg recache
179 # See: https://github.com/NixOS/nixpkgs/issues/79441
180 rm ${packageCfgDir}/package.cache.lock
181 rm ${packageCfgDir}/package.cache
182
183 $out/bin/${ghcCommand}-pkg recache
184 ''}
185 ${
186 # ghcjs will read the ghc_libdir file when resolving plugins.
187 lib.optionalString (isGhcjs && ghcLibdir != null) ''
188 mkdir -p "${libDir}"
189 rm -f "${libDir}/ghc_libdir"
190 printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
191 ''
192 }
193 $out/bin/${ghcCommand}-pkg check
194 ''
195 + postBuild;
196 preferLocalBuild = true;
197 passthru = {
198 inherit (ghc) version meta;
199
200 hoogle = hoogleWithPackages';
201
202 # Inform users about backwards incompatibilities with <= 21.05
203 override =
204 _:
205 throw ''
206 The ghc.withPackages wrapper itself can now be overridden, but no longer
207 the result of calling it (as before). Consequently overrides need to be
208 adjusted: Instead of
209
210 (ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; }
211
212 use
213
214 (ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ])
215
216 Also note that withLLVM has been renamed to useLLVM for consistency with
217 the GHC Nix expressions.'';
218 };
219 }