on e94de40 help sign in
haskell.nix/builder/ghc-for-component-wrapper.nix
102 lines 4.1 kB history raw
1# The ghcForComponent function wraps ghc so that it is configured with
2# the package database of all dependencies of a given component.
3# It has been adapted from the ghcWithPackages wrapper in nixpkgs.
4#
5# This wrapper exists so that nix-shells for components will have a
6# GHC automatically configured with the dependencies in the package
7# database.
9{ lib, stdenv, ghc, runCommand, lndir, makeWrapper, haskellLib
10}:
12{ componentName # Full derivation name of the component
13, configFiles # The component's "config" derivation
14, postInstall ? ""
15}:
17let
18 inherit (configFiles) targetPrefix ghcCommand ghcCommandCaps packageCfgDir;
19 libDir = "$out/${configFiles.libDir}";
20 docDir = "$out/share/doc/ghc/html";
21 # For musl we can use haddock from the buildGHC
22 haddock = if stdenv.hostPlatform.isLinux && stdenv.targetPlatform.isMusl && !haskellLib.isNativeMusl
23 then ghc.buildGHC
24 else ghc;
26in runCommand "${componentName}-${ghc.name}-env" {
27 preferLocalBuild = true;
28 passthru = {
29 inherit (ghc) version meta;
30 inherit targetPrefix;
31 baseGhc = ghc;
32 };
33} (
34 ''
35 . ${makeWrapper}/nix-support/setup-hook
37 # Start with a ghc...
38 mkdir -p $out/bin
39 ${lndir}/bin/lndir -silent ${ghc} $out
40 # ... remove all of the package directories
41 rm -rf ${libDir}/*/
42 # ... but retain the lib/ghc/bin directory. This contains `unlit' and friends.
43 ln -s ${ghc}/lib/${ghcCommand}-${ghc.version}/bin ${libDir}
44 # ... and the ghcjs shim's if they are available ...
45 if [ -d ${ghc}/lib/${ghcCommand}-${ghc.version}/shims ]; then
46 ln -s ${ghc}/lib/${ghcCommand}-${ghc.version}/shims ${libDir}
47 fi
48 # ... and node modules ...
49 if [ -d ${ghc}/lib/${ghcCommand}-${ghc.version}/ghcjs-node ]; then
50 ln -s ${ghc}/lib/${ghcCommand}-${ghc.version}/ghcjs-node ${libDir}
51 fi
52 # Replace the package database with the one from target package config.
53 ln -s ${configFiles}/${packageCfgDir} $out/${packageCfgDir}
55 # Wrap compiler executables with correct env variables.
56 # The NIX_ variables are used by the patched Paths_ghc module.
58 for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
59 if [[ -x "${ghc}/bin/$prg" ]]; then
60 rm -f $out/bin/$prg
61 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
62 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
63 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
64 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
65 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
66 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
67 fi
68 done
70 for prg in "${targetPrefix}runghc" "${targetPrefix}runhaskell"; do
71 if [[ -x "${ghc}/bin/$prg" ]]; then
72 rm -f $out/bin/$prg
73 makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
74 --add-flags "-f $out/bin/${ghcCommand}" \
75 --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
76 --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
77 --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
78 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
79 fi
80 done
82 # Wrap haddock, if the base GHC provides it.
83 if [[ -x "${haddock}/bin/haddock" ]]; then
84 rm -f $out/bin/haddock
85 makeWrapper ${haddock}/bin/haddock $out/bin/haddock \
86 --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
87 --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
88 fi
90 # Point ghc-pkg to the package database of the component using the
91 # --global-package-db flag.
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 "--global-package-db=$out/${packageCfgDir}"
97 fi
98 done
100 ${postInstall}
101 ''