nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, ghc, llvmPackages, packages, symlinkJoin, makeWrapper
2, withLLVM ? false
3, postBuild ? ""
4, ghcLibdir ? null # only used by ghcjs, when resolving plugins
5}:
6
7assert ghcLibdir != null -> (ghc.isGhcjs or false);
8
9# This wrapper works only with GHC 6.12 or later.
10assert lib.versionOlder "6.12" ghc.version || ghc.isGhcjs || ghc.isHaLVM;
11
12# It's probably a good idea to include the library "ghc-paths" in the
13# compiler environment, because we have a specially patched version of
14# that package in Nix that honors these environment variables
15#
16# NIX_GHC
17# NIX_GHCPKG
18# NIX_GHC_DOCDIR
19# NIX_GHC_LIBDIR
20#
21# instead of hard-coding the paths. The wrapper sets these variables
22# appropriately to configure ghc-paths to point back to the wrapper
23# instead of to the pristine GHC package, which doesn't know any of the
24# additional libraries.
25#
26# A good way to import the environment set by the wrapper below into
27# your shell is to add the following snippet to your ~/.bashrc:
28#
29# if [ -e ~/.nix-profile/bin/ghc ]; then
30# eval $(grep export ~/.nix-profile/bin/ghc)
31# fi
32
33let
34 isGhcjs = ghc.isGhcjs or false;
35 isHaLVM = ghc.isHaLVM or false;
36 ghc761OrLater = isGhcjs || isHaLVM || lib.versionOlder "7.6.1" ghc.version;
37 packageDBFlag = if ghc761OrLater then "--global-package-db" else "--global-conf";
38 ghcCommand' = if isGhcjs then "ghcjs" else "ghc";
39 ghcCommand = "${ghc.targetPrefix}${ghcCommand'}";
40 ghcCommandCaps= lib.toUpper ghcCommand';
41 libDir = if isHaLVM then "$out/lib/HaLVM-${ghc.version}"
42 else "$out/lib/${ghcCommand}-${ghc.version}";
43 docDir = "$out/share/doc/ghc/html";
44 packageCfgDir = "${libDir}/package.conf.d";
45 paths = lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages);
46 hasLibraries = lib.any (x: x.isHaskellLibrary) paths;
47 # CLang is needed on Darwin for -fllvm to work:
48 # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
49 llvm = lib.makeBinPath
50 ([ llvmPackages.llvm ]
51 ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang);
52in
53if paths == [] && !withLLVM then ghc else
54symlinkJoin {
55 # this makes computing paths from the name attribute impossible;
56 # if such a feature is needed, the real compiler name should be saved
57 # as a dedicated drv attribute, like `compiler-name`
58 name = ghc.name + "-with-packages";
59 paths = paths ++ [ghc];
60 postBuild = ''
61 . ${makeWrapper}/nix-support/setup-hook
62
63 # wrap compiler executables with correct env variables
64
65 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
66 if [[ -x "${ghc}/bin/$prg" ]]; then
67 rm -f $out/bin/$prg
68 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
69 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
70 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
71 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
72 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
73 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \
74 ${lib.optionalString (ghc.isGhcjs or false)
75 ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
76 } \
77 ${lib.optionalString withLLVM ''--prefix "PATH" ":" "${llvm}"''}
78 fi
79 done
80
81 for prg in runghc runhaskell; do
82 if [[ -x "${ghc}/bin/$prg" ]]; then
83 rm -f $out/bin/$prg
84 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
85 --add-flags "-f $out/bin/${ghcCommand}" \
86 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
87 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
88 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
89 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
90 fi
91 done
92
93 for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
94 if [[ -x "${ghc}/bin/$prg" ]]; then
95 rm -f $out/bin/$prg
96 makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "${packageDBFlag}=${packageCfgDir}"
97 fi
98 done
99
100 # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976
101 if [[ -x "${ghc}/bin/haddock" ]]; then
102 rm -f $out/bin/haddock
103 makeWrapper ${ghc}/bin/haddock $out/bin/haddock \
104 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
105 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
106 fi
107
108 '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) ''
109 # Work around a linker limit in macOS Sierra (see generic-builder.nix):
110 local packageConfDir="$out/lib/${ghc.name}/package.conf.d";
111 local dynamicLinksDir="$out/lib/links"
112 mkdir -p $dynamicLinksDir
113 # Clean up the old links that may have been (transitively) included by
114 # symlinkJoin:
115 rm -f $dynamicLinksDir/*
116 for d in $(grep dynamic-library-dirs $packageConfDir/*|awk '{print $2}'|sort -u); do
117 ln -s $d/*.dylib $dynamicLinksDir
118 done
119 for f in $packageConfDir/*.conf; do
120 # Initially, $f is a symlink to a read-only file in one of the inputs
121 # (as a result of this symlinkJoin derivation).
122 # Replace it with a copy whose dynamic-library-dirs points to
123 # $dynamicLinksDir
124 cp $f $f-tmp
125 rm $f
126 sed "s,dynamic-library-dirs: .*,dynamic-library-dirs: $dynamicLinksDir," $f-tmp > $f
127 rm $f-tmp
128 done
129 '') + ''
130 ${lib.optionalString hasLibraries ''
131 # GHC 8.10 changes.
132 # Instead of replacing package.cache[.lock] with the new file,
133 # ghc-pkg is now trying to open the file. These file are symlink
134 # to another nix derivation, so they are not writable. Removing
135 # them allow the correct behavior of ghc-pkg recache
136 # See: https://github.com/NixOS/nixpkgs/issues/79441
137 rm $out/lib/${ghc.name}/package.conf.d/package.cache.lock
138 rm $out/lib/${ghc.name}/package.conf.d/package.cache
139
140 $out/bin/${ghcCommand}-pkg recache
141 ''}
142 ${# ghcjs will read the ghc_libdir file when resolving plugins.
143 lib.optionalString (isGhcjs && ghcLibdir != null) ''
144 mkdir -p "${libDir}"
145 rm -f "${libDir}/ghc_libdir"
146 printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
147 ''}
148 $out/bin/${ghcCommand}-pkg check
149 '' + postBuild;
150 passthru = {
151 preferLocalBuild = true;
152 inherit (ghc) version meta;
153 };
154}