2023-12-12 14:12:15 +01:00
# pkgs.checkpointBuildTools {#sec-checkpoint-build}
2022-06-27 14:03:10 +02:00
2023-12-13 11:15:29 +01:00
`pkgs.checkpointBuildTools` provides a way to build derivations incrementally. It consists of two functions to make checkpoint builds using Nix possible.
2022-06-27 14:03:10 +02:00
2024-01-07 18:57:04 +08:00
For hermeticity, Nix derivations do not allow any state to be carried over between builds, making a transparent incremental build within a derivation impossible.
2022-06-27 14:03:10 +02:00
However, we can tell Nix explicitly what the previous build state was, by representing that previous state as a derivation output. This allows the passed build state to be used for an incremental build.
2023-12-12 13:19:21 +01:00
To change a normal derivation to a checkpoint based build, these steps must be taken:
2024-01-07 18:57:04 +08:00
- apply `prepareCheckpointBuild` on the desired derivation, e.g.
2023-12-12 14:12:15 +01:00
```nix
2024-03-27 19:10:27 +01:00
{
checkpointArtifacts = (pkgs.checkpointBuildTools.prepareCheckpointBuild pkgs.virtualbox);
}
2023-12-12 14:12:15 +01:00
```
2024-01-07 18:57:04 +08:00
- change something you want in the sources of the package, e.g. use a source override:
2023-12-12 14:12:15 +01:00
```nix
2024-03-27 19:10:27 +01:00
{
changedVBox = pkgs.virtualbox.overrideAttrs (old: {
src = path/to/vbox/sources;
});
}
2023-12-12 14:12:15 +01:00
```
2024-01-07 18:57:04 +08:00
- use `mkCheckpointBuild changedVBox checkpointArtifacts`
2023-12-12 14:12:15 +01:00
- enjoy shorter build times
2022-06-27 14:03:10 +02:00
2023-12-12 14:12:15 +01:00
## Example {#sec-checkpoint-build-example}
```nix
2025-04-11 09:36:54 +02:00
{
pkgs ? import < nixpkgs > { },
}:
2023-12-12 14:12:15 +01:00
let
2024-01-07 18:57:04 +08:00
inherit (pkgs.checkpointBuildTools)
prepareCheckpointBuild
mkCheckpointBuild
;
helloCheckpoint = prepareCheckpointBuild pkgs.hello;
2023-12-12 14:12:15 +01:00
changedHello = pkgs.hello.overrideAttrs (_: {
doCheck = false;
patchPhase = ''
2024-06-10 10:45:36 +02:00
runHook prePatch
2023-12-12 14:12:15 +01:00
sed -i 's/Hello, world!/Hello, Nix!/g' src/hello.c
2024-06-10 10:45:36 +02:00
runHook postPatch
2023-12-12 14:12:15 +01:00
'';
});
2025-04-11 09:36:54 +02:00
in
mkCheckpointBuild changedHello helloCheckpoint
2023-12-12 14:12:15 +01:00
```