@@ -1,4 +1,4 @@
1−{ stdenv, buildPackages, ghc, lib, pkgconfig, writeText, runCommand, haskellLib, nonReinstallablePkgs }:
1+{ stdenv, buildPackages, ghc, lib, pkgconfig, writeText, runCommand, haskellLib, nonReinstallablePkgs, withPackage }:
22
33{ componentId
44, component
@@ -16,6 +16,7 @@
1616, preBuild ? null, postBuild ? null
1717, preCheck ? null, postCheck ? null
1818, preInstall ? null, postInstall ? null
19+, shellHook ? null
1920
2021, doCheck ? component.doCheck || haskellLib.isTest componentId
2122, doCrossCheck ? component.doCrossCheck || false
@@ -46,18 +47,30 @@ let
4647 in map ({val,...}: val) closure;
4748
4849 exactDep = pdbArg: p: ''
49− if id=$(${ghc.targetPrefix}ghc-pkg -v0 ${pdbArg} field ${p} id --simple-output); then
50+ if id=$(target-pkg ${pdbArg} field ${p} id --simple-output); then
5051 echo "--dependency=${p}=$id" >> $out/configure-flags
5152 fi
52− if ver=$(${ghc.targetPrefix}ghc-pkg -v0 ${pdbArg} field ${p} version --simple-output); then
53+ if ver=$(target-pkg ${pdbArg} field ${p} version --simple-output); then
5354 echo "constraint: ${p} == $ver" >> $out/cabal.config
5455 echo "constraint: ${p} installed" >> $out/cabal.config
5556 fi
5657 '';
5758
59+ envDep = pdbArg: p: ''
60+ if id=$(target-pkg ${pdbArg} field ${p} id --simple-output); then
61+ echo "package-id $id" >> $out/ghc-environment
62+ fi
63+ '';
64+
5865 configFiles = runCommand "${fullName}-config" { nativeBuildInputs = [ghc]; } (''
5966 mkdir -p $out
60− ${ghc.targetPrefix}ghc-pkg -v0 init $out/package.conf.d
67+
68+ # Calls ghc-pkg for the target platform
69+ target-pkg() {
70+ ${ghc.targetPrefix}ghc-pkg "$@"
71+ }
72+
73+ target-pkg init $out/package.conf.d
6174
6275 ${lib.concatStringsSep "\n" (lib.mapAttrsToList flagsAndConfig {
6376 "extra-lib-dirs" = map (p: "${lib.getLib p}/lib") component.libs;
@@ -69,11 +82,11 @@ let
6982 # Note: we need to use --global-package-db with ghc-pkg to prevent it
7083 # from looking into the implicit global package db when registering the package.
7184 ${lib.concatMapStringsSep "\n" (p: ''
72− ${ghc.targetPrefix}ghc-pkg -v0 describe ${p} | ${ghc.targetPrefix}ghc-pkg -v0 --force --global-package-db $out/package.conf.d register - || true
85+ target-pkg describe ${p} | target-pkg --force --global-package-db $out/package.conf.d register - || true
7386 '') nonReinstallablePkgs}
7487
7588 ${lib.concatMapStringsSep "\n" (p: ''
76− ${ghc.targetPrefix}ghc-pkg -v0 --package-db ${p}/package.conf.d dump | ${ghc.targetPrefix}ghc-pkg -v0 --force --package-db $out/package.conf.d register -
89+ target-pkg --package-db ${p}/package.conf.d dump | target-pkg --force --package-db $out/package.conf.d register -
7790 '') flatDepends}
7891
7992 # Note: we pass `clear` first to ensure that we never consult the implicit global package db.
@@ -81,6 +94,14 @@ let
8194
8295 echo ${lib.concatStringsSep " " (lib.mapAttrsToList (fname: val: "--flags=${lib.optionalString (!val) "-" + fname}") flags)} >> $out/configure-flags
8396
97+ # Provide a GHC environment file
98+ cat > $out/ghc-environment <<EOF
99+ clear-package-db
100+ package-db $out/package.conf.d
101+ EOF
102+ ${lib.concatMapStringsSep "\n" (p: envDep "--package-db ${p.components.library}/package.conf.d" p.identifier.name) component.depends}
103+ ${lib.concatMapStringsSep "\n" (envDep "") (lib.remove "ghc" nonReinstallablePkgs)}
104+
84105 '' + lib.optionalString component.doExactConfig ''
85106 echo "--exact-configuration" >> $out/configure-flags
86107 echo "allow-newer: ${package.identifier.name}:*" >> $out/cabal.config
@@ -117,9 +138,9 @@ let
117138 sed -i "s,dynamic-library-dirs: .*,dynamic-library-dirs: $dynamicLinksDir," $f
118139 done
119140 '' + ''
120− ${ghc.targetPrefix}ghc-pkg -v0 --package-db $out/package.conf.d recache
141+ target-pkg --package-db $out/package.conf.d recache
121142 '' + ''
122− ${ghc.targetPrefix}ghc-pkg -v0 --package-db $out/package.conf.d check
143+ target-pkg --package-db $out/package.conf.d check
123144 '');
124145
125146 finalConfigureFlags = lib.concatStringsSep " " (
@@ -147,6 +168,16 @@ let
147168 ++ component.configureFlags
148169 );
149170
171+ executableToolDepends = lib.concatMap (c: if c.isHaskell or false
172+ then builtins.attrValues (c.components.exes or {})
173+ else [c]) component.build-tools;
174+
175+ # Unfortunately, we need to wrap ghc commands for cabal builds to
176+ # work in the nix-shell. See ../doc/removing-with-package-wrapper.md.
177+ shellWrappers = withPackage {
178+ inherit package configFiles;
179+ };
180+
150181in stdenv.mkDerivation ({
151182 name = fullName;
152183
@@ -156,6 +187,7 @@ in stdenv.mkDerivation ({
156187 inherit (package) identifier;
157188 config = component;
158189 inherit configFiles;
190+ env = shellWrappers;
159191 };
160192
161193 meta = {
@@ -169,6 +201,7 @@ in stdenv.mkDerivation ({
169201 };
170202
171203 CABAL_CONFIG = configFiles + /cabal.config;
204+ GHC_ENVIRONMENT = configFiles + /ghc-environment;
172205 LANG = "en_US.UTF-8"; # GHC needs the locale configured during the Haddock phase.
173206 LC_ALL = "en_US.UTF-8";
174207
@@ -181,9 +214,7 @@ in stdenv.mkDerivation ({
181214 nativeBuildInputs =
182215 [ghc]
183216 ++ lib.optional (component.pkgconfig != []) pkgconfig
184− ++ lib.concatMap (c: if c.isHaskell or false
185− then builtins.attrValues (c.components.exes or {})
186− else [c]) component.build-tools;
217+ ++ executableToolDepends;
187218
188219 SETUP_HS = setup + /bin/Setup;
189220
@@ -235,6 +266,11 @@ in stdenv.mkDerivation ({
235266 ''}
236267 runHook postInstall
237268 '';
269+
270+ shellHook = ''
271+ export PATH="${shellWrappers}/bin:$PATH"
272+ ${toString shellHook}
273+ '';
238274}
239275# patches can (if they like) depend on the version and revision of the package.
240276// lib.optionalAttrs (patches != []) { patches = map (p: if builtins.isFunction p then p { inherit (package.identifier) version; inherit revision; } else p) patches; }