2024-01-07 14:27:30 +01:00
|
|
|
{ lib
|
|
|
|
, buildPackages
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
# rudimentary support for cross-compiling
|
|
|
|
# see: https://github.com/NixOS/nixpkgs/pull/279487#discussion_r1444449726
|
|
|
|
inherit (buildPackages)
|
|
|
|
mktemp
|
|
|
|
rsync
|
|
|
|
;
|
|
|
|
in
|
|
|
|
|
2022-04-06 16:14:42 +02:00
|
|
|
rec {
|
|
|
|
/* Prepare a derivation for local builds.
|
|
|
|
*
|
2024-01-07 11:57:04 +01:00
|
|
|
* This function prepares checkpoint builds by storing
|
|
|
|
* the build output and the sources for cross checking.
|
2022-12-20 13:53:03 +01:00
|
|
|
* The build output can be used later to allow checkpoint builds
|
|
|
|
* by passing the derivation output to the `mkCheckpointBuild` function.
|
2022-04-07 14:47:56 +02:00
|
|
|
*
|
2024-01-07 11:57:04 +01:00
|
|
|
* To build a project with checkpoints, follow these steps:
|
|
|
|
* - run `prepareCheckpointBuild` on the desired derivation, e.g.
|
|
|
|
* checkpointArtifacts = prepareCheckpointBuild virtualbox;
|
|
|
|
* - change something you want in the sources of the package,
|
|
|
|
* e.g. using source override:
|
|
|
|
* changedVBox = pkgs.virtuabox.overrideAttrs (old: {
|
|
|
|
* src = path/to/vbox/sources;
|
|
|
|
* };
|
|
|
|
* - use `mkCheckpointBuild changedVBox checkpointArtifacts`
|
2022-04-07 14:47:56 +02:00
|
|
|
* - enjoy shorter build times
|
2022-04-06 16:14:42 +02:00
|
|
|
*/
|
2022-12-20 13:53:03 +01:00
|
|
|
prepareCheckpointBuild = drv: drv.overrideAttrs (old: {
|
2022-06-15 11:10:47 +02:00
|
|
|
outputs = [ "out" ];
|
2022-12-20 13:53:03 +01:00
|
|
|
name = drv.name + "-checkpointArtifacts";
|
2022-12-20 10:43:15 +01:00
|
|
|
# To determine differences between the state of the build directory
|
2024-01-07 11:57:04 +01:00
|
|
|
# from an earlier build and a later one we store the state of the build
|
2022-12-20 10:43:15 +01:00
|
|
|
# directory before build, but after patch phases.
|
|
|
|
# This way, the same derivation can be used multiple times and only changes are detected.
|
2024-01-07 11:57:04 +01:00
|
|
|
# Additionally, removed files are handled correctly in later builds.
|
2022-06-15 11:10:47 +02:00
|
|
|
preBuild = (old.preBuild or "") + ''
|
|
|
|
mkdir -p $out/sources
|
|
|
|
cp -r ./* $out/sources/
|
|
|
|
'';
|
|
|
|
|
2024-01-07 11:57:04 +01:00
|
|
|
# After the build, the build directory is copied again
|
2022-12-20 10:43:15 +01:00
|
|
|
# to get the output files.
|
2024-01-07 11:57:04 +01:00
|
|
|
# We copy the complete build folder, to take care of
|
|
|
|
# build tools that build in the source directory, instead of
|
|
|
|
# having a separate build directory such as the Linux kernel.
|
2022-06-15 11:10:47 +02:00
|
|
|
installPhase = ''
|
2023-12-12 13:21:01 +01:00
|
|
|
runHook preCheckpointInstall
|
2022-06-15 11:10:47 +02:00
|
|
|
mkdir -p $out/outputs
|
|
|
|
cp -r ./* $out/outputs/
|
2023-12-12 13:21:01 +01:00
|
|
|
runHook postCheckpointInstall
|
2022-04-06 16:14:42 +02:00
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
2022-12-20 13:53:03 +01:00
|
|
|
/* Build a derivation based on the checkpoint output generated by
|
2024-01-07 11:57:04 +01:00
|
|
|
* the `prepareCheckpointBuild` function.
|
2022-04-06 16:14:42 +02:00
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* let
|
2024-01-07 11:57:04 +01:00
|
|
|
* checkpointArtifacts = prepareCheckpointBuild drv;
|
2024-01-07 11:47:47 +01:00
|
|
|
* in mkCheckpointBuild drv checkpointArtifacts
|
2022-04-06 16:14:42 +02:00
|
|
|
*/
|
2024-01-07 14:27:30 +01:00
|
|
|
mkCheckpointBuild = drv: checkpointArtifacts: drv.overrideAttrs (old: {
|
2022-12-20 13:53:03 +01:00
|
|
|
# The actual checkpoint build phase.
|
2024-01-07 11:57:04 +01:00
|
|
|
# We compare the changed sources from a previous build with the current and create a patch.
|
|
|
|
# Afterwards we clean the build directory and copy the previous output files (including the sources).
|
|
|
|
# The source difference patch is then applied to get the latest changes again to allow short build times.
|
2022-06-15 11:10:47 +02:00
|
|
|
preBuild = (old.preBuild or "") + ''
|
|
|
|
set +e
|
2024-01-07 14:27:30 +01:00
|
|
|
sourceDifferencePatchFile=$(${mktemp}/bin/mktemp)
|
|
|
|
diff -ur ${checkpointArtifacts}/sources ./ > "$sourceDifferencePatchFile"
|
2022-06-15 11:10:47 +02:00
|
|
|
set -e
|
2024-01-07 14:27:30 +01:00
|
|
|
shopt -s dotglob
|
|
|
|
rm -r *
|
|
|
|
${rsync}/bin/rsync \
|
|
|
|
--checksum --times --atimes --chown=$USER:$USER --chmod=+w \
|
|
|
|
-r ${checkpointArtifacts}/outputs/ .
|
|
|
|
patch -p 1 -i "$sourceDifferencePatchFile"
|
|
|
|
rm "$sourceDifferencePatchFile"
|
2022-06-15 11:10:47 +02:00
|
|
|
'';
|
2022-04-06 16:14:42 +02:00
|
|
|
});
|
2024-01-07 11:47:47 +01:00
|
|
|
|
2024-01-07 14:27:30 +01:00
|
|
|
mkCheckpointedBuild = lib.warn
|
2024-01-07 11:47:47 +01:00
|
|
|
"`mkCheckpointedBuild` is deprecated, use `mkCheckpointBuild` instead!"
|
|
|
|
mkCheckpointBuild;
|
2022-04-06 16:14:42 +02:00
|
|
|
}
|