Home Explore Blog Models CI



nixpkgs

doc/build-helpers/special/checkpoint-build.section.md
036a67dbda9197a7ff1fa5a2944a7340043db97c7401442b0000000300000595
# pkgs.checkpointBuildTools  {#sec-checkpoint-build}

`pkgs.checkpointBuildTools` provides a way to build derivations incrementally. It consists of two functions to make checkpoint builds using Nix possible.

For hermeticity, Nix derivations do not allow any state to be carried over between builds, making a transparent incremental build within a derivation impossible.

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.

To change a normal derivation to a checkpoint based build, these steps must be taken:
  ```nix
  {
    checkpointArtifacts = (pkgs.checkpointBuildTools.prepareCheckpointBuild pkgs.virtualbox);
  }
  ```
  ```nix
  {
    changedVBox = pkgs.virtualbox.overrideAttrs (old: {
      src = path/to/vbox/sources;
    });
  }
  ```
  - use `mkCheckpointBuild changedVBox checkpointArtifacts`
  - enjoy shorter build times

## Example {#sec-checkpoint-build-example}
```nix
{
  pkgs ? import <nixpkgs> { },
}:
let
  inherit (pkgs.checkpointBuildTools) prepareCheckpointBuild mkCheckpointBuild;
  helloCheckpoint = prepareCheckpointBuild pkgs.hello;
  changedHello = pkgs.hello.overrideAttrs (_: {
    doCheck = false;
    postPatch = ''
      sed -i 's/Hello, world!/Hello, Nix!/g' src/hello.c
    '';
  });
in
mkCheckpointBuild changedHello helloCheckpoint
```

Chunks
8c69ac29 (1st chunk of `doc/build-helpers/special/checkpoint-build.section.md`)
Title: Incremental Builds with pkgs.checkpointBuildTools in Nix
Summary
The `pkgs.checkpointBuildTools` in Nix provides functionality for incremental builds, overcoming the hermetic nature of Nix derivations that typically prevents state transfer between builds. It achieves this by representing the previous build state as a derivation output, which can then be used for subsequent incremental builds. The process involves two main functions: `prepareCheckpointBuild` to prepare an existing derivation for checkpointing, and `mkCheckpointBuild` to perform an incremental build using a modified derivation and the prepared checkpoint artifacts, ultimately leading to shorter build times. An example demonstrates how to use these tools with `pkgs.hello` to modify its behavior incrementally.