Skip to content

Commit

Permalink
stdenv: Make it easier to correctly run phases in nix-shell
Browse files Browse the repository at this point in the history
Currently, it's a bit annoying to run build phases manually inside a
nix-shell. To run say, buildPhase, the invocation that works correctly
in all cases is:

eval "${buildPhase:-buildPhase}"

Which is quite a mouthful and non-obvious. Even worse, the obvious
thing to try, i.e.:

buildPhase

works _sometimes_ (i.e. when the package doesn't have a
`buildPhase = "...";`) but silently does the wrong thing in other cases.

Let's fix this by having the obvious thing work always.

To do that, we simply pull the logic of 'if $buildPhase exists as a variable,
then eval "$buildPhase", else do the default buildPhase' into the
buildPhase() function itself using a common helper:

buildPhase() {
    commonPhaseImpl buildPhase --default defaultBuildPhase --pre-hook preBuild --post-hook postBuild
}

As a bonus, we get to fix another current annoyance/footgun at the same
time, that is setting e.g. `buildPhase = "...";` makes the preBuild and
postBuild hooks not being run anymore.
  • Loading branch information
dezgeg committed Aug 29, 2018
1 parent b43c4d8 commit 87959f3
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 40 deletions.
20 changes: 16 additions & 4 deletions pkgs/build-support/release/ant-build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ stdenv.mkDerivation (
inherit jre ant;
showBuildStats = true;

postPhases =
["generateWrappersPhase" "finalPhase"];
preHook = ''
antSetupPhase() {
commonPhaseImpl antSetupPhase
}
generateWrappersPhase() {
commonPhaseImpl generateWrappersPhase
}
finalPhase() {
commonPhaseImpl finalPhase
}
'';

postPhases = ["generateWrappersPhase" "finalPhase"];

prePhases =
["antSetupPhase"];
prePhases = ["antSetupPhase"];

antSetupPhase = with stdenv.lib; ''
if test "$hydraAntLogger" != "" ; then
Expand Down
10 changes: 10 additions & 0 deletions pkgs/build-support/release/debian-build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ vmTools.runInLinuxImage (stdenv.mkDerivation (
{
name = name + "-" + diskImage.name + (if src ? version then "-" + src.version else "");

preHook = ''
installExtraDebsPhase() {
commonPhaseImpl installExtraDebsPhase
}
sysInfoPhase() {
commonPhaseImpl sysInfoPhase
}
'';

# !!! cut&paste from rpm-build.nix
postHook = ''
. ${./functions.sh}
Expand Down
8 changes: 8 additions & 0 deletions pkgs/build-support/release/nix-build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ stdenv.mkDerivation (
alias make="scan-build -o _clang_analyze_$name --html-title='Scan results for $name' make"
fi
initPhase() {
commonPhaseImpl initPhase
}
finalPhase() {
commonPhaseImpl finalPhase
}
${preHook}
'';

Expand Down
6 changes: 4 additions & 2 deletions pkgs/build-support/setup-hooks/autoreconf.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
preConfigurePhases+=" autoreconfPhase"

autoreconfPhase() {
runHook preAutoreconf
commonPhaseImpl autoreconfPhase --default defaultAutoreconfPhase --pre-hook preAutoreconf --post-hook postAutoreconf
}

defaultAutoreconfPhase() {
autoreconf ${autoreconfFlags:---install --force --verbose}
runHook postAutoreconf
}
6 changes: 3 additions & 3 deletions pkgs/build-support/setup-hooks/gog-unpack.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
unpackPhase="unpackGog"

unpackGog() {
runHook preUnpackGog
commonPhaseImpl unpackGog --default defaultUnpackGog --pre-hook preUnpackGog --post-hook postUnpackGog
}

defaultUnpackGog() {
innoextract --silent --extract --exclude-temp "${src}"

find . -depth -print -execdir rename -f 'y/A-Z/a-z/' '{}' \;

runHook postUnpackGog
}
14 changes: 14 additions & 0 deletions pkgs/development/haskell-modules/generic-builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ stdenv.mkDerivation ({

LANG = "en_US.UTF-8"; # GHC needs the locale configured during the Haddock phase.

preHook = ''
setupCompilerEnvironmentPhase() {
commonPhaseImpl setupCompilerEnvironmentPhase --pre-hook preSetupCompilerEnvironment --post-hook postSetupCompilerEnvironment
}
compileBuildDriverPhase() {
commonPhaseImpl compileBuildDriverPhase --pre-hook preCompileBuildDriver --post-hook postCompileBuildDriver
}
haddockPhase() {
commonPhaseImpl haddockPhase --pre-hook preHaddock --post-hook postHaddock
}
'';

prePatch = optionalString (editedCabalFile != null) ''
echo "Replace Cabal file with edited version from ${newCabalFileUrl}."
cp ${newCabalFile} ${pname}.cabal
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/ruby-modules/gem/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ stdenv.mkDerivation ((builtins.removeAttrs attrs ["source"]) // {
else
# Fall back to the original thing for everything else.
dontBuild=""
preUnpack="" postUnpack="" unpackPhase
preUnpack="" postUnpack="" defaultUnpackPhase
fi
runHook postUnpack
Expand Down
Loading

0 comments on commit 87959f3

Please sign in to comment.