Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 15 min · revision 3

What makes a build reproducible

Learn what makes two builds of the same commit produce identical bytes: fixed timestamps with SOURCE_DATE_EPOCH, sorted inputs, pinned toolchains and locked dependencies.

Contentsand lesson path

What you will learn

  • Script a build that runs identically on a laptop and a CI runner, with pinned toolchain versions and locked dependencies.

01. The Core Principle: Bit-for-Bit Deterministic Verification

A build is reproducible (or deterministic) if, given the same source code, build environment and build instructions, anyone who runs it gets bit-for-bit identical output. This bit-level identity is verified by computing cryptographic checksums (such as SHA-256): if two independent builds of commit a1b2c3d produce differing checksums, the build is non-deterministic.

Reproducibility has practical benefits:

  • Supply chain integrity: if a release is reproducible, several independent builders can build the same source and compare checksums. If an attacker compromises one CI worker or compiler to insert a backdoor, that builder's checksum no longer matches the others, which exposes the tampering as soon as someone compares. Debian, Arch Linux and the Tor Project use this kind of independent rebuild.
  • Trustworthy caching: build tools such as Bazel, Nix and Docker BuildKit hash the inputs of a step to decide whether they can skip it. If a step produces different output from the same inputs, every step that depends on it looks changed too, so caches miss and builds take longer.
  • Debugging production crashes: to investigate a crash in production, engineers need to rebuild exactly the binary that is running, with debug symbols that match it.
  • Comparable multi-architecture builds: with timestamps and paths removed, builds for AMD64 and ARM64 differ only where the target architecture requires it, which makes their differences easier to review.

02. Anatomy of Non-Determinism: Timestamps, Ordering, and Host Leaks

Even when the source code does not change, many tools write details of the build machine and the moment of the build into their output. The most common sources of non-determinism are:

  • Embedded timestamps and file modification times (mtime): Standard archive formats (.tar, .zip, .jar) and compiler binary headers embed file creation timestamps down to the second or microsecond. Compiling the exact same codebase five minutes later produces a differing archive checksum because internal metadata records the current clock time. Similarly, the C/C++ preprocessor macros __DATE__ and __TIME__ embed the build time into the binary. (Go is a counter-example: it does not embed the build time, and its builds are reproducible by default; it does embed version-control metadata such as the commit and commit time unless you pass -buildvcs=false.) The open SOURCE_DATE_EPOCH specification addresses this: the build sets the variable to a fixed Unix time, usually the time of the last commit, and tools that support it use that time instead of the current clock.
  • Directory listing order: The POSIX call readdir() returns directory entries in an order defined by the filesystem implementation (for example hash order on ext4, creation order on some others), so the same tree can list differently on two machines. Tools that list directories, such as find, Python's os.listdir and many build tools, inherit that order. Shell globs such as src/*.c are sorted, but by the current locale's collation rules, which also differ between machines. If a build links object files or packs files in listing order, the output changes. Sort explicitly, with LC_ALL=C so the sort is by byte value.
  • Host environment and time zones: compilers and build scripts often read details of the host, such as the active user account ($USER), local hostname (uname -n), absolute build directory paths (/home/developer/app vs /home/runner/work), and system locale settings (LC_ALL / LANG). Furthermore, local timezones (TZ) cause compilers and zip utilities to record diverging local time offsets unless forced to TZ=UTC. If a compiler embeds absolute source file paths into debug tables (DWARF symbols), binaries compiled in different folders will not match unless you remap the paths (for example with GCC and Clang -ffile-prefix-map).
  • File permissions: archives record each file's mode (its read, write and execute bits). A new file gets its mode from the creating shell's umask, so one checkout has 0644 files on one machine and 0664 on another, and the archives differ. Set the modes while packing, as the script below does with --mode.
  • Floating dependency versions: Specifying loose version ranges in manifests (such as "express": "^4.18.0" or requests >= 2.25.0) causes builds to install whatever newer minor or patch release was published to public registries between two compilation runs.
Bash · build.sh: clamping timestamps and sorting the file list for a deterministic archive
#!/usr/bin/env bash
set -euo pipefail

# Use the time of the latest commit as the reference time
SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
export SOURCE_DATE_EPOCH

# Strip host locale variations
export LC_ALL=C
export TZ=UTC

mkdir -p dist

# Create the archive the same way on every machine
# 1. Sort files deterministically (byte order, because LC_ALL=C)
# 2. Set every modification time to SOURCE_DATE_EPOCH
# 3. Normalise user ownership and group IDs to root
# 4. Normalise file modes: everyone reads, owner writes, x only where
#    it already was, so the umask of the build machine does not matter
# 5. Fix the format to gnu: the pax format, the default on some systems,
#    can add access and change times
# 6. Compress with gzip -n so no name or time goes in the gzip header
find src/ -mindepth 1 | sort | tar \
  --create \
  --no-recursion \
  --format=gnu \
  --numeric-owner --owner=0 --group=0 \
  --mode=a=rX,u+w \
  --mtime="@${SOURCE_DATE_EPOCH}" \
  --files-from=- \
  | gzip -n > dist/package.tar.gz

echo "[OK] Archive written to dist/package.tar.gz"
sha256sum dist/package.tar.gz

03. Hermetic Isolation: Toolchain Pinning and Immutable Lockfiles

To get the same result on different laptops and CI workers, the build must depend as little as possible on what happens to be installed on the host. Two practices do most of the work:

  • Lock files with checksums: commit a lock file (package-lock.json, pnpm-lock.yaml, poetry.lock, Cargo.lock, go.sum). These lockfiles record not only exact resolved dependency versions but also cryptographic integrity hashes (such as sha512 or sha256) for every downloaded tarball. If a registry is later compromised, or an author replaces a published file in place, the hash no longer matches and the install fails instead of using the modified package. In CI, do not run commands that resolve versions again: npm install can rewrite package-lock.json, and pip install -r on a file of version ranges installs whatever is newest that day. Instead, use commands that install exactly what the lockfile says and fail when it no longer matches the manifest: npm ci, pnpm install --frozen-lockfile, poetry check --lock && poetry install (Poetry refuses to install from a lock file that is out of date), or, for pip, pip install --require-hashes -r requirements.txt with hashes generated by a tool such as pip-compile or uv. Note that poetry install --no-root is not a lock check: it only skips installing your own project package.
  • Pinned toolchains and base images: pin the compilers, runtimes and system libraries too. Instead of using whatever GCC, Node.js or Go version the host has, many pipelines build inside a container image pinned by its SHA-256 digest rather than a tag such as latest, which can move. Nix and Guix go further and build each package in a sandbox without network access; Bazel can sandbox build actions in a similar way.

Fixed timestamps, sorted inputs, locked dependencies and pinned toolchains remove the most common sources of difference. Whether they remove all of them, you find out only by building twice and comparing the checksums, as the lesson 04 lab does.

04. Check your understanding

Practice check · 3 questions

Timestamps, file order and hash checking

Three questions on sources of non-determinism and on making pip verify downloads.

Q1. Why does the Open Source Reproducible Builds initiative define and promote the `SOURCE_DATE_EPOCH` environment variable standard across build tools and compilers?
Q2. Two automated builds of the same commit run on different continuous integration workers produce different SHA-256 binary checksums. Investigation reveals that object files were linked in different orders. What caused this discrepancy?
Q3. Which command makes pip refuse to install any requirement that has no hash, as well as any download that does not match its recorded hash?
Practice only: not graded, not saved, not evidence.

05. Apply it

In your own repositories:

  • Adopt SOURCE_DATE_EPOCH: export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) in build wrappers and packaging scripts.
  • Sort files before archiving: pass file lists from find or a glob through sort under LC_ALL=C before they reach tar or zip.
  • Normalize file ownership: pass --numeric-owner --owner=0 --group=0 to GNU tar so that the user IDs of the build machine do not end up in the package.
  • Commit and freeze lock files: review them like source code, and use --frozen-lockfile, npm ci or the equivalent strict command in CI.
  • Pin compiler toolchains: build release artifacts in a container image pinned by digest rather than by a tag that can move.

06. Review prompt

Define what constitutes a reproducible build and explain its importance for software supply chain security and caching. Detail three distinct mechanisms through which non-determinism enters compiled artifacts, and describe how SOURCE_DATE_EPOCH and explicit file sorting remove two of them.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check