2021-11-20 16:43:12 +00:00
|
|
|
{ lib, stdenv, haskellPackages, symlinkJoin, makeWrapper
|
2021-11-25 16:14:21 +00:00
|
|
|
# GHC will have LLVM available if necessary for the respective target,
|
|
|
|
# so useLLVM only needs to be changed if -fllvm is to be used for a
|
|
|
|
# platform that has NCG support
|
|
|
|
, useLLVM ? false
|
2021-11-20 16:43:12 +00:00
|
|
|
, withHoogle ? false
|
2021-12-16 13:13:30 +00:00
|
|
|
# Whether to install `doc` outputs for GHC and all included libraries.
|
|
|
|
, installDocumentation ? true
|
2021-11-20 16:43:12 +00:00
|
|
|
, hoogleWithPackages
|
2015-04-28 11:14:05 +00:00
|
|
|
, postBuild ? ""
|
2017-02-25 22:45:40 +00:00
|
|
|
, ghcLibdir ? null # only used by ghcjs, when resolving plugins
|
2015-04-28 11:14:05 +00:00
|
|
|
}:
|
2015-03-09 19:11:23 +00:00
|
|
|
|
2021-11-20 16:43:12 +00:00
|
|
|
# This argument is a function which selects a list of Haskell packages from any
|
|
|
|
# passed Haskell package set.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# (hpkgs: [ hpkgs.mtl hpkgs.lens ])
|
|
|
|
selectPackages:
|
2017-02-25 22:45:40 +00:00
|
|
|
|
2015-01-07 19:31:32 +00:00
|
|
|
# It's probably a good idea to include the library "ghc-paths" in the
|
|
|
|
# compiler environment, because we have a specially patched version of
|
|
|
|
# that package in Nix that honors these environment variables
|
|
|
|
#
|
|
|
|
# NIX_GHC
|
|
|
|
# NIX_GHCPKG
|
|
|
|
# NIX_GHC_DOCDIR
|
|
|
|
# NIX_GHC_LIBDIR
|
|
|
|
#
|
|
|
|
# instead of hard-coding the paths. The wrapper sets these variables
|
|
|
|
# appropriately to configure ghc-paths to point back to the wrapper
|
|
|
|
# instead of to the pristine GHC package, which doesn't know any of the
|
|
|
|
# additional libraries.
|
|
|
|
#
|
|
|
|
# A good way to import the environment set by the wrapper below into
|
|
|
|
# your shell is to add the following snippet to your ~/.bashrc:
|
|
|
|
#
|
|
|
|
# if [ -e ~/.nix-profile/bin/ghc ]; then
|
|
|
|
# eval $(grep export ~/.nix-profile/bin/ghc)
|
|
|
|
# fi
|
|
|
|
|
|
|
|
let
|
2021-11-20 16:43:12 +00:00
|
|
|
inherit (haskellPackages) llvmPackages ghc;
|
|
|
|
|
|
|
|
packages = selectPackages haskellPackages
|
|
|
|
++ lib.optional withHoogle (hoogleWithPackages selectPackages);
|
|
|
|
|
2015-03-29 09:02:31 +00:00
|
|
|
isGhcjs = ghc.isGhcjs or false;
|
2017-02-04 17:19:14 +00:00
|
|
|
isHaLVM = ghc.isHaLVM or false;
|
2023-01-22 01:18:12 +00:00
|
|
|
ghcCommand' = if isGhcjs then "ghcjs" else "ghc";
|
|
|
|
ghcCommand = "${ghc.targetPrefix}${ghcCommand'}";
|
2017-01-14 02:21:21 +00:00
|
|
|
ghcCommandCaps= lib.toUpper ghcCommand';
|
2018-06-26 20:34:03 +00:00
|
|
|
libDir = if isHaLVM then "$out/lib/HaLVM-${ghc.version}"
|
2023-01-22 01:17:29 +00:00
|
|
|
else "$out/lib/${ghc.targetPrefix}${ghc.haskellCompilerName}"
|
|
|
|
+ lib.optionalString (ghc ? hadrian) "/lib";
|
2023-04-03 21:33:07 +00:00
|
|
|
# Boot libraries for GHC are present in a separate directory.
|
|
|
|
bootLibDir = let arch = if stdenv.targetPlatform.isAarch64
|
|
|
|
then "aarch64"
|
|
|
|
else "x86_64";
|
|
|
|
platform = if stdenv.targetPlatform.isDarwin then "osx" else "linux";
|
|
|
|
in "${ghc}/lib/${ghc.haskellCompilerName}/lib/${arch}-${platform}-${ghc.haskellCompilerName}";
|
2015-01-07 19:31:32 +00:00
|
|
|
docDir = "$out/share/doc/ghc/html";
|
|
|
|
packageCfgDir = "${libDir}/package.conf.d";
|
2021-12-16 13:13:30 +00:00
|
|
|
paths = lib.concatLists (
|
|
|
|
builtins.map
|
|
|
|
(pkg: [ pkg ] ++ lib.optionals installDocumentation [ (lib.getOutput "doc" pkg) ])
|
|
|
|
(lib.filter (x: x ? isHaskellLibrary) (lib.closePropagation packages))
|
|
|
|
);
|
2015-03-29 02:24:35 +00:00
|
|
|
hasLibraries = lib.any (x: x.isHaskellLibrary) paths;
|
2015-03-09 19:11:23 +00:00
|
|
|
# CLang is needed on Darwin for -fllvm to work:
|
2018-01-03 04:19:22 +00:00
|
|
|
# https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
|
2016-04-13 12:53:51 +00:00
|
|
|
llvm = lib.makeBinPath
|
2015-03-09 19:11:23 +00:00
|
|
|
([ llvmPackages.llvm ]
|
2018-08-20 18:43:41 +00:00
|
|
|
++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang);
|
2015-01-07 19:31:32 +00:00
|
|
|
in
|
2021-11-20 16:43:12 +00:00
|
|
|
|
|
|
|
assert ghcLibdir != null -> (ghc.isGhcjs or false);
|
|
|
|
|
2021-11-23 22:15:34 +00:00
|
|
|
if paths == [] && !useLLVM then ghc else
|
2020-01-10 14:20:08 +00:00
|
|
|
symlinkJoin {
|
2016-11-16 12:45:59 +00:00
|
|
|
# this makes computing paths from the name attribute impossible;
|
|
|
|
# if such a feature is needed, the real compiler name should be saved
|
|
|
|
# as a dedicated drv attribute, like `compiler-name`
|
|
|
|
name = ghc.name + "-with-packages";
|
2021-12-16 13:13:30 +00:00
|
|
|
paths = paths
|
|
|
|
++ [ ghc ]
|
2023-01-19 17:12:58 +00:00
|
|
|
++ lib.optionals installDocumentation [ (lib.getOutput "doc" ghc) ];
|
2021-03-02 22:33:53 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
2015-01-07 19:31:32 +00:00
|
|
|
postBuild = ''
|
2017-01-15 23:13:13 +00:00
|
|
|
# wrap compiler executables with correct env variables
|
|
|
|
|
2015-05-06 19:56:02 +00:00
|
|
|
for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
|
|
|
|
if [[ -x "${ghc}/bin/$prg" ]]; then
|
|
|
|
rm -f $out/bin/$prg
|
|
|
|
makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
|
|
|
|
--add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
|
|
|
|
--set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
|
|
|
|
--set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
|
|
|
|
--set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
|
|
|
|
--set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \
|
2017-10-31 22:10:47 +00:00
|
|
|
${lib.optionalString (ghc.isGhcjs or false)
|
|
|
|
''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"''
|
|
|
|
} \
|
2021-11-23 22:15:34 +00:00
|
|
|
${lib.optionalString useLLVM ''--prefix "PATH" ":" "${llvm}"''}
|
2015-05-06 19:56:02 +00:00
|
|
|
fi
|
2015-01-07 19:31:32 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
for prg in runghc runhaskell; do
|
2015-05-06 19:56:02 +00:00
|
|
|
if [[ -x "${ghc}/bin/$prg" ]]; then
|
|
|
|
rm -f $out/bin/$prg
|
|
|
|
makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
|
|
|
|
--add-flags "-f $out/bin/${ghcCommand}" \
|
|
|
|
--set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
|
|
|
|
--set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
|
|
|
|
--set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
|
|
|
|
--set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
|
|
|
|
fi
|
2015-01-07 19:31:32 +00:00
|
|
|
done
|
|
|
|
|
2015-03-13 03:20:56 +00:00
|
|
|
for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
|
2015-05-06 19:56:02 +00:00
|
|
|
if [[ -x "${ghc}/bin/$prg" ]]; then
|
|
|
|
rm -f $out/bin/$prg
|
2024-01-28 09:58:14 +00:00
|
|
|
makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "--global-package-db=${packageCfgDir}"
|
2015-05-06 19:56:02 +00:00
|
|
|
fi
|
2015-01-07 19:31:32 +00:00
|
|
|
done
|
2018-03-15 17:55:18 +00:00
|
|
|
|
|
|
|
# haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976
|
|
|
|
if [[ -x "${ghc}/bin/haddock" ]]; then
|
|
|
|
rm -f $out/bin/haddock
|
|
|
|
makeWrapper ${ghc}/bin/haddock $out/bin/haddock \
|
|
|
|
--add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
|
|
|
|
--set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
|
|
|
|
fi
|
|
|
|
|
2018-08-20 18:43:41 +00:00
|
|
|
'' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) ''
|
2017-08-06 22:05:18 +00:00
|
|
|
# Work around a linker limit in macOS Sierra (see generic-builder.nix):
|
2022-03-31 10:37:00 +00:00
|
|
|
local packageConfDir="${packageCfgDir}";
|
2023-04-03 21:33:07 +00:00
|
|
|
local dynamicLinksDir="$out/lib/links";
|
2017-05-05 16:53:08 +00:00
|
|
|
mkdir -p $dynamicLinksDir
|
|
|
|
# Clean up the old links that may have been (transitively) included by
|
|
|
|
# symlinkJoin:
|
|
|
|
rm -f $dynamicLinksDir/*
|
2023-04-03 21:33:07 +00:00
|
|
|
|
|
|
|
# Boot libraries are located differently than other libraries since GHC 9.6, so handle them separately.
|
|
|
|
if [[ -x "${bootLibDir}" ]]; then
|
|
|
|
ln -s "${bootLibDir}"/*.dylib $dynamicLinksDir
|
|
|
|
fi
|
|
|
|
|
2020-05-28 20:10:36 +00:00
|
|
|
for d in $(grep -Poz "dynamic-library-dirs:\s*\K .+\n" $packageConfDir/*|awk '{print $2}'|sort -u); do
|
2017-05-05 16:53:08 +00:00
|
|
|
ln -s $d/*.dylib $dynamicLinksDir
|
|
|
|
done
|
|
|
|
for f in $packageConfDir/*.conf; do
|
|
|
|
# Initially, $f is a symlink to a read-only file in one of the inputs
|
|
|
|
# (as a result of this symlinkJoin derivation).
|
|
|
|
# Replace it with a copy whose dynamic-library-dirs points to
|
|
|
|
# $dynamicLinksDir
|
|
|
|
cp $f $f-tmp
|
|
|
|
rm $f
|
2023-05-05 18:20:46 +00:00
|
|
|
sed "N;s,dynamic-library-dirs:\s*.*\n,dynamic-library-dirs: $dynamicLinksDir\n," $f-tmp > $f
|
2017-05-05 16:53:08 +00:00
|
|
|
rm $f-tmp
|
|
|
|
done
|
|
|
|
'') + ''
|
2020-04-17 15:17:06 +00:00
|
|
|
${lib.optionalString hasLibraries ''
|
|
|
|
# GHC 8.10 changes.
|
|
|
|
# Instead of replacing package.cache[.lock] with the new file,
|
|
|
|
# ghc-pkg is now trying to open the file. These file are symlink
|
|
|
|
# to another nix derivation, so they are not writable. Removing
|
|
|
|
# them allow the correct behavior of ghc-pkg recache
|
|
|
|
# See: https://github.com/NixOS/nixpkgs/issues/79441
|
2022-03-31 10:37:00 +00:00
|
|
|
rm ${packageCfgDir}/package.cache.lock
|
|
|
|
rm ${packageCfgDir}/package.cache
|
2020-04-17 15:17:06 +00:00
|
|
|
|
|
|
|
$out/bin/${ghcCommand}-pkg recache
|
|
|
|
''}
|
2017-02-25 22:45:40 +00:00
|
|
|
${# ghcjs will read the ghc_libdir file when resolving plugins.
|
|
|
|
lib.optionalString (isGhcjs && ghcLibdir != null) ''
|
|
|
|
mkdir -p "${libDir}"
|
|
|
|
rm -f "${libDir}/ghc_libdir"
|
|
|
|
printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir"
|
|
|
|
''}
|
2015-03-13 03:20:56 +00:00
|
|
|
$out/bin/${ghcCommand}-pkg check
|
2015-04-28 11:14:05 +00:00
|
|
|
'' + postBuild;
|
2023-01-14 21:37:52 +00:00
|
|
|
preferLocalBuild = true;
|
2015-01-22 23:10:08 +00:00
|
|
|
passthru = {
|
|
|
|
inherit (ghc) version meta;
|
2022-01-27 08:42:51 +00:00
|
|
|
|
|
|
|
# Inform users about backwards incompatibilities with <= 21.05
|
|
|
|
override = _: throw ''
|
|
|
|
The ghc.withPackages wrapper itself can now be overridden, but no longer
|
|
|
|
the result of calling it (as before). Consequently overrides need to be
|
|
|
|
adjusted: Instead of
|
|
|
|
|
|
|
|
(ghc.withPackages (p: [ p.my-package ])).override { withLLLVM = true; }
|
|
|
|
|
|
|
|
use
|
|
|
|
|
|
|
|
(ghc.withPackages.override { useLLVM = true; }) (p: [ p.my-package ])
|
|
|
|
|
|
|
|
Also note that withLLVM has been renamed to useLLVM for consistency with
|
|
|
|
the GHC Nix expressions.'';
|
2015-01-22 23:10:08 +00:00
|
|
|
};
|
2015-01-10 08:12:37 +00:00
|
|
|
}
|