Skip to content

Commit 9b06216

Browse files
committed
Less derivations first cut
1 parent b4a36e9 commit 9b06216

File tree

5 files changed

+105
-86
lines changed

5 files changed

+105
-86
lines changed

builder/comp-builder.nix

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ pkgs, stdenv, buildPackages, ghc, lib, gobject-introspection ? null, haskellLib, makeConfigFiles, haddockBuilder, ghcForComponent, hsPkgs, compiler, runCommand, libffi, gmp, zlib, ncurses, nodejs }@defaults:
1+
{ pkgs, stdenv, buildPackages, ghc, lib, gobject-introspection ? null, haskellLib, makeConfigFiles, haddockBuilder, ghcForComponent, hsPkgs, compiler, runCommand, libffi, gmp, zlib, ncurses, nodejs, nonReinstallablePkgs }@defaults:
22
lib.makeOverridable (
33
let self =
44
{ componentId
@@ -162,7 +162,7 @@ let
162162
if configureAllComponents
163163
then ["--enable-tests" "--enable-benchmarks"]
164164
else ["${haskellLib.componentTarget componentId}"]
165-
) ++ [ "$(cat ${configFiles}/configure-flags)"
165+
) ++ [ "$(cat $configFiles/configure-flags)"
166166
] ++ commonConfigureFlags);
167167

168168
# From nixpkgs 20.09, the pkg-config exe has a prefix matching the ghc one
@@ -337,9 +337,9 @@ let
337337
config = component;
338338
srcSubDir = cleanSrc.subDir;
339339
srcSubDirPath = cleanSrc.root + cleanSrc.subDir;
340-
inherit configFiles executableToolDepends exeName enableDWARF;
340+
inherit executableToolDepends exeName enableDWARF;
341341
exePath = drv + "/bin/${exeName}";
342-
env = shellWrappers;
342+
env = shellWrappers.drv;
343343
profiled = self (drvArgs // { enableLibraryProfiling = true; });
344344
dwarf = self (drvArgs // { enableDWARF = true; });
345345
} // lib.optionalAttrs (haskellLib.isLibrary componentId) ({
@@ -380,10 +380,10 @@ let
380380
++ map haskellLib.dependToLib component.depends);
381381

382382
nativeBuildInputs =
383-
[shellWrappers buildPackages.removeReferencesTo]
383+
[ghc buildPackages.removeReferencesTo]
384384
++ executableToolDepends;
385385

386-
outputs = ["out" ]
386+
outputs = ["out" "configFiles"]
387387
++ (lib.optional enableSeparateDataOutput "data")
388388
++ (lib.optional keepSource "source")
389389
++ (lib.optional writeHieFiles "hie");
@@ -403,6 +403,15 @@ let
403403
'') + commonAttrs.prePatch;
404404

405405
configurePhase = ''
406+
echo A ${name}
407+
${configFiles.script}
408+
echo B ${name}
409+
wrappedGhc=$(mktemp -d)
410+
echo C ${name}
411+
${shellWrappers.script}
412+
echo D ${name}
413+
PATH=$wrappedGhc/bin:$PATH
414+
echo E ${name}
406415
runHook preConfigure
407416
echo Configure flags:
408417
printf "%q " ${finalConfigureFlags}
@@ -462,7 +471,7 @@ let
462471
${lib.optionalString (haskellLib.isLibrary componentId) ''
463472
$SETUP_HS register --gen-pkg-config=${name}.conf
464473
${ghc.targetPrefix}ghc-pkg -v0 init $out/package.conf.d
465-
${ghc.targetPrefix}ghc-pkg -v0 --package-db ${configFiles}/${configFiles.packageCfgDir} -f $out/package.conf.d register ${name}.conf
474+
${ghc.targetPrefix}ghc-pkg -v0 --package-db $configFiles/${configFiles.packageCfgDir} -f $out/package.conf.d register ${name}.conf
466475
467476
mkdir -p $out/exactDep
468477
touch $out/exactDep/configure-flags
@@ -578,10 +587,10 @@ let
578587
fi
579588
'';
580589

581-
shellHook = ''
582-
export PATH="${shellWrappers}/bin:$PATH"
583-
${shellHookApplied}
584-
'';
590+
#shellHook = ''
591+
# export PATH="${shellWrappers.drv}/bin:$PATH"
592+
# ${shellHookApplied}
593+
#'';
585594
}
586595
// haskellLib.optionalHooks {
587596
# These are the hooks that are not needed by haddock (see commonAttrs for the ones that
@@ -593,4 +602,4 @@ let
593602
// lib.optionalAttrs (hardeningDisable != [] || stdenv.hostPlatform.isMusl) {
594603
hardeningDisable = hardeningDisable ++ lib.optional stdenv.hostPlatform.isMusl "pie";
595604
});
596-
in drv; in self)
605+
in drv // { configFiles = drv.configFiles // configFiles; }; in self)

builder/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
let
44
# Builds a single component of a package.
55
comp-builder = haskellLib.weakCallPackage pkgs ./comp-builder.nix {
6-
inherit ghc haskellLib makeConfigFiles haddockBuilder ghcForComponent hsPkgs compiler;
6+
inherit ghc haskellLib makeConfigFiles haddockBuilder ghcForComponent hsPkgs compiler nonReinstallablePkgs;
77
};
88

99
haddockBuilder = haskellLib.weakCallPackage pkgs ./haddock-builder.nix {

builder/ghc-for-component-wrapper.nix

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,21 @@ let
2020
ghc = if enableDWARF then defaults.ghc.dwarf else defaults.ghc;
2121

2222
inherit (configFiles) targetPrefix ghcCommand ghcCommandCaps packageCfgDir;
23-
libDir = "$out/${configFiles.libDir}";
24-
docDir = "$out/share/doc/ghc/html";
23+
libDir = "$wrappedGhc/${configFiles.libDir}";
24+
docDir = "$wrappedGhc/share/doc/ghc/html";
2525
# For musl we can use haddock from the buildGHC
2626
haddock = if stdenv.hostPlatform.isLinux && stdenv.targetPlatform.isMusl && !haskellLib.isNativeMusl
2727
then ghc.buildGHC
2828
else ghc;
2929

30-
in runCommand "${componentName}-${ghc.name}-env" {
31-
preferLocalBuild = true;
32-
passthru = {
33-
inherit (ghc) version meta;
34-
inherit targetPrefix;
35-
baseGhc = ghc;
36-
};
37-
} (
38-
''
30+
script = ''
3931
. ${makeWrapper}/nix-support/setup-hook
4032
4133
''
4234
# Start with a ghc and remove all of the package directories
4335
+ ''
44-
mkdir -p $out/bin
45-
${lndir}/bin/lndir -silent ${ghc} $out
36+
mkdir -p $wrappedGhc/bin
37+
${lndir}/bin/lndir -silent ${ghc} $wrappedGhc
4638
rm -rf ${libDir}/*/
4739
''
4840
# ... but retain the lib/ghc/bin directory. This contains `unlit' and friends.
@@ -63,7 +55,7 @@ in runCommand "${componentName}-${ghc.name}-env" {
6355
''
6456
# Replace the package database with the one from target package config.
6557
+ ''
66-
ln -s ${configFiles}/${packageCfgDir} $out/${packageCfgDir}
58+
cp -r $configFiles/${packageCfgDir} $wrappedGhc/${packageCfgDir}
6759
6860
''
6961
# Set the GHC_PLUGINS environment variable according to the plugins for the component.
@@ -104,11 +96,11 @@ in runCommand "${componentName}-${ghc.name}-env" {
10496
+ ''
10597
for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do
10698
if [[ -x "${ghc}/bin/$prg" ]]; then
107-
rm -f $out/bin/$prg
108-
makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
99+
rm -f $wrappedGhc/bin/$prg
100+
makeWrapper ${ghc}/bin/$prg $wrappedGhc/bin/$prg \
109101
--add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
110-
--set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
111-
--set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
102+
--set "NIX_${ghcCommandCaps}" "$wrappedGhc/bin/${ghcCommand}" \
103+
--set "NIX_${ghcCommandCaps}PKG" "$wrappedGhc/bin/${ghcCommand}-pkg" \
112104
--set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
113105
--set "GHC_PLUGINS" "$GHC_PLUGINS" \
114106
--set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
@@ -117,11 +109,11 @@ in runCommand "${componentName}-${ghc.name}-env" {
117109
118110
for prg in "${targetPrefix}runghc" "${targetPrefix}runhaskell"; do
119111
if [[ -x "${ghc}/bin/$prg" ]]; then
120-
rm -f $out/bin/$prg
121-
makeWrapper ${ghc}/bin/$prg $out/bin/$prg \
122-
--add-flags "-f $out/bin/${ghcCommand}" \
123-
--set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \
124-
--set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \
112+
rm -f $wrappedGhc/bin/$prg
113+
makeWrapper ${ghc}/bin/$prg $wrappedGhc/bin/$prg \
114+
--add-flags "-f $wrappedGhc/bin/${ghcCommand}" \
115+
--set "NIX_${ghcCommandCaps}" "$wrappedGhc/bin/${ghcCommand}" \
116+
--set "NIX_${ghcCommandCaps}PKG" "$wrappedGhc/bin/${ghcCommand}-pkg" \
125117
--set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \
126118
--set "GHC_PLUGINS" "$GHC_PLUGINS" \
127119
--set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
@@ -132,8 +124,8 @@ in runCommand "${componentName}-${ghc.name}-env" {
132124
# Wrap haddock, if the base GHC provides it.
133125
+ ''
134126
if [[ -x "${haddock}/bin/haddock" ]]; then
135-
rm -f $out/bin/haddock
136-
makeWrapper ${haddock}/bin/haddock $out/bin/haddock \
127+
rm -f $wrappedGhc/bin/haddock
128+
makeWrapper ${haddock}/bin/haddock $wrappedGhc/bin/haddock \
137129
--add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \
138130
--set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}"
139131
fi
@@ -144,11 +136,27 @@ in runCommand "${componentName}-${ghc.name}-env" {
144136
+ ''
145137
for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do
146138
if [[ -x "${ghc}/bin/$prg" ]]; then
147-
rm -f $out/bin/$prg
148-
makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "--global-package-db=$out/${packageCfgDir}"
139+
rm -f $wrappedGhc/bin/$prg
140+
makeWrapper ${ghc}/bin/$prg $wrappedGhc/bin/$prg --add-flags "--global-package-db=$wrappedGhc/${packageCfgDir}"
149141
fi
150142
done
151143
152144
${postInstall}
153-
''
154-
)
145+
'';
146+
147+
drv = runCommand "${componentName}-${ghc.name}-env" {
148+
preferLocalBuild = true;
149+
passthru = {
150+
inherit (ghc) version meta;
151+
};
152+
nativeBuildInputs = [ghc];
153+
} (''
154+
configFiles=$(mktemp -d)
155+
${configFiles.script}
156+
wrappedGhc=$out
157+
${script}
158+
'');
159+
in {
160+
inherit script drv targetPrefix;
161+
baseGhc = ghc;
162+
}

builder/make-config-files.nix

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ let
1010
ghc = if enableDWARF then defaults.ghc.dwarf else defaults.ghc;
1111

1212
flagsAndConfig = field: xs: lib.optionalString (xs != []) ''
13-
echo ${lib.concatStringsSep " " (map (x: "--${field}=${x}") xs)} >> $out/configure-flags
14-
echo "${field}: ${lib.concatStringsSep " " xs}" >> $out/cabal.config
13+
echo ${lib.concatStringsSep " " (map (x: "--${field}=${x}") xs)} >> $configFiles/configure-flags
14+
echo "${field}: ${lib.concatStringsSep " " xs}" >> $configFiles/cabal.config
1515
'';
1616

1717
target-pkg = "${ghc.targetPrefix}ghc-pkg";
@@ -64,25 +64,8 @@ let
6464
libDeps;
6565
in lib.concatStringsSep "\" \"" xs;
6666
libs = lib.concatMapStringsSep "\" \"" (p: "${p}") libDeps;
67-
drv = runCommand "${ghc.targetPrefix}${fullName}-config" {
68-
nativeBuildInputs = [ghc];
69-
passthru = {
70-
inherit (ghc) targetPrefix;
71-
inherit ghcCommand ghcCommandCaps libDir packageCfgDir component;
72-
# Use ''${pkgroot} relative paths so that we can relocate the package database
73-
# along with referenced packages and still have it work on systems with
74-
# or without nix installed.
75-
relocatableConfigFiles = runCommand "${ghc.targetPrefix}${fullName}-relocatable-config" ''
76-
cp -r ${drv} $out
77-
chmod -R +w $out
78-
sed -i 's|/nix/store/|''${pkgroot}/../../../|' $out/${packageCfgDir}/*.conf
79-
${target-pkg} -v0 --package-db $out/${packageCfgDir} recache
80-
'';
81-
};
82-
} (''
83-
mkdir -p $out
84-
85-
${target-pkg} init $out/${packageCfgDir}
67+
script = ''
68+
${target-pkg} init $configFiles/${packageCfgDir}
8669
8770
${lib.concatStringsSep "\n" (lib.mapAttrsToList flagsAndConfig {
8871
"extra-lib-dirs" = map (p: "${lib.getLib p}/lib") (lib.flatten component.libs)
@@ -99,15 +82,15 @@ let
9982
${ # Copy over the nonReinstallablePkgs from the global package db.
10083
''
10184
for p in ${lib.concatStringsSep " " nonReinstallablePkgs'}; do
102-
find $ghc/lib/${ghc.name}/package.conf.d -name $p'*.conf' -exec cp -f {} $out/${packageCfgDir} \;
85+
find $ghc/lib/${ghc.name}/package.conf.d -name $p'*.conf' -exec cp -f {} $configFiles/${packageCfgDir} \;
10386
done
10487
''}
10588
10689
for l in "${cfgFiles}"; do
10790
if [ -n "$l" ]; then
10891
files=("$l/${packageCfgDir}/"*.conf)
10992
if (( ''${#files[@]} )); then
110-
cp -f "''${files[@]}" $out/${packageCfgDir}
93+
cp -f "''${files[@]}" $configFiles/${packageCfgDir}
11194
else
11295
echo "$l/${packageCfgDir} didn't contain any *.conf files!"
11396
exit 1
@@ -118,7 +101,7 @@ let
118101
if [ -n "$l" ]; then
119102
files=("$l/package.conf.d/"*.conf)
120103
if (( ''${#files[@]} )); then
121-
cp -f "''${files[@]}" $out/${packageCfgDir}
104+
cp -f "''${files[@]}" $configFiles/${packageCfgDir}
122105
else
123106
echo "$l/package.conf.d didn't contain any *.conf files!"
124107
exit 1
@@ -127,46 +110,46 @@ let
127110
done
128111
129112
${ # Note: we pass `clear` first to ensure that we never consult the implicit global package db.
130-
flagsAndConfig "package-db" ["clear" "$out/${packageCfgDir}"]
113+
flagsAndConfig "package-db" ["clear" "$configFiles/${packageCfgDir}"]
131114
}
132115
133-
echo ${lib.concatStringsSep " " (lib.mapAttrsToList (fname: val: "--flags=${lib.optionalString (!val) "-" + fname}") flags)} >> $out/configure-flags
116+
echo ${lib.concatStringsSep " " (lib.mapAttrsToList (fname: val: "--flags=${lib.optionalString (!val) "-" + fname}") flags)} >> $configFiles/configure-flags
134117
135118
${ # Provide a cabal config without remote package repositories
136119
''
137-
echo "write-ghc-environment-files: never" >> $out/cabal.config
120+
echo "write-ghc-environment-files: never" >> $configFiles/cabal.config
138121
''}
139122
140123
${ # Provide a GHC environment file
141124
''
142-
cat > $out/ghc-environment <<EOF
143-
package-db $out/${packageCfgDir}
125+
cat > $configFiles/ghc-environment <<EOF
126+
package-db $configFiles/${packageCfgDir}
144127
EOF
145128
''}
146129
147130
${ lib.optionalString component.doExactConfig ''
148-
echo "--exact-configuration" >> $out/configure-flags
149-
echo "allow-newer: ${identifier.name}:*" >> $out/cabal.config
150-
echo "allow-older: ${identifier.name}:*" >> $out/cabal.config
131+
echo "--exact-configuration" >> $configFiles/configure-flags
132+
echo "allow-newer: ${identifier.name}:*" >> $configFiles/cabal.config
133+
echo "allow-older: ${identifier.name}:*" >> $configFiles/cabal.config
151134
''}
152135
153136
for p in ${lib.concatStringsSep " " libDeps}; do
154-
cat $p/envDep >> $out/ghc-environment
137+
cat $p/envDep >> $configFiles/ghc-environment
155138
${ lib.optionalString component.doExactConfig ''
156-
cat $p/exactDep/configure-flags >> $out/configure-flags
157-
cat $p/exactDep/cabal.config >> $out/cabal.config
139+
cat $p/exactDep/configure-flags >> $configFiles/configure-flags
140+
cat $p/exactDep/cabal.config >> $configFiles/cabal.config
158141
''}
159142
done
160143
for p in ${lib.concatStringsSep " " (lib.remove "ghc" nonReinstallablePkgs')}; do
161144
if [ -e $ghc/envDeps/$p ]; then
162-
cat $ghc/envDeps/$p >> $out/ghc-environment
145+
cat $ghc/envDeps/$p >> $configFiles/ghc-environment
163146
fi
164147
done
165148
'' + lib.optionalString component.doExactConfig ''
166149
for p in ${lib.concatStringsSep " " nonReinstallablePkgs'}; do
167150
if [ -e $ghc/exactDeps/$p ]; then
168-
cat $ghc/exactDeps/$p/configure-flags >> $out/configure-flags
169-
cat $ghc/exactDeps/$p/cabal.config >> $out/cabal.config
151+
cat $ghc/exactDeps/$p/configure-flags >> $configFiles/configure-flags
152+
cat $ghc/exactDeps/$p/cabal.config >> $configFiles/cabal.config
170153
fi
171154
done
172155
''
@@ -199,24 +182,43 @@ let
199182
# Create a local directory with symlinks of the *.dylib (macOS shared
200183
# libraries) from all the dependencies.
201184
+ lib.optionalString stdenv.isDarwin ''
202-
local dynamicLinksDir="$out/lib/links"
185+
local dynamicLinksDir="$configFiles/lib/links"
203186
mkdir -p $dynamicLinksDir
204187
# Enumerate dynamic-library-dirs with ''${pkgroot} expanded.
205188
local dirsToLink=$(
206-
for f in "$out/${packageCfgDir}/"*.conf; do
189+
for f in "$configFiles/${packageCfgDir}/"*.conf; do
207190
(cat $f; echo) | sed -En '/^ ./{H;$!d} ; x ; /^dynamic-library-dirs:/ {s/^dynamic-library-dirs:// ; s/ /\n/g ; s/\n\n*/\n/g; s/^\n//; p}'
208191
done | sed 's|''${pkgroot}/../../../|/nix/store/|' | sort -u
209192
)
210193
for d in $dirsToLink; do
211194
ln -f -s "$d/"*.{a,dylib,so} $dynamicLinksDir
212195
done
213196
# Edit the local package DB to reference the links directory.
214-
for f in "$out/${packageCfgDir}/"*.conf; do
197+
for f in "$configFiles/${packageCfgDir}/"*.conf; do
215198
chmod +w $f
216199
echo >> $f
217200
sed -i -E "/^ ./{H;$!d} ; x ; s,^dynamic-library-dirs:.*,dynamic-library-dirs: $dynamicLinksDir," $f
218201
done
219202
'' + ''
220-
${target-pkg} -v0 --package-db $out/${packageCfgDir} recache
203+
${target-pkg} -v0 --package-db $configFiles/${packageCfgDir} recache
204+
'';
205+
drv = runCommand "${ghc.targetPrefix}${fullName}-config" {
206+
nativeBuildInputs = [ghc];
207+
} (''
208+
mkdir -p $out
209+
configFiles=$out
210+
${script}
221211
'');
222-
in drv
212+
in {
213+
inherit (ghc) targetPrefix;
214+
inherit script drv ghcCommand ghcCommandCaps libDir packageCfgDir component;
215+
# Use ''${pkgroot} relative paths so that we can relocate the package database
216+
# along with referenced packages and still have it work on systems with
217+
# or without nix installed.
218+
relocatableConfigFiles = runCommand "${ghc.targetPrefix}${fullName}-relocatable-config" ''
219+
cp -r ${drv} $configFiles
220+
chmod -R +w $configFiles
221+
sed -i 's|/nix/store/|''${pkgroot}/../../../|' $configFiles/${packageCfgDir}/*.conf
222+
${target-pkg} -v0 --package-db $configFiles/${packageCfgDir} recache
223+
'';
224+
}

0 commit comments

Comments
 (0)