Definition
A hermetic build is a build whose output can depend only on inputs declared in advance and identified exactly: source files, tools, configuration and outside data, each pinned by version and preferably by a digest, a fingerprint of the bytes computed by a cryptographic hash function §5.1. Everything else on the build machine is kept out of reach: the network, undeclared files, environment settings and installed programs. Hermetic means sealed airtight. A hermetic build fixes which inputs can matter; it does not by itself make the output bytes identical.
The idea
A build script downloads the newest version of a data schema from a server and generates code from it. Nobody changes the project’s files, yet the build run on Tuesday produces a different program from the build run on Monday, because the server’s copy of the schema changed overnight. The schema was an input of the build all along. It was simply never written down, and nothing held it fixed. Quieter hidden inputs act the same way, such as the current date or whichever compiler version happens to be installed.
A hermetic build turns every such hidden input into either a declared input or nothing. A sealed laboratory with an airlock is a fair picture: only what passes through the airlock gets in, and the airlock keeps a list. The picture stops being accurate in two respects. What happens inside the room can still vary from run to run, because parallel steps finish in different orders and programs can draw random numbers. And some things cross any seal: no common sandbox §2.30 stops a program from reading the clock or asking the processor for its model. Such inputs must be made irrelevant rather than blocked.
How it works
An ambient input is anything the build reads from the machine it runs on without declaring it. The common channels are:
Network: a library, package or data file downloaded by name; the server can return different bytes on different days.
Host files: headers, libraries and settings files installed on the machine or kept in the user’s home directory.
Environment variables: named text values that every process inherits from the process that started it, such as
LANGandLC_ALL, which select the locale (the rules for language, sorting order and number and date formats), andTZ, which selects the time zone.Host tools: the compiler, linker §12.4 or interpreter §2.13 that a script finds by searching the directories listed in the environment variable
PATH.Time: the current date and time from the system clock.
Machine identity: the host name, the user name, the absolute path of the build directory, the number of processors and the processor model.
Order and chance: the order in which the file system lists a directory, and random numbers from the operating system.
A hermetic build closes these channels in four steps.
Declare. List every input with an exact identity: the source files, each tool by version and digest, each outside package by version and digest, and a fixed set of environment values such as
LC_ALL=CandTZ=UTC. A version name alone is weaker than a digest, because a server can publish different bytes under the same version name.Fetch and verify. Before any build step runs, download the declared outside inputs, compute the digest of each, and stop if any digest differs from the declared one.
Isolate. Run each build step in a sandbox, a restricted environment in which the step sees only its declared files, starts with an empty environment plus the declared values, and has no network. A read of anything undeclared then fails, so a missing declaration breaks the build at once instead of silently taking the host’s value.
Make the unblockable irrelevant. Give tools a declared time, for example through the environment variable
SOURCE_DATE_EPOCH, which tools that follow this convention read in place of the clock, or remove every use of the time from the output. Name the target processor explicitly instead of letting the compiler detect the build machine’s processor.
Then test the seal: build the same declared inputs in two environments that differ in date, time zone, locale, user, directory path and installed tools, with no network, and compare the outputs byte for byte. A difference proves a leak. Agreement shows only that the channels which were varied did not matter in this test; a channel that was not varied, such as the processor model, can still leak.
Worked example
A project has three source files, lexer.c, main.c and Parser.c. Its build script lists them with the sort command and passes the list to the linker. The linker places the compiled code of the files one after another, in the order received, starting at address 0x1000, which is 4096 in decimal. The compiled code of Parser.c takes 0x500 bytes (1280), that of lexer.c takes 0x300 bytes (768), and that of main.c takes 0x80 bytes (128).
Machine 1 builds with LC_ALL=C, a locale that sorts by byte value. The upper-case letter P has byte value 0x50, below the lower-case l (0x6C) and m (0x6D), so the order is Parser.c, lexer.c, main.c. The parser code starts at 4096, the lexer code at (0x1500) and the code of main.c at (0x1800).
Machine 2 builds with LC_ALL unset and LANG=en_US.UTF-8, a locale whose sorting rules first compare letters without regard to case, so the order is lexer.c, main.c, Parser.c. The lexer code starts at 4096, the code of main.c at (0x1300) and the parser code at (0x1380). The parser code now starts at 4992 instead of 4096, so every instruction in main.c’s code that calls into the parser encodes a different target. The two programs differ in many bytes although every source file and every tool is identical. The locale was an undeclared input.
The hermetic version clears the environment and declares LC_ALL=C as part of the build. Both machines then sort by byte value and produce the machine 1 layout. Listing the three files explicitly, in a fixed order, in the build description is the stronger repair, because it removes the dependence on sorting altogether.
Stated precisely
Let be the declared inputs, each identified by its digest, and let be the declared environment values. Let be everything else the build machine could supply: other files, network replies, the clock, the host name, the installed tools and the processor. Write for the output of one build run. A build can contain internal chance, such as the order in which parallel steps finish, so treat as a random outcome. The build is hermetic with respect to a set of environments when, for every two environments and in and every possible output , where denotes probability. In words: changing anything outside and changes neither which outputs the build can produce nor how likely each one is.
The claim rests on three assumptions.
Complete isolation. The sandbox really blocks every channel it is said to block: undeclared files, network access and inherited environment variables.
Content identity. Each declared input is fixed by its digest, and the hash function is collision resistant §5.5, meaning that nobody can feasibly find a second byte string with the same digest.
No unblockable channel in the output. No step lets its output depend on something the sandbox cannot hide, such as the clock or the processor model.
If, in addition, the build has no internal chance, then in every environment in the output is one fixed artifact determined by and alone, and the build is reproducible in the sense of the Reproducible build §13.8 entry. Hermeticity removes influence from outside the declared inputs; determinism removes variation inside the build; byte-for-byte reproducibility §11.43 needs both.
Limits and common mistakes
A container is not a proof of hermeticity. A container image is a packaged file system that holds the tools. It fixes those tools only if the image is named by its digest rather than by a movable tag such as latest. A build inside the container can still download files, read directories mounted from the host, read inherited environment variables and read the clock.
Hermetic does not mean reproducible. A build that sees only declared inputs can still write the current time into its output, walk a hash table §14.7 in an order that changes from run to run, or record the results of parallel steps in the order they finish. Some build-system documentation, Bazel’s among it, describes hermeticity by its intended result, the same output from the same inputs. The two properties are kept apart here because each can fail while the other holds.
Hermetic does not mean correct or safe. A declared compiler that inserts a back door inserts it in every hermetic build. Declaring an input makes it visible and checkable; it does not make it trustworthy.
Build caches depend on hermeticity. A build system that reuses a stored output whenever the declared inputs match returns a wrong result if an undeclared input changed. One leaking step can therefore spread a wrong output to everyone who shares the cache.
Questions answered
Can network access, locale, time, or host tools affect output?
Yes. Each of the four can change the output of a build that is not hermetic, that is, one that does not restrict itself to inputs declared in advance, and a hermetic build must close each one explicitly.
Network access: a dependency or data file downloaded by name is whatever the server returns that day. A hermetic build downloads only declared inputs, checks each against its declared digest (a fingerprint of the bytes computed by a cryptographic hash function) before the build starts, and runs the build steps with no network.
Locale, the rules for language, sorting order and formats selected by environment variables such as
LANGandLC_ALL: it changes the sorted order of file names, and with it the order in which code is linked and therefore the program’s bytes; it also changes number formats, date formats and messages in generated text. A hermetic build clears the environment and declares a locale such asLC_ALL=C.Time: dates in version strings, the compiler’s date and time macros and the modification times stored in archives change with every run. No common sandbox hides the clock, so the build supplies a declared time, for example through the variable
SOURCE_DATE_EPOCH, or keeps the time out of the output.Host tools: a different version of the compiler, linker or archiver can produce different code or a different file layout. A hermetic build lets its steps see only declared tools, pinned by digest.
A container does not by itself close all four: it fixes the tools only if its image is pinned by digest, and it leaves the network, the clock and the environment open unless the build closes them. To check, build the same declared inputs in environments that differ in these four respects and compare the outputs byte for byte.
Related entries: Source closure (§13.6), Reproducible build (§13.8), Toolchain (§12.2), Content-addressed identity (§13.2).