@@ -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,8 +16,9 @@
1616, preBuild ? null, postBuild ? null
1717, preCheck ? null, postCheck ? null
1818, preInstall ? null, postInstall ? null
19+, shellHook ? null
1920
20−, doCheck ? component.doCheck || componentId.ctype == "test"
21+, doCheck ? component.doCheck || haskellLib.isTest componentId
2122, doCrossCheck ? component.doCrossCheck || false
2223, dontPatchELF ? true
2324, dontStrip ? true
@@ -27,7 +28,9 @@
2728}:
2829
2930let
30− fullName = "${name}-${componentId.ctype}-${componentId.cname}";
31+ fullName = if haskellLib.isAll componentId
32+ then "${name}-all"
33+ else "${name}-${componentId.ctype}-${componentId.cname}";
3134
3235 flagsAndConfig = field: xs: lib.optionalString (xs != []) ''
3336 echo ${lib.concatStringsSep " " (map (x: "--${field}=${x}") xs)} >> $out/configure-flags
@@ -44,18 +47,30 @@ let
4447 in map ({val,...}: val) closure;
4548
4649 exactDep = pdbArg: p: ''
47− 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
4851 echo "--dependency=${p}=$id" >> $out/configure-flags
4952 fi
50− 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
5154 echo "constraint: ${p} == $ver" >> $out/cabal.config
5255 echo "constraint: ${p} installed" >> $out/cabal.config
5356 fi
5457 '';
5558
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+
5665 configFiles = runCommand "${fullName}-config" { nativeBuildInputs = [ghc]; } (''
5766 mkdir -p $out
58− ${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
5974
6075 ${lib.concatStringsSep "\n" (lib.mapAttrsToList flagsAndConfig {
6176 "extra-lib-dirs" = map (p: "${lib.getLib p}/lib") component.libs;
@@ -67,11 +82,11 @@ let
6782 # Note: we need to use --global-package-db with ghc-pkg to prevent it
6883 # from looking into the implicit global package db when registering the package.
6984 ${lib.concatMapStringsSep "\n" (p: ''
70− ${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
7186 '') nonReinstallablePkgs}
7287
7388 ${lib.concatMapStringsSep "\n" (p: ''
74− ${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 -
7590 '') flatDepends}
7691
7792 # Note: we pass `clear` first to ensure that we never consult the implicit global package db.
@@ -79,6 +94,14 @@ let
7994
8095 echo ${lib.concatStringsSep " " (lib.mapAttrsToList (fname: val: "--flags=${lib.optionalString (!val) "-" + fname}") flags)} >> $out/configure-flags
8196
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+
82105 '' + lib.optionalString component.doExactConfig ''
83106 echo "--exact-configuration" >> $out/configure-flags
84107 echo "allow-newer: ${package.identifier.name}:*" >> $out/cabal.config
@@ -115,14 +138,14 @@ let
115138 sed -i "s,dynamic-library-dirs: .*,dynamic-library-dirs: $dynamicLinksDir," $f
116139 done
117140 '' + ''
118− ${ghc.targetPrefix}ghc-pkg -v0 --package-db $out/package.conf.d recache
141+ target-pkg --package-db $out/package.conf.d recache
119142 '' + ''
120− ${ghc.targetPrefix}ghc-pkg -v0 --package-db $out/package.conf.d check
143+ target-pkg --package-db $out/package.conf.d check
121144 '');
122145
123146 finalConfigureFlags = lib.concatStringsSep " " (
124147 [ "--prefix=$out"
125− "${componentId.ctype}:${componentId.cname}"
148+ "${haskellLib.componentTarget componentId}"
126149 "$(cat ${configFiles}/configure-flags)"
127150 # GHC
128151 "--with-ghc=${ghc.targetPrefix}ghc"
@@ -145,6 +168,16 @@ let
145168 ++ component.configureFlags
146169 );
147170
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+
148181in stdenv.mkDerivation ({
149182 name = fullName;
150183
@@ -154,6 +187,7 @@ in stdenv.mkDerivation ({
154187 inherit (package) identifier;
155188 config = component;
156189 inherit configFiles;
190+ env = shellWrappers;
157191 };
158192
159193 meta = {
@@ -167,6 +201,7 @@ in stdenv.mkDerivation ({
167201 };
168202
169203 CABAL_CONFIG = configFiles + /cabal.config;
204+ GHC_ENVIRONMENT = configFiles + /ghc-environment;
170205 LANG = "en_US.UTF-8"; # GHC needs the locale configured during the Haddock phase.
171206 LC_ALL = "en_US.UTF-8";
172207
@@ -179,9 +214,7 @@ in stdenv.mkDerivation ({
179214 nativeBuildInputs =
180215 [ghc]
181216 ++ lib.optional (component.pkgconfig != []) pkgconfig
182− ++ lib.concatMap (c: if c.isHaskell or false
183− then builtins.attrValues (c.components.exes or {})
184− else [c]) component.build-tools;
217+ ++ executableToolDepends;
185218
186219 SETUP_HS = setup + /bin/Setup;
187220
@@ -217,12 +250,12 @@ in stdenv.mkDerivation ({
217250 installPhase = ''
218251 runHook preInstall
219252 $SETUP_HS copy ${lib.concatStringsSep " " component.setupInstallFlags}
220− ${lib.optionalString (haskellLib.isLibrary componentId) ''
253+ ${lib.optionalString (haskellLib.isLibrary componentId || haskellLib.isAll componentId) ''
221254 $SETUP_HS register --gen-pkg-config=${name}.conf
222255 ${ghc.targetPrefix}ghc-pkg -v0 init $out/package.conf.d
223256 ${ghc.targetPrefix}ghc-pkg -v0 --package-db ${configFiles}/package.conf.d -f $out/package.conf.d register ${name}.conf
224257 ''}
225− ${lib.optionalString (componentId.ctype == "test") ''
258+ ${lib.optionalString (haskellLib.isTest componentId || haskellLib.isAll componentId) ''
226259 mkdir -p $out/${name}
227260 if [ -f "dist/build/${componentId.cname}/${componentId.cname}" ]; then
228261 cp dist/build/${componentId.cname}/${componentId.cname} $out/${name}/
@@ -233,6 +266,11 @@ in stdenv.mkDerivation ({
233266 ''}
234267 runHook postInstall
235268 '';
269+
270+ shellHook = ''
271+ export PATH="${shellWrappers}/bin:$PATH"
272+ ${toString shellHook}
273+ '';
236274}
237275# patches can (if they like) depend on the version and revision of the package.
238276// lib.optionalAttrs (patches != []) { patches = map (p: if builtins.isFunction p then p { inherit (package.identifier) version; inherit revision; } else p) patches; }