Skip to content

DraftOwner sparkles:build-primitivesReviewed 2026-11-01

sparkles:build-primitives build graph — Specification ​

Abstract ​

The build graph compiles the packages of a dub monorepo directly, without running dub. Each library is compiled once per distinct configuration, and the result is reused by every package, test and application that needs it, across every git worktree on a machine. The graph reads the packages' dub.sdl recipes itself and turns each requested build into compile, archive, link, generator and test actions. It keys every action by the content of what it reads, including what it looked for and did not find. The same graph runs on a local executor with a shared content-addressed cache, or lowers to Nix derivations so that remote builders and binary caches can execute it.

Introduction ​

A Sparkles CI run tests about sixty dub packages and builds a dozen applications and many examples. Almost all of them depend on the same few libraries: sparkles:base, sparkles:wired, sparkles:event-horizon and sparkles:ui. A developer typically has several git worktrees of the repository checked out at once, each holding a nearly identical tree.

dub cannot share that work:

  • Its build identifier hashes the absolute paths of a package's sources, so the same library under a second worktree, or a second DUB_HOME, is compiled again.
  • Its staleness check compares modification times only, so copied or restored trees rebuild needlessly or, worse, skip a needed rebuild.
  • Several dub test processes sharing one DUB_HOME race on its package database.

The repository's workarounds show the cost. CI tasks were specified with a private DUB_HOME each, which recompiles sparkles:base once per dependent. The Nix example builder copies every source tree to one fixed path and flattens every timestamp so that one cache bundle can be reused.

This library takes over the part of dub that decides what to compile and whether it is already compiled. It resolves each package's recipe into explicit compiler invocations. It identifies each library build by everything that changes its object code, which this document calls its variant. For every compile, it records the exact set of files the compiler read and the search paths it found empty. An action whose recorded inputs are unchanged is never repeated, whichever worktree, directory or machine asks for it. Recipes stay dub recipes: humans, editors and dmd-lsp keep using dub on the same files, and dub itself is the oracle the resolver must agree with.

The build graph does not download or resolve registry packages; their pinned sources come from the repository's dub lockfiles. Which packages a command selects, how tasks are reported and how many run at once belong to the task DAG, whose executor runs this library's actions. The graph declares test actions and the conditions under which a test result may be reused, but owns no test policy. A per-module compilation mode, a remote-execution protocol of its own, and a general workflow language are out of scope. So are:

  • speculative execution: the plan is static, so the graph has no unresolved discovery to speculate on;
  • a resident compiler process, unless its state enters no key and removing it leaves every output identical;
  • build provenance, which would record an action's terminal inputs beside its result, never inside its key.

Section 1 states the invariants. Sections 2–4 define recipe resolution, variants and the action graph; §5 the action key and the input manifest; §6 the shared store; §7 generators; §8 toolchain identity; §9 test actions and result reuse; §10 the Nix backend; §11 the public surface; §12 file-system access. Delivery order lives in PLAN.md, oracles and evidence in testing.md, and the choices behind each section in decisions.md.

1. Invariants ​

  1. An action is a pure function of its key. Two actions with equal keys produce interchangeable outputs. Anything that can change an output is in the key, or the action is not cacheable.
  2. No ambient input. An action runs with an explicit argument vector, working directory, environment and compiler configuration file. It inherits nothing its key does not cover.
  3. Outputs do not name their worktree. No cached output embeds the absolute path of the worktree that built it, unless the action's key includes that path.
  4. dub agrees. For every package and request in scope, the resolver's settings equal what dub resolves, except where decisions.md records an intentional divergence.
  5. Reuse never changes a verdict. A cache hit and a cold build of the same key report the same diagnostics, exit status and outputs.
  6. The source tree is read, never written. Generated files live in action outputs.
  7. A result is keyed by its action's inputs, never by graph edges. An edge decides whether an action runs, not what it produces. A fallback compile that runs only after another action failed shares its key with the same compile run unconditionally.

2. Recipe resolution ​

A recipe is a package's dub.sdl, read through sparkles.wired.sdl, or a registry package's dub.json. Resolution turns a recipe, a selected configuration, a build type and a platform into resolved settings: the ordered source files, import paths, C import paths, string import paths, versions, debug versions, dflags, lflags, libraries and target properties that a compiler invocation needs.

The recipes in scope are the root recipe's sub-packages, the registry packages they depend on, and the single-file packages that apps/ci builds (BGR6). Any other recipe in the tree is out of scope until a consumer requests it. The reference semantics are dub's, at the revision the flake pins.

BGR1: Typed recipe reading. The resolver must read recipes through the repository's typed schema (sparkles.build_primitives.dub_manifest), from SDL and from JSON. It must reject a directive the schema does not name, with a diagnostic naming the file, line and directive. dub's x: extension tags are the only directives that are read and ignored.

BGR2: Supported directives. Resolution must support every directive, attribute and form the recipes in scope use. A directive outside the supported set is a resolution error. The supported set is listed in testing.md, generated by scanning the recipes in scope and checked by oracle 1. It includes at least:

  • the package, dependency and sub-package forms: inline subPackage blocks, the :name shorthand, and dependency with its path, version, repository, optional, default and platform attributes;
  • configurations, top-level platforms, build types resolved against the request's root recipe, and toolchainRequirements;
  • every build setting, each with dub's platform matching (the platform attribute in SDL, the name suffix in JSON);
  • an empty sourcePaths, and dub's glob forms in file lists;
  • dub's variables ($PACKAGE_DIR, $ROOT_PACKAGE_DIR, $<NAME>_PACKAGE_DIR, $ARCH, $PLATFORM, $PLATFORM_POSIX, $BUILD_TYPE, $DUB, $DUB_EXE, $DC, ${…} and $$), and environment references. An environment variable a value names is an input of every action whose settings contain it.

BGR3: dub's configuration steps. Resolution must reproduce the steps of dub's ProjectGenerator.configurePackages, in dub's order:

  1. Collect targets. A sourceLibrary dependency is folded into each consumer, merging all of its settings, and a non-executable target drops its mainSourceFile from its sources.
  2. Down: from each dependent to every dependency, its versions and debug versions, the build options dub classes as inherited, and the dflags dub classes as ABI-critical (filterABICriticalFlags).
  3. Across: a Have_<package> version, its name sanitized as dub does, for each package a target incorporates and each direct dependency.
  4. Up: from each dependency to its dependents, dflags, versions (so Have_ versions reach every transitive dependent), debug versions, version filters, import, C import and string import paths, injected source files, and, from library targets, libraries, frameworks, lflags and linker files.
  5. Override string imports from dependents, as dub does.
  6. Build type: add the build type's settings to every target, remove the unittests option from non-root targets, and extract build options from the dflags.

Downward versions are pruned per BGV1. A build option selects the divergent mode of D13 instead: a dependent's configuration-level versions and debug versions do not pass down, and resolution rejects a dependency whose sources test one. Both modes pass build-type options and ABI-critical flags down, and the mode is part of every variant key.

BGR4: Registry packages. A dependency that is not an in-tree sub-package must resolve to the source the repository's dub lockfiles (nix/dub-lock.json and nix/ci-dub-lock.json) pin:

  • With Nix: the source is a store path fetched by the lockfile's hash.
  • Without Nix: it is fetched by the lockfile's URL into the build store (§6) and verified by the lockfile's hash. An archive is checked against its flat SHA-256, and a git checkout against its NAR hash, which the build graph computes itself.

A path dependency a package states at its top level stands for its package below that package: a dependency below it that asks for the package by version resolves to that path, and no lock is asked, as dub selects it. A dependency missing from the lockfiles is a resolution error, and so is a locked source that a version range or repository pin naming it does not admit: every one, from every package depending on it, at the top level or in any configuration, whichever configuration the request builds. The resolver never resolves a version against a registry. An optional dependency is used exactly when the lockfiles pin it, or when it is a sub-package of its dependent's own package, whether or not it is default (D46).

BGR5: Library resolution. libs entries must resolve through pkg-config at planning time, with dub's rules: the lib<name> fallback, the split of the results into compiler and linker flags, and the -P prefixing of preprocessor flags.

  • Executable and dynamic-library targets: these receive the results, as dub gives them.
  • Library targets: these also receive the --cflags results of their own libs for their compiles. dub gives them none, and the divergence is recorded in decisions.md.
  • Separated operands: a preprocessor option written apart from its operand (-isystem /x) keeps it, joined into one flag (-P-isystem/x). dub keeps the option and drops the operand, and the divergence is recorded in decisions.md.

The resolved flags, the digests of the .pc files consulted, and the libraries the link flags resolve to are inputs of the actions that use them. A library that resolves to a store path may be identified by that path. BGK9 adds what the searches did not find.

BGR6: Single-file packages. A D source file with an embedded dub.sdl recipe (/+ dub.sdl: … +/) must resolve as a package whose recipe is the embedded one and whose only source is the file. In-tree dependencies it names by version, rather than by path, resolve to the in-tree package when the requester asks for that rewrite, as documentation examples do.

BGR7: The oracle. Resolution must agree with dub on every resolved field, for every package and request in scope (see testing.md). Fields before library resolution are compared with dub describe. Fields after it, and anything else dub describe does not expose, are compared with dub used as a library. A disagreement fails the gate. Accepting one is a decision record, never a silent exception.

Rationale: an independent resolver is what removes dub's path-hashed and mtime-based behaviour from the loop, and an oracle is what keeps it from silently drifting into a different build system.

BGR8: No hidden source edges. A package in scope must not reach into another package's directory through importPaths, sourcePaths, sourceFiles, stringImportPaths, cImportPaths or cSourcePaths without a dependency on it, in any configuration. Resolution reports each violation, naming the recipe line and the package reached. There is no exception: the test runner runs out of process, so a test binary links only its shim and no package source-includes its implementation. TRD1, TRD9 and TRM8 in docs/specs/test-runner/ define that model.

3. Variants ​

A variant is one way of compiling one package. Two builds of a package are the same variant exactly when they produce interchangeable object code.

BGV1: The variant key. A variant must be identified by:

  • the package;
  • its configuration, including one chosen by a consumer's subConfiguration;
  • its role (BGV2);
  • its resolved build options and flags, including coverage instrumentation, sanitizers and optimization, after inheritance (BGR3);
  • its versions and debug versions after inheritance, pruned as below;
  • the inheritance mode (BGR3);
  • the toolchain identity (§8);
  • the host platform (BGH3).

Two requests that resolve to equal variant keys must share one set of actions.

A request may carry a profile (RequestProfile): flags every compile and link of its variants takes after the recipe's, such as a sanitizer's, and, for its root, the variables its tests run with and the legs they run in. The flags are in each variant's key and every compile and link of it takes them after the recipe's (a test runner's own probe compiles do not), so a profiled request's variants, its dependency libraries' included, are keyed apart from an unprofiled one's and share no action or stored result with them; the variables and legs change only its tests' actions. Two requests whose roots resolve to one variant with different variables or legs must fail planning.

Pruning. A downward-inherited version or debug identifier must be kept only if a file the variant's compile can reach mentions it. Mentioned means it appears in a version (…) or debug (…) condition, or in a version = … or debug = … assignment:

  • in a reachable D source file, read as D tokens;
  • in any file under a string import root, whatever its extension, read as text.

The reachable D files are the variant's own source files and, transitively, each module one of them imports, found under its import roots in the order the compiler searches them. An import counts wherever it appears, under any version branch and inside any function body. A reachable file with a mixin whose string literals, joined, contain the word import makes every D file under the import roots reachable.

The scan has these further rules:

  • Scanned at plan time: the scan runs during planning, over the files present then, and the digest of the scanned file listing is part of the declared key (BGK1). A new file that mentions an identifier therefore re-plans the variant.
  • Generated roots: a root filled by a generator (§7) is not scanned. A variant with such a root keeps every inherited identifier.
  • Conditions built as strings: a variant must keep every inherited identifier if, in a reachable D file and outside a unittest block, the string literals of one mixin's arguments, joined in order, or the string literals that initialize one manifest constant (enum), contain version or debug followed by (, with optional whitespace between them. Planning reports each such mixin or constant. The one exemption is the mixin in sparkles.base.meta.isVersion, whose arguments the next rule checks.
  • The isVersion lint: planning must fail on a use of sparkles.base.meta.isVersion, outside a unittest block, whose argument is not a string literal naming one of D's predefined version identifiers. It names the file and line. A use is found by resolving the template's name through the module's imports, including selective and renamed imports and sparkles.base's public import of sparkles.base.meta. A unittest block compiles only in its own package's test root, which inherits nothing.

Rationale: dub passes a test root's versions down to every dependency, so without pruning, any root with a configuration-local version forks its whole dependency closure. Version identifiers are module-local in D, so an identifier no file the compile can reach mentions cannot change the object code. A mixin writes a condition that no file spells out, which D34 handles.

BGV2: Roles. A variant has one of these roles:

  • Test root: the variant a package's unittest binary compiles its own sources in, with -unittest, in the configuration dub's test runner copies: the one the request names, as dub test -c names it, else the package's unittest configuration, else its first library configuration, else its first executable one.
    • Custom runner: if that configuration is unittest and its target type is executable, the configuration is a custom test runner. Its mainSourceFile is kept and no test-root module is generated.
    • Generated entry point: otherwise its mainSourceFile is excluded, and a generated test-root module (§7) gives the binary its entry point. The module imports the package's own sources only, with no sourceLibrary files, skipping every package.d and the mainSourceFile. It names each module by its parsed module declaration, as dub does.
  • Dependency library: the variant a dependent links against. It uses the package's library configuration under the requester's build type, with -unittest removed, as dub does for every non-root package.
  • Application: a root compiled for shipping, with dependency libraries of the same build type: an executable, or a shared library (dynamicLibrary) that a program loads, such as an Android app's NativeActivity library. A shared library is linked -shared, its own code position independent, named as dub names it for its host (lib<name>.so), and given its file name as its DT_SONAME (on Darwin, @rpath/<name>.dylib as its install name) before the recipe's linker flags, as dub gives it. A Windows DLL is refused: its exports come from LDC's -shared compile defaults, which a graph that compiles before it links does not reproduce, so dub builds it.

A package that other selected packages depend on is therefore built at least twice when its tests run: once as a test root and once as a dependency library. A package nothing selected depends on is built only in its root role.

BGV3: Source libraries are not compiled alone. A sourceLibrary package must never produce a compile action of its own. Its sources are part of each consumer's compile and take that consumer's variant, so they compile with -unittest inside a test root. It may own generate actions (§7).

BGV4: The unittest layout rule. In a package compiled as a dependency library, version (unittest) must not change anything a dependent's compile or link can observe, outside unittest blocks. That covers:

  • a declaration visible outside its module (public, package or protected), at any level;
  • an aggregate member of any visibility: a field, field initializer, virtual method, destructor, postblit or copy constructor;
  • an enum member or value, or an alias target;
  • any version (unittest) inside a template, or inside the body of a function whose body a dependent compiles: an auto-returning or attribute-inferred function, or one marked pragma(inline, true);
  • a module-scope import of a package that the library configuration does not depend on.

A private, non-template, module-level declaration that only the module's own unittest blocks use is exempt. Without -unittest such a declaration does not exist, so code outside the tests that used it would not compile as a library: a version (unittest) with no else branch is how the lint tells it. The rule also applies to a sourceLibrary compiled into both a test root and a dependency library of one binary. A module that is version (unittest) throughout belongs in the test root alone, outside the library configuration.

Rationale: -unittest turns on version (unittest) in every module a test root imports, including its dependencies' modules. A test root then sees a different declaration, layout, mangled attribute set or template instance than the dependency library it links against was compiled with. The linker merges template instances across the two, so a template's test-only branch also leaks into library code.

BGV5: The layout lint. Planning must report every violation of BGV4 in the packages it plans, naming the file and line, and must fail the plan on one in a package of the repository. A violation in a registry package, or in a clone outside the tree or in a .deps directory that a single-file package names by path, is reported and does not fail it: this repository cannot fix it. The lint works on the parsed module before conditional compilation is resolved, so it sees both branches of every version (unittest) and every uninstantiated template body. Only the import rule reads the variant's own versions: an import in a branch of a user-defined version the variant does not compile is not the library's. See D58.

4. The action graph ​

An action is one tool invocation with declared inputs and outputs.

BGA1: Action kinds. The graph must use exactly these kinds:

  • compile: one compiler invocation over all of a variant's own modules;
  • archive: a static library from compile outputs;
  • link: an executable from objects and archives;
  • generate (§7);
  • test (§9).

A kind outside this list is an error.

BGA2: One compile per variant. A library or root variant must compile its own modules in one compiler invocation. A dependent's compile reads its dependencies' sources through import paths; it does not wait for their archives. A link action waits for the archives it links.

BGA3: Declared outputs. Every action must declare the outputs it produces, relative to an action-private output directory on the store's file system. A declared output is a file or a directory tree; a generator whose file names depend on its inputs declares its output directory. The action must fail if it produced an output outside its declarations, or did not produce a declared one. Files it writes into its measurement directory (observability OBD8) are not outputs.

BGA4: Explicit environment. Every action must run with the environment its key declares (invariant 2). That environment is complete, not a change to the executor's own: an ambient DFLAGS, CC, NIX_CFLAGS_COMPILE or any other variable is not inherited. A caller that wants extra compiler flags requests them as variant options. The declared environment must:

  • set SOURCE_DATE_EPOCH to a fixed value, so that __DATE__, __TIME__ and __TIMESTAMP__ do not follow the clock, in D and in ImportC sources;
  • fix the variables that change diagnostics (TERM, NO_COLOR) and the target (MACOSX_DEPLOYMENT_TARGET);
  • set PATH explicitly, empty for compile, archive and link actions.
  • set TMPDIR to the action's own measurement directory, since the C driver an LDC link runs writes temporary files there (D39).
  • set ZERO_AR_DATE=1 for a Darwin link, since ld64 otherwise records each object file's modification time in the debug map (its N_OSO entries), and an object written again with the same bytes would change the binary.

A compile or link must name its C compiler driver and its linker by absolute path (-gcc= and --linker= on LDC). They are the files toolchain identity covers (BGT1), so the compiler cannot pick another from a search path. An action passes no @file response file, because LDC reads an environment variable of the same name instead when one is set.

Rationale: with a PATH, LDC finds cc by searching it, so toolchain identity could hash one driver while the compiler runs another.

BGA5: Relocatable invocation.

  • Working directory: a compile action must run in the repository root, passing sources and in-tree paths as root-relative paths.
  • Prefix map: with a compiler that supports it, the action must map the absolute repository root, without a trailing separator, to . in debug information, __FILE__, __FILE_FULL_PATH__, coverage registration and the C preprocessor's output (--ffile-prefix-map=<root>=. on LDC, -ffile-prefix-map=<root>=. on the toolchain's DMD, D63). So every in-tree path the outputs record is relative to the root, such as ./libs/base/src/…, and so is the debug information's compilation directory, which is the root itself. Store paths name no worktree, so this map leaves them alone; BGA11 maps the toolchain's for another reason.
  • Fallback: with a compiler that cannot map them, the repository root's absolute path is part of the action key.

BGA6: Argument normalization. Before an action's argument vector enters its key (§5), the absolute repository root must be replaced by a fixed placeholder, the action's private output directory by another, and its measurement directory by a third. No other argument is rewritten. So a prefix-map argument naming a worktree hashes equally in every worktree.

BGA7: Diagnostics are outputs. An action's standard output, standard error and exit status must be recorded with its outputs and replayed on a cache hit (invariant 5). That includes a hit on a failed action that BGA9 publishes.

An action's measurements are recorded beside its diagnostics, outside every key and every declared output (observability OBD8).

BGA8: Graph exposure. The build graph must expose its actions as task nodes with stable ids, typed actions and resource claims, so that the task-DAG executor can run them (see task DAG). Their form is the one observability OBD2 states, so that measurements line up across toolchain bumps; BGA8 adopts it at E2 (see PLAN.md). Edges between actions are ordinary onSuccess task edges. Every action but a test looks its result up first (task-DAG TDM7): it is charged its claim only when the lookup misses and it executes, so the lookups of a run whose lookups hit run together, up to the run's capacity at once, as their prerequisites and file claims allow. The default weights are:

  • compile 1, since one LDC compile runs one thread (task-DAG TDM10);
  • archive 1;
  • link 2;
  • generate 1;
  • test 2.

A test action must pass, as the test runner's maximum thread count, the weight a run admits of its claim (the claim clamped to the run's capacity), or the thread count its caller binds in its place, which may exceed the claim (ResourceClaim.threads; ci CIC11), and a link action its claimed weight as the linker's thread count (--threads on ld.lld). The plan names a test's thread count by a placeholder that the run replaces as it lowers the action, so neither the action's key nor a kept plan (BGP2) changes with the capacity, which follows the host's load. The count selects no test: through the runner core every non-serial test runs on a worker whatever the count, and a test that needs the process to itself is @serial, run after the others whatever the count (test-runner TRS2). A test's key holds the placeholder, not the count: a verdict reused under BGX3 stands for every count, as the tests it selects do.

A node's label is <kind> <package>:<role>, a test leg's name after the role, and the first six digits of its variant key (compile doc-view:test-root 48f533), the name a transcript gives it (task DAG TDO3). A test node's output policy is lastLineOnSuccess, so a passing leg shows its summary line; every other node's is tailOnFailure, so a build action's output (deprecations, cache notes) shows only when it fails. What the build graph reports of an action whatever its outcome is a notice (task DAG TDM21), which every policy shows: a publication that failed (BGS2), a nondeterministic result (BGS3, BGS5), a corrupt entry a lookup discarded (BGS6), a replacement that waits for a lease (BGS7), an input edited during a compile (BGK7). A line that explains a failure, such as a read outside the declared roots (BGK8), stays among the action's lines, which the failure shows. So does a cache note (a miss's explanation, a result a digest record serves), which only explains an outcome; the run's statistics record both (BGS5, BGS8).

BGA9: Which results are published. A result must be published only for an action that ran to completion and exited on its own:

  • compile, archive and link: with any exit status. A failed compile is keyed by the manifest its dependency output lists, as a successful one is, or conservatively (BGK6) when the compiler writes none. A hit on it replays the failure (BGA7), so a deterministic error is not compiled again.
  • generate and test: with a zero exit status only. A test's result is published only when BGX3 allows it to be reused.

An action ended by a signal, a timeout or a cancellation, or one that exited zero without producing its declared outputs (BGA3), must not be published. See D30.

Rationale: a compile killed by the kernel's out-of-memory killer, or by fail-fast, failed for a reason its key does not cover. Replaying that failure would report a verdict that no cold build of the key gives (invariant 5).

BGA10: Deterministic archives. An archive action must produce a non-thin archive whose member headers are deterministic: zero timestamps, owner and group, and a fixed mode (D on llvm-ar). An archive's and a link's outputs are then functions of the bytes they consume. So a compile whose outputs are byte-identical to an earlier one leaves every archive, link and test key downstream of it unchanged. That is the graph's early cutoff.

Rationale: a thin archive records its members' paths, not their bytes, so a changed member would leave the archive, and every link key that digests it, unchanged.

BGA11: Toolchain paths are remapped. With a compiler that supports a prefix map, a compile must map the store paths of the compiler's import directories to a fixed prefix in debug information and __FILE__, with the same mechanism BGA5 uses. Those directories hold the druntime and Phobos sources. The toolchain's prebuilt druntime and Phobos must be built with the same map. No debug information or __FILE__ in an output then names a druntime or Phobos source by its store path. A linked binary's interpreter and run path still name the C library, which this requirement does not cover.

Rationale: a Phobos assert records its source file's store path, so every binary that links Phobos keeps the whole toolchain alive in its Nix closure (D40).

BGA12: Every action has a time limit. Each process action the graph runs must carry a limit on its run time, after which the executor kills it and reports it as timed out. By default a test's limit is 20 minutes and every other action's is 30, so only a hang reaches one. A caller may set other limits, or none. A limit is not part of any key: changing it rebuilds nothing, and a timed-out action is never published (BGA9).

Rationale: a test that waits forever on a connection its sandbox never delivers held a whole-repository run for five and a half hours, with every other action finished. A limit turns a hang into a failure that names the action.

5. Keys and the input manifest ​

Each action has a key, a SHA-256 digest that identifies its result. A compile cannot know its exact inputs until it has run, so compile keys are computed in two steps, as ccache's direct mode does.

BGK1: The declared key. An action's declared key must cover:

  • the action kind;
  • its argument vector, normalized per BGA6;
  • its declared environment, each value that names a producer's outputs ($IN) by their digest, as an argument names them. A generator that states no environment of its own runs in its caller's, finding its tools on that PATH, and has no declared key;
  • the toolchain identity (§8);
  • the content digests of its declared inputs. For a compile, these are the variant's own source files, the recipe files that resolved it, the generator outputs it reads, and the digest of the file listing that version pruning scanned (BGV1). For a link or archive, they are the outputs it consumes;
  • the content digest of every regular file an argument names outside the Nix store, in the worktree (by a path relative to the working directory, by $ROOT/… or absolutely) or not: a plain operand (one with a /, or a bare linker input such as libfoo.a or libfoo.so.1), -conf=, and a linker's files through -L and -Xcc=-Wl, (an operand); but the program it runs, which the toolchain identity covers. A path with a .. is resolved by the file system, never collapsed first. An argument whose inputs no key can establish leaves the action without a declared key: an option apart from its value (-L -lfoo) or spelled -L=… or --L…, a response file (@file), a linker script (-T, --script, --default-script, -dT), the linker's state stack, -L or -l split from its operand, --library, a symbol-ordering file, -R/--just-symbols, a version script or dynamic list (which lld also finds along -L), a separate -conf or --conf, a -P search or read option the C roots do not read (-imacros, -iprefix, -F, --embed-dir and the like) outside the store, and a C driver option through -Xcc= outside an allow-list (a target, machine, code, warning, macro, optimization, debug or standard flag, a search root in the store, -B, which names the toolchain's linker (§8), and -Wl, parts each checked as -L passes one). A library found outside the store that is no ELF or Mach-O object, archive or Darwin stub (a linker script, a thin archive) leaves the link without a declared key. A configuration outside the store that an argument names after the toolchain's (-conf=), whose import and library directories no key reads, leaves the action without a declared key. One that is there but cannot be read, or that a link leads to outside the worktree, leaves the action without a declared key: it runs every time. A file outside the worktree is read where its links lead, the link named in the key. A named file's absence, and a named directory's existence, are facts of the whole-run record (BGS12); a run whose argument is a special file, is read through a link, or holds a .., keeps no record;
  • a format version. It changes with any change to this specification's key layout, to how a manifest is extracted from the compiler's dependency output, or to argument normalization (BGA6).

Rationale: a fix to manifest extraction leaves every key's layout as it was, yet results published before it may rest on incomplete manifests. Only a new format version retires them.

BGK2: The input manifest. A compile must record a manifest of everything the compiler resolved. It lists every file the compiler read, each as a root-relative or store path with a content digest, BLAKE3 over the bytes (D48):

  • its own sources;
  • the modules it imported;
  • its string imports;
  • the C headers its ImportC sources included.

It also lists every absent candidate: each path the compiler's D import or string import search probed and found missing before the path it resolved to, and every path probed by a lookup that failed. An absent candidate also stands for each missing parent directory on its path. The C preprocessor cannot report its failed lookups (__has_include, the includer's own directory, #include_next), so a compile with ImportC sources also keys on the paths and content digests of every file under its C include roots (-P-I, -P=-I, and the -isystem, -idirafter and -iquote forms), its C sources' and forced includes' directories and its D import roots, as BGK6 does, and on each forced include (-P-include). It does so for a compile with C sources, and for one with a C include root in the worktree, whose D modules may import C ones. A root named absolutely beneath the worktree is a worktree root. Such a compile with a root outside the worktree and the Nix store, or one whose walk could not read every entry, has no declared key: it runs every time, and publishes nothing.

The manifest comes from the compiler's dependency output. On LDC, that is --makedeps plus the fork's extensions for C headers and absent candidates (decisions.md).

  • Compiler-reported digests: where the compiler's dependency output carries the content digest of the bytes it read for each file, as the LDC fork's does, the manifest must record those digests rather than digests taken afterwards. A file's entry then describes what the compile read, whenever the file changed.
  • Search probes: where the compiler reports its searches as their factors, as the LDC fork's --makedeps-probes does (the ordered import and string-import roots, the order a module's candidates are tried in, and where each lookup resolved), the manifest must record those factors instead of the expanded list. They stand for the same absent candidates: for a module, every candidate under each root before the one it resolved in, and those before its own in that root, or every candidate under every root when the lookup failed; for a string import, its name under each string-import root before the one it resolved in, or under every one when it failed; and each path the compiler lists alone. The format is read line by line on LF only, since a name may hold any other byte, a CR included. One compile's probes are a few thousand short lines where the expanded list is tens of thousands of long ones. The store keeps each set of probes once, under its digest, and a manifest names it by that digest, so the manifests of one declared key, and the compiles that search the same roots for the same modules, share it.
  • Immutable roots: an absent candidate inside the Nix store is never recorded, and never checked. A store path's contents are fixed by its name, so no file can appear in one. A store object that holds a link to a mutable directory breaks that assumption, and is outside what a key covers, as it always was.
  • Checking absence: on Linux, a lookup may decide that absent candidates are still absent from directory listings, rather than probing each path. A listing must be reused only while the directory's identity, modification time and status-change time are those it was read with, the directory lies on the file system whose clock the run read (BGK5's probe), and both times predate that clock reading, taken before the listing, by BGK5's two seconds; otherwise the directory is listed again. A run validates each directory once, after its parent, and a directory may be validated through a handle kept open since it was listed while its parent's listing is the one it was opened from: renaming a directory into place changes its parent's modification time. A directory must be opened from its parent's handle, entry by entry, and never through a symbolic link: a link on the way leaves the directory unknown, since a change beneath the link's target changes nothing the walk validates. A directory that its parent's current listing lacks is absent; any other missing directory must be looked up again by each run, and a directory gone or unknown vouches for nothing beneath it. Candidates are normalized first; one with a .. component is looked up itself. A walk, open or listing that fails leaves the directory unknown, and each candidate in it is looked up itself, as is every path in the run directory or the cache, which the run's own actions write. On other systems, whose file systems may fold case, every candidate is looked up itself.
  • Once per run: a run keeps its first observation. It decides whether a path exists, a lookup's candidates under one root and a set of probes at most once, and digests each root BGK6 reads at most once; every manifest and key that names them takes that answer, as each file's digest is taken once per run (BGK5). A change made after the run first observed a path is the next run's to see; BGK7's check of what a compiler read reads afresh. What the run's own actions may write is decided each time it is checked: a path in the run directory or the cache, named by a placeholder, absolutely or, with the cache inside the worktree, relative to it, a lookup whose candidates lie there, one that climbs with .., and a set of probes holding any of them. A file in the cache that a BGK6 root holds is read afresh too.

BGK3: Several manifests per declared key. The store must keep, under one declared key, a bounded list of manifests. Each manifest leads to its own result key: the digest of the declared key and the manifest's entries. A lookup must take the most recently used manifest whose every file entry matches the current digest and whose every absent candidate is still absent, and reuse its result. The list's bound is 16 by default, with least recently used manifests evicted first.

Rationale: two worktrees on different revisions of sparkles:base produce the same declared key for sparkles:ui's compile but different manifests. Both must stay reusable.

BGK4: Precision. Editing a file that no compile read must not change that compile's result key. So editing a module of sparkles:base that sparkles:ui never imports does not rebuild sparkles:ui's variants. Creating a file at an absent candidate's path must change the result key.

BGK5: Digests are cached by identity. File digests may be cached by the tuple (device, inode, size, modification time, change time). A cached digest must not be trusted when the file's modification or change time is not older than the time the digest was recorded, minus two seconds; such a file is rehashed. This is git's rule for racily clean index entries, with a margin that covers coarse kernel timestamps. It is also the subtree cache's rule (FSC2 and FSC3): one statement and one validator serve every reader of a file's digest, the manifest, an input state's id (BGW9 of the fileset-native workspace) and a Nix source, and a file's content is hashed at most once per run, whichever of them needs it.

  • The clock: the recording time must be read from the clock of the file system that holds the file: the modification time of a probe file the build graph has just written there, in the store or in the worktree's private git directory. Where it can write no probe on that file system, it must not cache digests of files there.
  • Future times: a file whose modification time is later than the recording time must be rehashed whenever it is read, and reported.

Rationale: the process clock and a file system's timestamps can disagree, on a network file system most of all, and a racy check that compares the two misses the edits it exists to catch.

BGK7: Inputs edited during a compile. A compile must detect that a file it read changed while it ran, and a compile that detects one must not publish (D31):

  1. Before the compile: for every file the most recently used manifest of its declared key lists, it records a stat proxy (inode, size, modification and change times in nanoseconds) and the file's digest. It takes its start time from the file system's clock, as BGK5 does: the modification time of a probe file it writes and stats.
  2. After the compile: it stats every file of the new manifest. A conflict is any of:
    • a file recorded before the compile whose stat proxy changed. When only the change time changed, as a new hard link makes it, the file is rehashed, and it conflicts only if its digest changed;
    • a file the compile read that no earlier manifest listed, whose modification or change time is at or after the start probe's;
    • with compiler-reported digests (BGK2), a reported digest that differs from the one recorded before the compile.
  3. On a conflict: the outputs serve the current run, which reports the action as unstable input. Nothing from the action is written to the local store or to any other: no result, manifest or digest-cache entry. The next run therefore looks the action up afresh.
  4. No rerun loop: the action must not be rerun until it is stable. A command in a watch mode may rerun it once.

The stat check has a residual gap: an edit that keeps a file's size and lands within one timestamp tick of the read leaves the proxy unchanged. Only compiler-reported digests (BGK2), or compiling from a snapshot (BGS11), close it.

Rationale: a manifest digests each file after the compiler read it. An editor that saves a module mid-compile would pair the new digest with object code built from the old content, and every worktree on the machine would reuse that wrong result. Rerunning until stable can loop for as long as an editor autosaves.

BGK6: Incomplete dependency output. If a compile's dependency output is missing, unparseable, or comes from a compiler that cannot list C headers or absent candidates, the action must key on its declared inputs plus the paths and content digests of every file under every import, C import and string import root it searches: the input state id of its declared read set (BGW7 and BGW9 of the fileset-native workspace), which needs no listing code of its own. Its roots are those -I=, -I, -J= and -J name, one named absolutely beneath the worktree among them; a root that is a link, and a link beneath a root, is read where it leads in the worktree, and a run that walks a linked root keeps no whole-run record (BGS12). A root outside the worktree and the Nix store, whose files the key would have to digest from elsewhere, or a walk that could not read every entry (a link out of the worktree or to a directory, an entry it could not list, stat or read), leaves the compile without a declared key: it runs every time, and publishes nothing. A failure keyed so (BGA9) is kept under the same conditions, and not kept otherwise. Otherwise its result is published under that key and reported as conservatively keyed: the run's statistics name the task, and a failed compile keyed so because it wrote none of the dependency output it was asked for says so among its lines (BGA8), and again when a hit replays them (BGA7).

BGK8: Reads outside the declared roots. A compile whose manifest lists a file outside all of the following must fail, and its result must not be published. In the worktree, the test is membership in the action's declared read set (BGW7 of the fileset-native workspace), whose roots these are:

  • the source, import, C import and string import roots of its variant and of its transitive dependencies, including the C import paths pkg-config resolved (BGR5);
  • the outputs of the generators it depends on;
  • the toolchain (§8).

The diagnostic names the file and the package whose directory holds it.

Rationale: an ImportC #include "../other/x.h" reaches another package with no recipe edge. BGR8 reads recipes, so it cannot see the reach, and selection would not rebuild the package's consumers when the header's owner changes.

BGK9: Library search inputs. Library resolution and linking must key on what their searches did not find, as compiles do (BGK2):

  • pkg-config: the value of PKG_CONFIG_PATH, the directories searched, and each .pc path found absent before the one used are inputs of every action that uses the query's results.
  • The link: a link's declared key covers its -L search order. For every library it links, including those that pragma(lib) names in its inputs, the key covers the path the library resolved to, the path its links lead to and the digest of its bytes when it lies outside the Nix store, and the candidates absent before it in that order. The candidates are the linker's: lib<name>.so, then .a (.dylib, .tbd, then .a on Darwin), only the archive for a -l after -Bstatic (or -static, -dn, -non_shared) and before the next -Bdynamic, and for a pragma(lib) name in the mode the arguments end in, or for every one under the driver's -static, and the file itself for -l:<file>. A library outside the store is read where its links lead, and a run that resolves one through a link keeps no whole-run record (BGS12), which holds no fact of a link. A library found where its bytes cannot be read, or a search directory that climbs out of the worktree, leaves the link without a declared key. Each library is resolved over the search order as the linker does: a -l library at planning, and a library that pragma(lib) names when the link is keyed, once the objects naming it exist. The names an object or archive carries are read from its bytes when it is published and kept in its result's record, so a link reads them from the record, its digest record included, rather than from the file. One whose names can be read from neither is covered as unread. The toolchain's own library directories are covered by toolchain identity (§8).

Rationale: a .pc file or a library created earlier on a search path changes what an action uses without changing anything it read.

6. The shared store ​

The store's layout is:

DirectoryHolds
<store>/results/<key>/a published result: outputs, diagnostics, digests
<store>/manifests/<key>the manifest list of a declared key
<store>/records/<key>the digest record of an evicted result (BGS5)
<store>/preimages/<task>the declared-key preimage a task last published (BGS8)
<store>/leases/<key>/the marks of processes moving a result (BGS5)
<store>/last-usedtouched by every run that opens the store (BGS10)
<store>/holders/<holder>one file per process that has the store open, its lease journal (BGS4, BGS10)
<store>/ended/the lease journals of processes that ended, until eviction folds them (BGS5)
<store>/recency.logwhen a lease on each result was last written, as folds record it (BGS5)
<store>/eviction-duepresent while the store grew since an eviction began (BGS5)
<store>/staging/entries being written
<store>/trash/entries being evicted or discarded
<store>/prune-tokenreplaced by each change that can make a manifest prunable
<store>/prunedthe prune token a complete scan of the lists started with
<store>/probes/<digest>a probe block (BGK2), shared by every manifest naming it
<store>/fast/<run key>the whole-run record of a run that reused every keyed action (BGS12)

BGS1: One store per machine. Results must live in one store per user and machine, under $XDG_CACHE_HOME/sparkles/build (~/.cache/sparkles/build when unset), shared by every worktree and process. <store> is that directory's v<N>/ subdirectory, where <N> is the store's layout version (BGS10). Revisions with different layouts run at once, each in its own v<N>/. Everything else ci keeps beneath $XDG_CACHE_HOME/sparkles is either inside a layout directory or names its own format (the kept plans, whose request digest names the ci binary; the kept toolchain identities, whose key names it too (BGT5); the memory peaks; the last ci test run, whose name also names the layout its keys are in), so no revision misreads or overwrites another's.

BGS2: Atomic publication. A result must become visible only complete.

  • Staging: its outputs, diagnostics and manifest are written to a private entry under staging/, so the publishing rename never crosses a file system.
  • Results: a result is published by one rename to results/<key>.
  • Manifest lists: a manifest list is replaced by writing a new list file and renaming it over the old one.
  • Durability: before the publishing rename, the staged entry's files and its directory are flushed to stable storage (fsync). After a crash, a published entry is therefore complete or absent.
  • Failure: a failure to stage or to publish must not change the action's outcome. The outputs serve the current run from where they were written, and the failure is reported.

A reader never observes a partial entry.

BGS3: Concurrent producers. Two processes may compute the same result key at once. Correctness must not depend on a lock: the first rename wins.

  • The loser adopts the winner: the other producer must move its own entry to trash/ and continue from the published entry's outputs and diagnostics, and so must every action that consumes its result.
  • Nondeterminism: if the loser's output digests differ from the published entry's, it must report the action as nondeterministic, naming the result key and each differing output.

Concurrent manifest-list updates may lose an entry, which costs only a rebuild. A per-key advisory lock may suppress duplicate work.

Rationale: archive and link keys digest the bytes they consume (BGK1). A loser that kept its own outputs would key its dependents on bytes that no store holds.

BGS4: Leases. A process must lease every result it looks up or publishes, and must hold the lease until its run ends.

  • Lease journals: a lease is a line, the result key, in the holder's journal, holders/<holder>: one file per open store, named by its process id, its start time and the store's number in the process, and on Linux its PID namespace. The process creates it as it opens the store, and only if no file has the name, beginning it with a line of its own, # and a random generation, then appends one write per lease, or one for many. A line not yet ended by a line break is no lease. A remover reads a journal through one handle, and parses it again whenever its generation or its length changed: a journal only grows within its generation. Ending its leases moves the journal to ended/ and begins a new one; ending its run moves it there too, or removes it when it leased nothing. A lease the journal cannot take (a full file system takes no byte) is an empty file, leases/<key>/<holder>, which removers read beside the marks, removed when the leases end. A holder is gone when no process has its id in its namespace; a holder in another namespace, such as a container sharing the cache, counts as running, so its leases and marks (BGS5) wait for garbage collection in its own namespace. A gone holder's journal leases nothing, and garbage collection moves it to ended/.
  • Lookup: an action's lookup happens when the executor admits it (TDM7), not when the graph is planned, so it sees what concurrent producers published meanwhile; a run its whole-run record serves decides every one when it starts instead (BGS12). Lookup creates the lease, then checks that no other process marks the key as moving its result (BGS5), then that results/<key> exists, then reads it. A marked key, a missing entry and one whose lease could not be created are misses. Publication leases its key before it renames, and publishes nothing while another process marks it; the run then reads its own outputs (BGS2).
  • Read only: outputs are read in place, and no process modifies a published entry. An entry leaves results/ only by the renames that eviction, removal, replacement and corruption handling make.

BGS5: Eviction. The store must enforce two configured size bounds, each by evicting its least recently used entries first:

  • Results: output bytes, 20 GiB by default.
  • Records: manifest lists, probe blocks, digest records and key preimages, 1 GiB by default. Large results therefore never evict the small records that later lookups need.

A result's recency is the last time a lease on it was written: the latest modification time of a journal, live or ended, that lists it (BGS4), or the time <store>/recency.log holds for it. Eviction and garbage collection fold the ended journals into that log, each published result keeping the later of its logged time and its latest journal's, and remove the journals only once the log holding them is written; two folds at once may lose one's times, which only reorders eviction. A result no lease names is dated by its publication. The store does not read access times (D38). A process that ends leaves its journal in ended/, and one that would leave more than 64 there folds them first. Eviction follows these rules:

  • Off the publish path: eviction runs when a bound is exceeded, in a task of its own, and evicts down to 90 % of the bound. Publication never waits for it. <store>/eviction-due is made before a publication, and after a probe block, a preimage or a manifest list is written, and by an eviction that a live lease held over a bound. An eviction claims it as it begins, renaming it eviction-due.<holder>, and removes the claim once it ends; it removes an ended holder's claim too. ci evicts after a run that published nothing only while the mark or a claim is there, so neither a publisher nor an evictor that crashed leaves the store over its bound.
  • On a full disk: a consumer that finds the store's file system under its reserve may evict the results down to a lower bound, as far as needed to restore it (ci's CIC23); the same order and the same leases apply. Such an eviction reclaims: each result it moved to trash/ is removed, so its bytes are free when it returns, and another evictor's entries in trash/ are left to it. ci cache gc reclaims the same way.
  • Leased overshoot: a result with a live lease is never evicted, so the store may exceed its bound while leases hold it. The overshoot is reported, and eviction resumes when the leases end.
  • Digest records: evicting a result removes its output bytes but keeps a record under its result key: its output digests and sizes, the library names its objects and archives carry (BGK9), its diagnostics and its exit status. A consumer whose key needs only those digests (BGK1) reads them from the record, and a lookup that needs the bytes is a miss. An action whose result is only recorded is executed only when an action that misses needs its bytes. If that execution's output digests differ from the record, the new digests replace it and the action is reported as nondeterministic, as BGS3 reports; the action that needed its bytes, keyed on the recorded digests, runs and publishes nothing. A digest record serves a success only, and not for an action whose published entry the lookup found corrupt or could not read. A store that cannot be opened (its directory a file, or refused) is no store: the run goes on without one.
  • Manifests: a manifest whose result is neither present nor recorded is a miss, and is dropped from its list. A list left empty is removed. A list records each manifest's result key beside it, so dropping one and recording a used one compare keys and never rehash a manifest. A manifest can become droppable only when a result leaves without a digest record, or a digest record goes. Each of those first replaces <store>/prune-token. A prune reads the token, scans every list, and writes the token into <store>/pruned only when it read and wrote each list; it is skipped while pruned holds the current token, because it could drop nothing. A change during a scan, a crash in one, or a failed read leaves pruned behind, and the next prune runs. Eviction writes a result's digest record before it moves the result to trash/. A list already led by the manifest a hit used is not rewritten; the hit leases its result, and that lease's recency dates the list.
  • Records' order: over the records' bound, eviction drops, until it is at 90 % of it: the manifests no result serves; the probe blocks no list names; the least recently written preimages, which only explain misses; the least recently written digest records, then the manifests those left without a result; and last whole manifest lists, least recently used first (the later of a list's write and its leading manifest's lease), and the blocks those named. A compile whose list went misses once, and records its manifest again. A probe block goes only when no list names it and it was not written in the last hour: a list that names a block writes it again first, so a block in use is never old, and a sweep whose scan of the lists could not read one removes nothing. A lookup through a manifest whose block is gone misses, and the compile writes the block again.

An eviction of a result:

  1. marks the key as moving its result: leases/<key>/.moving-<holder>, named by the process as its lease is;
  2. skips a result that another live process's journal leases or its mark marks, and one whose lease directory, journal or holders cannot be read;
  3. writes its digest record;
  4. renames the result into trash/ under a unique name;
  5. removes the mark.

The mark is made before the journals are read, and a lookup reads it after it appended its lease (BGS4), so of the two at least one sees the other: either the eviction sees the lease and skips the result, or the lookup sees the mark and misses. A journal is read whole each time, and parsed again when its generation or length changed (BGS4); a line still being written is no lease: the lookup writing it then meets the mark. A journal that is not a regular file, or cannot be read, counts as held; only one gone has ended. No process reads an entry that moves, and none is moved back. Of two processes that mark one key at once, at least one skips it, and a mark made after a replacement moved the result meets its lease. A mark is removed only by its own process, or by name once its holder has ended, so reclaiming a dead mark never removes a live one. A crash leaves a mark that holds its key's lookups to misses until a lookup that can tell its holder ended, garbage collection, or the next mark on the key reclaims it.

Garbage collection removes trash/, stale staging/ entries and the marks of processes that have ended, moves their journals to ended/ and folds them, and removes older layouts as BGS10 allows.

Rationale: archive and link keys digest their inputs' outputs. Without the record, evicting an archive whose link result survives would force the archive to be rebuilt only to recompute a digest, and the link would then hit.

BGS6: Corruption. An entry whose recorded digests do not match its files must be treated as a miss. It moves even while another process leases it: its bytes are already wrong for every reader. It is renamed into trash/ before it is deleted, so a concurrent publisher never meets a half-deleted directory, and a diagnostic is reported. The rename is made under the key's mark (BGS5), and only of the entry examined, by its identity on its device: an entry published in its place meanwhile stays. Verification leases each result it rehashes, as a lookup does (BGS4). Corruption is detected at three points:

  • Every lookup checks that each of the entry's declared outputs exists, with its recorded type, size and execute bit, that a success records every file its action declares, and that the entry is a directory whose record is a regular file that parses as its key's: its task, declared, result and status lines, through its last line, end. Every file the store reads it opens as a regular file only, so a FIFO never blocks a lookup. A digest record (BGS5) serves a success only, and not for an action whose published entry the lookup found corrupt or could not read. Publication renames an entry in whole, its record written first, and every key names the record format (BGK1), so an entry or record of another kind, a record missing, malformed, cut short or another key's, and an output gone or of another size are corruption. A record in another format is another build's to judge (BGS10), and what exists but cannot be read, for a permission or an I/O error, says nothing wrong of the entry: each is only a miss, the entry stays, and the action's outputs serve the run unpublished. A corrupt entry's publication in place takes its key once it moved. One repair of a key runs at a time (leases/<key>/.repair, written whole and made new; one a process that ended left is taken over), and a publication never renames over an entry, an empty directory included (a no-replace rename, VFB8; where the file system has none, only while the key has no entry): so the entry a repair examined is the one at the key until it moves it, and a move found to have taken another goes back. Verification takes an output it cannot read for no corruption, as a lookup does.
  • A sample of hits, one in 64 by default and chosen at random, also has every recorded digest rehashed. The fraction is configurable, and a value of one rehashes every hit.
  • Verification (BGS7) rehashes every entry it is asked to check.

BGS7: Inspection and repair. The store must provide operations to:

  • report its size, and list entries by key, task and age;
  • collect garbage;
  • verify entries, by rehashing every recorded digest (BGS6);
  • remove the results of one result key, one task, one package, or one toolchain identity, by moving them to trash/;
  • replace a result: execute its action with lookup bypassed, move the published entry to trash/, and publish the new one;
  • run the determinism check (BGS9).

Removal and replacement must wait for every live lease on an entry held by another process, as eviction does (BGS5 steps 1, 2 and 5), and report the entries they wait for. A replacement keeps its mark until its own entry took the published one's place, so no lookup reads the key in between. A replacement that waits leaves the published result and publishes nothing; a notice reports it, and the run's statistics name the task, so that ci cache replace fails, as it does when nothing took the published result's place. The operating process's own leases do not hold it back.

apps/ci exposes them as ci cache commands: stats, list, gc, verify, remove, replace, explain and prune-layouts (BGS10). The store's top directory, and the directory under which ci keeps its run directories, each hold a CACHEDIR.TAG file, as the Cache Directory Tagging Specification defines, so backup tools skip them.

Rationale: a wrong result that passes every digest check, such as one keyed on an incomplete manifest, is never evicted by age while it keeps being used. Removal by toolchain identity retires every result a defective compiler built.

BGS8: Explaining a miss. For every action it publishes, the store must keep the preimage of its declared key under the action's task id (BGA8): each component BGK1 lists, by name, with its value or digest. A task's newer preimage replaces its older one. On a miss, the build graph must classify it as one of:

  1. declared: no manifest list exists for the declared key. The explanation names each component that differs from the task's recorded preimage, or says that none is recorded.
  2. manifest: a list exists but no manifest matches. The explanation names, for the most recently used manifest, its first file entry whose digest differs, or its first absent candidate that now exists.
  3. evicted: a manifest matches, but its result is gone and no record serves the lookup.

Each miss is reported with its class and its explanation: in the run's statistics, by class and per task, and as a cache note among the action's lines (BGA8). ci cache explain <task> plans the selection the last ci test beneath the store recorded (its packages, legs and coverage, so that its task ids and keys are that run's), or every package when none is recorded; it takes an id, a prefix of one, or a task's handle (compile:b70e70, a key prefix with the kind and leg); and it plans the task and prints the classification of its lookup against the current tree and store, executing nothing. A value marked redacted (TDM4) must appear in a preimage only as a digest keyed with a secret private to the store, so that the preimage shows that the value changed without letting its value be guessed and checked.

BGS9: The determinism check. The store must provide a check that re-executes a given set of actions cold and unshared:

  • Cold: each action runs with its own lookup bypassed, on the inputs its published result was keyed on. A compile reads its sources from the tree, which must still match the result's manifest; otherwise the action is reported as not checkable, and is not compared. An archive, link or test reads the outputs it consumes from the shared store.
  • Unshared: its outputs go to a private store, created empty for the check and discarded after it, and never reach the shared store or a binary cache.

The check compares each action's output digests, manifest, diagnostics and exit status with its published result. A difference must fail the check, name the action, its result key and each differing item, and remove the published result as BGS7's removal does. BGS3's nondeterminism reports feed the same report.

Rationale: a determinism check must run cold and unshared, or it tests the cache (reproducibility). Which actions are checked, and when, is the consumer's policy; ci checks a sample on every CI run (CIC13).

BGS10: Format versions. The store's layout version names its directory (BGS1), so two revisions of the build graph with different layouts use separate stores on one machine. Each entry, record and manifest list must record the format version of its own encoding. An entry in a format the reading process does not know is a miss, and is left in place for a process that does. A change to how a list is read, such as the probe blocks and cached result keys of layout 2, takes a layout of its own, so an older revision, which would read the new lists without them, never reads or prunes them. A change to the protocol by which processes share an entry, such as the marks of layout 3 (BGS5) and the lease journals of layout 4 (BGS4), takes one too, so a revision that does not follow it never shares a store with one that does.

Revisions take turns on one machine: worktrees and agents on different revisions run at once, or one after another. Every run that opens a layout makes a holder file, holders/<holder>, its lease journal, which it moves to ended/ when it ends, and touches its last-used stamp. A layout's last use is the latest modification time of its directory and of each entry in it: the stamp, its clock probe (BGK5), which every run writes as it opens the store, and its holders/, leases/, staging/, results/ and other directories, which every entry made or removed in them moves. So a revision that writes no stamp or holder file dates its layout from its opening on. A run collects, on its own, each older layout unused for a week (configurable), so revisions that alternate keep theirs; a run that published nothing looks for them at most once an hour, by build/layouts-checked. ci cache prune-layouts [--older-than <age>] collects the older layouts unused for that long, 0 meaning however recent, but never one used within the last hour unless --force is given: a revision that writes no holder file is in use before its first lease only through what it writes. No collection removes a layout with a live holder file, one without holder files whose clock probe names a live process (the last to open it), one in which a live process leases or marks a key, or one whose holders or leases cannot be read. What a collection set aside and never removed goes once its collector has ended. A collection renames the layout aside before it reads its holders and its last use again, and renames it back when either says it is in use, so a run that opened it in between keeps it; a process reading through it meanwhile meets the entry missing for that moment. ci cache stats lists every layout with its size and last use.

BGS11: Snapshots before sharing. A result must be published to a store or cache shared beyond its machine only if its action read a snapshot of its inputs: the inputs ingested into content-addressed storage, hashed in the same pass that copies them, and the action run on a view materialized from that storage. Hard links into the tree are not a snapshot, because an editor that writes in place changes the linked file. The Nix backend satisfies this requirement (BGN2). The local backend's store is per machine (BGS1), and any remote cache it gains must meet this requirement first.

Rationale: a snapshot is the only design without a timestamp window, and a wrong result in a shared cache reaches every machine that uses it.

BGS12: The whole-run record. A run that reused every keyed action from the store may keep a record of it, <store>/fast/<run key>: each keyed action's declared key, result key and outputs digest, and every fact its keys and lookups rested on. A later run with the same run key may take each keyed action's result from the record, building no key and looking nothing up, only when every fact still holds. Otherwise it must look every action up as a run without a record does.

  • The run key names the run's actions: the plan's request, the kept plan's digest (BGP2), and the selection made from it (--build-only, the tasks a selection kept). Only a run that reused its plan keeps or reads a record, and a determinism check (BGS9), an explanation (BGS8), a replacement (BGS7), a confined run, a run with the cache inside the worktree and one with SPARKLES_CI_FAST_PATH=0 keep and read none. The record also names the toolchain identity with the in-process generators' code (BGT1), the worktree, the cache and the digest of the store's secret (BGS8), and is another run's record when any differs.
  • Facts: each worktree file a key or a manifest read, by its identity (device, inode, size, modification and status-change times) when both its times predate the store's clock by BGK5's two seconds, and otherwise by the digest of its content, read again; each absolute file by its content; each file that could not be read; each path whose existence a key or lookup asked, one in the Nix store, answered through no link, by whether its store path exists, since a store path's contents never change; each directory a listing answered from (BGK2) or a tree walk read (BGK6), by its identity when its times predate the clock by that margin on the clock's file system, and otherwise by its entries' names and kinds as the listing gave them or the walk statted them, listed again, so an entry that became a directory changes it; and each store path a lookup read or found missing: the result's entry, record, out/ and outputs, the manifest list, the probe block, by identity, and the three a lookup parses (the record, the list, the block) by their bytes too, read again, so a flipped bit the stat cannot see still falls back. A path checked by identity is read through a root that refuses every symbolic link, so a link anywhere on it falls back; a worktree file read through its links is resolved again and must lead where it did. A path in the run directory a key read must be an output the record of a reused producer holds, and one a lookup asked for must lie in such a producer's output directory: that producer's entry settles it.
  • What keeps no record: a run that did not end cleanly (a task failed or was cancelled); one that reused no keyed action; one in which a keyed action it reached missed, replayed a failure, was served from a digest record or had no key (one it never reached, a fallback it did not need, is no part of the record); a lookup that met a mark, a lease it could not make or a corrupt entry; a path seen two ways in one run (one a run asks again each time); a link's key that could not read an archive; a key or lookup that read the run directory but as above, or a path the run writes; a file dated in the future (BGK5); a tree walk that could not list or stat an entry; and a path with a tab or a line break.
  • One decision per run: a run the record serves decides every lookup when it starts, as a run keeps its first observation of each file (BGK5): what a concurrent process publishes, evicts or rewrites meanwhile is the next run's to see, and every result it serves stays leased (BGS4).
  • Leases and corruption: a run the record serves leases every recorded result in one append, then checks that no other process marks it, before it checks any fact (BGS4). Once every fact holds it draws BGS6's sample of hits as a lookup would, and a few threads rehash the hits drawn while the run goes on, reading its record as a lookup would (its outputs digest the one recorded) and streaming each output as a regular file; a sampled action waits for its rehash when it is reached, rehashing it itself when no thread has begun it, and keeps its verdict should it be looked up.
  • Links: once every fact holds, each served action's output directory is linked to its result; a sampled one's only once its rehash matched. A link made for an action that is not served by the run's end goes.
  • Falling back mid-run: a sampled result that does not match its record, a link that cannot be made when its action is reached, and a keyed action the record does not name reached after all: that action and every later one are looked up as without a record, a link made for one first removed, and the record is dropped, so the next run looks up and records anew. The lookup of a sampled result that did not match rehashes it, so BGS6 moves it to trash/, and the action runs: no digest record serves it. The actions already served rested on facts that held; a looked-up action's consumers key on the recorded outputs digests, and its links on the library names its served producers' records hold (BGK9).
  • Bound: the store keeps the 64 most recently written records.

Rationale: a run with nothing changed spends almost all its time building keys and looking up results it will reuse. The record's facts are the union of what those keys and lookups read, so checking them is the same decision made from one pass over the file system, and any doubt costs only the full lookups.

The store also reports each lookup's outcome and miss reason, and keeps a bounded journal of evicted keys to tell an eviction from a first build (observability §4.1).

7. Generators ​

A generator is a tool the graph runs to produce files that a compile reads. There are three kinds:

  • The shader compiler, run as preGenerateCommands declare.
  • A single-file D program that a preGenerateCommands entry runs through dub: the dmd registry package's config.d is one.
  • The test-root module, generated in-process (BGV2).

BGG1: Typed generators. Each generator must be a typed generate action with declared inputs and declared outputs. Its tool is built by the graph, except an in-process generator, which has none. A preGenerateCommands entry that the resolver does not recognize as one of these kinds is a resolution error, never a shell command. An entry is read in the directory of the package that declares it, as dub runs it there, also when a source library folds into a dependent.

BGG2: Generated inputs replace in-tree paths. A compile that reads generated files must read them from the generator's output directory and must depend on the generator action. Where the recipe names an in-tree output directory (such as stringImportPaths "generated/shaders"), the generator's output directory replaces that entry in the same position. The replacement is a recorded divergence from dub (decisions.md).

BGG3: Generators take explicit inputs. A generator invoked by the graph must not decide staleness itself; the cache decides. It receives its sources, import paths, output directory and tool paths as arguments, and it does not call dub or search PATH. The shader compiler gains a direct invocation mode for this, and its own specification (shaders build) is amended with it. The device configuration the graph writes for it names paths beneath the root as $ROOT/…, and the tool resolves them against the root it runs in: the file, and the result that holds it, is then the same in every worktree. A single-file program that searches PATH anyway finds there only tools that fail (D60).

BGG4: Generated sources have content-addressed paths. A generated source that a compile names as a source file, such as an extracted documentation example, must live at a path derived from its content digest, so that its path, and with it __FILE__, is the same in every run.

BGG5: Generators that resolve imports. A generator whose tool resolves imports, such as the shader compiler running LDC over D shader modules, must be keyed as a compile is: by a manifest of what its tool read and probed (BGK2–BGK3), or conservatively (BGK6) when the tool reports none.

Rationale: its declared inputs name the shader modules, not the modules they import, so a declared key alone would miss an edit to an imported one.

BGG6: Generator tools are build-platform variants. A generator's tool must be built as an application variant for the build platform (§8.1), whatever the host platform of the variant that reads its outputs.

BGG7: Commands the graph does not run. Planning must refuse a variant whose settings carry postGenerateCommands, preBuildCommands, postBuildCommands, preRunCommands or postRunCommands, or copyFiles, frameworks, environments, buildEnvironments or runEnvironments; a target of type object; a root of type none; a dynamic library a dependent links; and a target built with the syntaxOnly option (dub's syntax). Its diagnostic names the package and the directive. dub runs such a command, copies such files and sets such variables around a build or a run, and builds no binary under syntax or for a none root: a graph that built past any of them would make an artifact, or run a program, that dub does not.

8. Toolchain identity ​

BGT1: Identity. The toolchain identity must cover:

  • the D compiler binary and the configuration file it uses, which the action passes explicitly (-conf= on LDC);
  • the druntime and Phobos sources and libraries;
  • the C compiler driver that the D compiler invokes for preprocessing and linking, the C library, startup objects and system headers;
  • the linker;
  • every generator tool, and for an in-process generator (§7), the code that runs it: the identity the caller gives (RunLayout.handlers), else that of the binary that holds it, its store path when it lies in the Nix store, else its path, size and modification time (and on POSIX its device, inode and status-change time), so a rebuilt binary runs its generators again while their consumers, keyed on the outputs' digest, still hit when the bytes are the same; an in-process action whose handler is a client's has no declared key unless the client keys it (RunLayout.keyedHandlers), answering for its key covering what it reads, as ci's Markdown and standalone examples do under their reuse rule (ci CIC31); it is then keyed as a built-in generator's is;
  • for a test action, the runner core that runs it: the identity of the ci build whose core drives the test binary. The test runner runs out of process, and this item follows TRM8 of its process model (docs/specs/test-runner/process-model.md).

BGT2: Store toolchains. When a component resolves into a Nix store path, its identity may be that path's closure of store paths, without reading file contents. A wrapper script's referenced store paths are part of its closure.

BGT3: Host toolchains. Otherwise, a component's identity must be the digests of the files BGT1 lists, found through the compiler's own reported configuration. It may be cached per BGK5.

BGT4: The pinned test runner (superseded). Superseded by the test runner's out-of-process model, TRM8 and decision TRD9 in docs/specs/test-runner/: test binaries link only the shim, never the runner's implementation, and BGT1's runner-core item replaces this requirement. Its former text, not normative:

Every test root must link the pinned test runner's prebuilt library and compile against its import directory. The test-runner shim, sparkles:test-runner, stays an in-tree sourceLibrary that every package depends on through an ordinary dependency. A change to the working tree's test-runner implementation affects only that package's own tests until the pin is bumped.

BGT5: Store toolchains are identified once. A toolchain may have its identity, with the configuration file, source tree and dependency output its compiler reports, kept and reused by later runs when every tool, the configuration file as reported, every path that configuration names (quoted, or on DMD's unquoted DFLAGS= lines, a switch's operand such as -P-I/… included) and every component resolves into the Nix store, and every probe that found them succeeded. The kept identity must be keyed by everything those probes read that the store does not fix: the ci binary that ran them, each tool as named and the store path it resolves to, the working directory, $HOME, $DFLAGS, $XDG_CONFIG_HOME and $XDG_CONFIG_DIRS, and the identity or absence of each configuration file the compiler searches outside the store, the XDG configuration directories' included. Any other toolchain must be identified anew by each run, and a dry run keeps no identity. A path the configuration names only in a comment configures nothing: it is neither a path the configuration names nor one of the toolchain's directories.

8.1 Platforms ​

The graph names platforms as Nix does (nixpkgs, cross-compilation): an action runs on the build platform, the machine that plans and executes the run, and a variant's output runs on its host platform. Native compilation is the case where the two are equal, not a mode of its own. A compiler's target platform is not modelled: LDC emits for any platform its -mtriple names, and its configuration chooses the runtime per triple.

BGH1: Platform names. A platform must be identified by its nixpkgs config, the LLVM target triple (x86_64-unknown-linux-gnu, wasm32-unknown-wasip1, aarch64-unknown-linux-android). Its Nix system double, the triple the compiler takes, and the dub platform and architecture identifiers a recipe's settings match must be derived from it, not declared beside it.

BGH2: The host is the request's. A request must name its host platform, the build platform when it names none, as pkgsCross.<platform>.<package> names one in Nix. Every request must be planned through the same steps whatever its host.

BGH3: Hosts along edges. A library dependency's variant must have the host platform of the variant that depends on it. A generator's tool must have the build platform as its host (BGG6), as a Nix build-time dependency's host is its dependent's build platform. The variant key (BGV1) must cover the host platform.

BGH4: The toolchain table. The toolchain of each host platform must come from one table the environment provides, keyed by config, which the repository's Nix shells generate from the same pkgsCross and toolchain packages its Nix builds use. An entry must give the D compiler, whose configuration the run asks the compiler for, and its archiver; another host's entry must give its triple, the one target its compiles, links and platform probe all name. It gives the C compiler driver where the host links through one, the pkg-config search directory for that host's libraries when it has one, and the emulator that runs the host's programs on the build platform when one can: none for Android, whose programs need its own loader. A request whose host has no entry must be refused, naming the host.

BGH5: Lowering for a host. Every compile, archive and link of a variant must use its host's entry: the compile names the host's triple, the archive is made by the host's compiler, and the link goes through the host's driver. An action of a variant must not use another platform's linker, library search path or pkg-config directory.

BGH6: Libraries for a host. A recipe's system libraries (libs) must resolve through the host entry's pkg-config directory alone, so no library of the build platform reaches a variant of another host. A variant that needs what its host's entry does not provide must be refused with the missing item named, so the caller can build it another way.

BGH7: Keys per toolchain. Every key must cover the identity (BGT1, BGT2) of its action's own toolchain, the host entry's, so two actions of one run with different hosts never share a key.

BGH8: Tests on another host. A test whose binary the build platform cannot execute, and that no emulator of its host's entry runs, must be skipped and reported as skipped, never as passed.

9. Test actions and result reuse ​

BGX1: Test actions. A test action runs a linked test binary with a declared argument vector, environment and working directory.

  • Working directory: the requesting worktree's repository root, whatever the recipe's workingDirectory says. That directive governs only the run actions of applications and examples.
  • Outcome: the binary's exit status and output.
  • No dub: a test binary must not run dub, and the test runner must not fall back to it. The import paths, string import paths, dflags and versions that its compile-time and extracted modes need are passed to it as arguments, from the variant's resolved settings, together with a working directory for its probe files inside the task's directory. The one exception is a test whose subject is dub: its package declares dub in test-programs.json, with a reason, and that test alone finds dub on its PATH (D62).

BGX2: Runtime inputs. A test action's declared runtime inputs must include:

  • the source closure the binary reads at run time, such as fixtures named through __FILE_FULL_PATH__;
  • the toolchain, when the test compiles;
  • the programs it runs that the graph builds, which its package declares in test-programs.json: each is linked by its own actions, and the test receives its path in the environment variable the file names (D61);
  • dub, when its package declares it (D62);
  • the flake inputs it reads.

BGX3: Result reuse. A test action's result must not be reused unless one of these holds, and otherwise every request runs it:

  • Sandboxed: it ran in the Nix sandbox (§10). Its output records the machine it observed at run time: the kernel release, the CPU model and features, and the system type. A reuse must compare them with the machine the test would otherwise run on, and a mismatch is a miss: the test runs again, without reuse. That machine is the requesting one for its own system type, and for another system type any remote builder configured for it.
  • Declared: the test declares every runtime input it reads, and the key covers them.

A test that needs the network is never reused.

Rationale: tests in this repository read the network, the kernel, /proc and flake inputs, and the Nix sandbox isolates none of the kernel's features. A reused result for an undeclared input reports a pass that was never observed. The machine facts are compared on reuse, not keyed, because Nix chooses the builder after the derivation's hash is fixed (D33).

BGX4: Shared verdicts. A test result that BGX3 allows to be reused, and that BGS11 allows to leave its machine, may be published to a binary cache as a shared verdict: a flat content-addressed path whose content is its key, looked up by asking whether the path exists.

  • The key: the test's result key, plus the kernel release, the CPU model and features, and the identity of the runner image, all as observed on the machine that ran it. A requester computes the key with its own machine's facts, so a verdict matches only a machine like the one it was observed on.
  • Publishing: only a run holding the cache's push credential publishes, and only once every gating check of that run passed.
  • A missing verdict costs a run of the test, never a failure.

A task skipped by a shared verdict carries the run-record id of the run that published it, as observability OBD9 requires.

Rationale: BGX3 compares machine facts on reuse, but an existence query cannot compare anything, so the facts enter the key. The gate keeps a run whose other checks failed from vouching for its tests (D43).

10. The Nix backend ​

The Nix backend lowers the same action graph to Nix derivations, so that /etc/nix/machines remote builders, the Nix store and binary caches execute and share it. It uses neither dynamic derivations, recursive Nix nor import-from-derivation: every derivation is known before any builds.

BGN1: Same graph. The Nix backend must lower the actions §4 defines, with the same declared keys, argument vectors and declared outputs as the local backend, plus the separate measurement output (observability OBD8).

  • Staging helper: each derivation's builder is a small, deterministic staging helper. It copies or hard-links each store input, never symlinks it, to its repository-relative path under a private root directory. It hard-links an input only when the input and the private root are on one mount, and copies it otherwise.
  • The input list: the helper reads the list of inputs to stage from a file (passAsFile), never from an argument or an environment variable, which MAX_ARG_STRLEN bounds.
  • Other systems: a derivation for another system type uses that system's toolchain, whose identity enters its keys (§8).
  • The argument vector: the helper then runs the action's normalized argument vector there, with the private root in the root placeholder's place (BGA6), and that measurement output's directory in the measurement placeholder's place. So the recorded paths are root-relative, exactly as in the local backend.

BGN2: Sources. A compile derivation's source inputs must be its declared inputs plus the complete file sets of the import, C import and string import roots it searches: the tree of its declared read set's input state (BGW7 and BGW9 of the fileset-native workspace). Each enters the store as a content-addressed path that the build graph computes itself, hashing each file in the same pass that copies it, so the derivation reads a snapshot (BGS11). Whole-repository source copies are not used.

An edit to any file under a searched root therefore re-runs the compiles that search it, unlike the local backend's manifest check (BGK4).

A backend may narrow the inputs to a manifest that the local store holds for the same declared key, under these constraints (D32):

  • Still absent: it narrows only while every absent candidate of that manifest is still absent.
  • Checked afterwards: the narrowed build's own dependency output must list only files among its inputs, and only absent candidates that are absent from the full roots too. Otherwise, or if the build fails, it is rerun with the full roots.
  • Never shared: a narrowed derivation's outputs must not be pushed to a shared store or binary cache.

Rationale: D searches import roots in order, so a file created earlier on the search path shadows the one the manifest resolved instead of failing. A narrowed build that omits it compiles the old module and succeeds. A narrowed derivation's identity also depends on the local store's history, which no other machine shares.

BGN3: Derivations without evaluation. Derivations must be created directly from their description (nix derivation add). Nix expression evaluation is not on the build path.

  • Lowering: derivations are added in topological order and in batches, and one already in the store is not added again.
  • Dry run: a dry run computes each derivation's store path in process, without the Nix daemon, so it writes nothing (TDY2).

BGN4: Content addressing. compile, archive and link derivations should be content-addressed (ca-derivations), so that a compile whose output is unchanged stops the rebuild of the archives and links that consume it. Where that feature is unavailable, the backend must fall back to input-addressed derivations and report it. That is the exception policy for this recommendation. test and generate derivations are input-addressed.

Rationale: a content-addressed derivation is a claim that equal inputs give equal bytes. A test's output records an observation, not a function of its inputs, and a generator tool's determinism is not checked by the graph.

BGN5: Agreement. For the same declared key and manifest, the local backend and the Nix backend must produce interchangeable outputs. The measurement output (observability OBD8) is not compared. The agreement oracle in testing.md checks it.

BGN6: Diagnostics in the output. A derivation's output must hold the action's standard output, standard error and exit status. A derivation whose action failed in a way BGA9 publishes therefore succeeds, with the failure recorded, and the backend replays it as BGA7 requires. A test derivation that records a failure is never reused (BGA9).

  • Reruns: a test the backend must not reuse, because its output records a failure or a machine that does not match (BGX3), is rerun by lowering it again with one more environment attribute, naming the output it replaces. The new derivation's path therefore differs, and Nix runs it instead of substituting the old output.

BGN7: Garbage-collector roots. A run must hold an indirect garbage-collector root for every store path it looks up or realises, from the lookup until the run ends. A concurrent nix-collect-garbage therefore cannot delete a result between its lookup and its use, as BGS4's leases ensure locally.

BGN8: Infrastructure failures. A failure of the Nix machinery must not be recorded or reported as the action's result:

  • Infrastructure: an unreachable or crashed builder, a failed substitution or copy, a lost connection, or a builder killed for memory. The backend retries it, on another builder or locally, up to a bound, and reports it as an infrastructure failure when the bound is reached.
  • The action's own ending: the staging helper reports the action's timeout or termination by a signal distinctly. That is the action's outcome, which BGA9 does not publish and the backend does not retry.

An outcome recorded in an output by BGN6 is authoritative.

Rationale: a remote builder that dies mid-compile says nothing about the compile. Reporting it as a compile failure would fail a run, and publishing it would replay a failure no cold build gives (invariant 5).

11. Public surface ​

The library exposes these operations, under sparkles.build_primitives:

  • resolve: a recipe, configuration, build type and platform, to resolved settings (§2);
  • plan: a set of requested roots and roles, to a validated action graph with variant dedup and the layout lint (§§3–4);
  • key: an action, to its declared key, and a manifest, to its result key (§5);
  • store: look up, lease, publish, inspect and collect garbage (§6);
  • lower: an action graph, to task nodes (BGA8) or to Nix derivations (§10).

Each operation returns its errors as values. Planning and keying read recipes, lex source files for version pruning and the layout lint, digest files and query pkg-config. They reach files only through the capability VFS, on the roots §12 names, and perform no other I/O.

BGP1: Planning's read set. Planning must record its read set and report it with the plan:

  • every file it read or listed, with its content digest or listing digest;
  • every path it probed and found absent;
  • every symbolic link a lookup crossed, with its target (BGF3);
  • every environment variable it consulted;
  • every pkg-config query, with its arguments.

The read set is what a plan depends on. A consumer that decides when to plan again, such as ci's invalidator list (CIC5), is checked against it. See D37.

BGP2: A plan reused while its read set holds. A driver may keep a plan with its read set and take it, instead of planning, for a later request. It must take it only when the request is the same and every entry of the read set still holds:

  • The request: every value planning reads that the read set does not hold: the arguments, the tools and their toolchain's identity (BGT2), the toolchain table's digest (BGH4), the store's layout and format versions, the driver's own program, and the environment it reads before planning, every variable pkg-config or its wrapper may read among it. The capacity a run admits is no part of it: a test's thread count is bound by the run (BGA8).
  • A file: its identity (device, inode, size, modification and change times) is the one recorded, found through the tree planning reads through under its rules (BGF3); it lies on the file system whose clock was read as planning began, and neither time is within two seconds of that clock, as BGK5 holds a cached digest; otherwise it is read, and its digest compared. The identity recorded must be taken on the handle that read the file, unchanged across the read, from bytes whose digest is the one planning recorded. A file in the Nix store holds while it exists.
  • A listing: held unlisted when every directory it rests on keeps the identity recorded: each directory it listed (device, inode, size, modification and change times), reached from the root that serves it through no link, on the file system whose clock was read as planning began, neither time within two seconds of that clock; and each directory on the way down to it, reached through no link, as the same directory (device and inode). A directory's identity changes when an entry is added to it, removed, renamed or replaced, and the listing rests on every directory it listed, so one whose directories all hold names the same files; a change to a file's contents moves no directory, and no listing depends on it. The identities must be taken as the listing is made again for the plan kept, each on the handle that lists the directory, before it is listed, from a listing whose digest is the one planning recorded. A listing that met a link, beneath it or on the way down to it, an entry whose kind its directory did not tell, or an entry it could not read, keeps no identities; one that keeps none, or whose directories do not all hold, is made again and compared. Identities are kept only when the cache lies on a file system known to move a directory's times with every change to its entries, to within a second, and to answer a stat afresh (ext2 to ext4, XFS, btrfs, ZFS, F2FS, bcachefs, tmpfs); on any other, a network file system that may answer from a cache among them, every listing is made again.
  • A probe or a link: made again through that tree, and compared. A lookup the tree refuses plans again.
  • A variable: read again, and compared; one a host's entry in the toolchain table supplies is held by the table's digest in the request.
  • A pkg-config query: held unasked only when the program and every directory it searches (its path variables, and its compiled-in default for the build platform; the entry's directory for a host) lie in the Nix store, whose contents are fixed by their names (BGK2), and the request holds them; otherwise asked again, and its answer compared.

Any entry that cannot be checked plans again. A reused plan must be the one planning would make: the same actions, edges and rendered plan. The reason a kept plan was not taken is reported. A dry run must not keep a plan, and a kept plan must be published whole, under a name no other writer uses before it is renamed into place. A driver that keeps plans must keep the one a run makes while creating the cache, so the run after it does not plan again.

12. File-system access ​

The build graph reaches the file system only through the capability VFS: a held directory handle and one name beneath it, never a path resolved from the process's root. Its listings are fileset resolutions answered through that VFS. The processes an action starts are outside this section: a compiler, linker, generator or test reads paths, and the manifest and BGK8 check what it read.

BGF1: Every operation through the VFS. Every file-system operation the build graph performs itself must be a VFS operation on a handle (VFO3), on a backend its caller supplies (VFB1): RingVfs for operations the task-DAG executor runs on its event loop, BlockingVfs for planning in a process without one, and MemVfs in tests. This covers:

  • reading recipes and lockfiles (§2), and lexing sources (BGV1, BGV5);
  • digesting files and probing absent candidates (BGK2, BGK5, BGK9), and the stat proxies of BGK7;
  • creating action output and measurement directories (BGA3);
  • every store operation (§6).

No module of the build graph may call std.file, open a std.stdio.File, or make a system call that names a path.

BGF2: The roots. The only directories the build graph opens with openRoot (VFH7) must be these, each once per run:

RootRightsPolicy
the worktreeread-onlysymlinks = beneath
the store, <store> (BGS1)all; entries created owner-onlythe default
the worktree's private git directory (BGK5)create, write, removethe default
each pinned registry source directory (BGR4)read-onlythe default
each directory a pkg-config query or a link searches (BGK9)read-onlythe default
each host toolchain directory (BGT3)read-onlysymlinks = beneath

Every other access descends from one of them, and the read set (BGP1) names each entry by its root and the path beneath it.

Rationale: a read-only worktree makes invariant 6 a property of the types: no planning or keying code holds a handle that can write the source tree. A fixed set of roots makes everything the graph can reach enumerable, and every escape from it findable by searching for ambientAuthority(.

BGF3: Links in the worktree. A committed relative link whose target stays in the worktree must resolve (VFP5); ui-app's link to its resilience catalog is one (D24). A link that is absolute or climbs above the worktree must fail planning with the VFS's escapesRoot, naming the link. The read set records every link a lookup crossed, with its target, so retargeting a link changes the plan.

BGF4: Store operations. The store must mutate itself with the VFS's primitives on handles beneath <store>:

  • Publication moves a staged entry into results/ with renameAt, which cannot leave the store's root (VFO8).
  • Manifest lists, preimages and digest records are written with writeFileAtomic (VFO9).
  • Eviction and discard remove trees with removeTree, which never descends a link (VFD2).

The store's root refuses links (VFP4). A link planted in the store fails the lookup that meets it with symlinkRefused, and BGS6 treats its entry as corrupt.

BGF5: Listings are filesets. Every directory listing the build graph makes, such as a source root's modules, the string import roots that pruning scans (BGV1), and a generator's declared input directories (BGG3), must be a fileset resolved by the fileset driver over the root that holds it (FSD1). The graph walks no directory itself, and the listing digests BGP1 records come from the resolution.

BGF6: Identity for digest caching. BGK5's cache tuple and BGK7's stat proxies must come from the VFS's Stat, with the file identity and the change time requested in its mask (VFO6). The tuple is the subtree cache's fingerprint (FSC2). A tuple decides only whether a file is read again; it never stands in for a handle.

See D44.