3.2.6 Hermeticity Settings
The .bazelrc hierarchy from 3.2.1 .bazelrc Hierarchy determines where flags live and who can override them. This item is about which flags to put there. A handful of settings close the most common paths through which the host environment leaks into build actions, breaking the cache sharing and reproducibility guarantees that 2.3 Hermeticity & Sandboxing described conceptually.
Strict action environment
The single highest-impact hermeticity flag is --incompatible_strict_action_env. Without it, Bazel forwards environment variables like PATH from the invoking shell into every action's command environment. Two developers with different PATH values produce actions with different cache keys even when building identical source, which causes unexplained cache misses across machines and between local builds and CI.1,2
With the flag enabled, Bazel stops forwarding the client shell and instead provides a minimal, fixed baseline environment to actions. Beyond that baseline, only variables explicitly added via --action_env reach the action command line.3
build --incompatible_strict_action_env
This flag followed the lifecycle pattern described in 3.2.5 Flag Lifecycle: it started as --experimental_strict_action_env, was renamed to --incompatible_strict_action_env, and later became a default behavior.1 Bazel 8.x and earlier default it to false, so the flag must be set explicitly there. Bazel 9.0 flipped the default to true, which leaves the flag as an opt-out on 9.x and 10.4 Check bazel help build --long or the command-line reference for the default in the Bazel version you run.
The fixed baseline is small enough to read in full, and it depends on the Bazel version. On Linux and macOS, Bazel 9.x sets PATH=/bin:/usr/bin:/usr/local/bin. Bazel 10 sets PATH=/bin:/usr/bin:/sbin:/usr/sbin. It drops /usr/local/bin because user-installed tools usually live there (FreeBSD and OpenBSD keep it because their system packages install there). Bazel 10 also adds LC_CTYPE=C.UTF-8 for actions that use the default shell environment, such as genrules. Bazel 9.x sets no locale variable for them.5,4 A genrule that prints its environment on Linux shows the difference:
# Bazel 9.3.1
PATH=/bin:/usr/bin:/usr/local/bin
# Bazel 10.0.0rc2
LC_CTYPE=C.UTF-8
PATH=/bin:/usr/bin:/sbin:/usr/sbin
The new baseline can expose hidden dependencies. An action that ran a tool from /usr/local/bin by accident fails on Bazel 10 with a command-not-found error. The real fix is the one from the hermetic-toolchains section below: make the tool a declared input or a toolchain. As a stopgap, pin the 9.x value with --action_env=PATH=/bin:/usr/bin:/usr/local/bin and the same value for --host_action_env. A tool whose output depends on the locale can be returned to the 9.x behavior with --action_env==LC_CTYPE. The leading = before the name unsets the variable, a form that already works on 9.x.5,6 Because these variables are part of the action, the new baseline also changes action keys. An action that Bazel 9.3.1 stored in a shared disk cache runs again under 10.0.0rc2, and becomes a cache hit once both overrides restore the 9.x environment.4
Sandbox network access
By default, sandboxed actions are allowed to reach the network. That means an action can silently fetch data from a remote service without declaring a dependency on it, and the build will succeed until the service is unavailable or the network is blocked.1,7
build --sandbox_default_allow_network=false
Setting --sandbox_default_allow_network=false blocks network access for all sandboxed actions. Actions that legitimately need the network can opt out with tags = ["requires-network"] on the target.1 This surfaces undeclared network dependencies early instead of letting them hide until a CI runner or air-gapped environment reveals them.
The network restriction applies only to sandboxed execution. Actions running with the local strategy or on platforms without sandbox support (notably Windows) are unaffected.3
Network blocking also catches downloads hidden inside actions, which a downloader configuration cannot. --downloader_config block and rewrite rules (3.1.6 BCR Infrastructure) govern only Bazel's own repository downloads: a genrule that runs wget bypasses them and succeeds, and it fails only once the sandbox blocks the network. Network blocking has a blind spot of its own. Sandbox flags are not part of the action key, so a result cached before you enabled the flag is still reused without running the action, and a green build with warm caches does not prove that nothing needs the network. To audit, build once where no cache can answer: after bazel clean, with no disk or remote cache.8
Controlling individual environment variables
Sometimes an action genuinely needs a host variable, for example a cross-compiler path or a cloud credential token. --action_env provides controlled forwarding with two forms:9,3
build --action_env=CC=/usr/bin/clang-18
build --action_env=CLOUD_TOKEN
Compare: These two .bazelrc lines differ by one missing =value:
build --action_env=CC=/usr/bin/clang-18
build --action_env=CC
One keeps a remote cache shareable across machines. The other quietly breaks it. Which is which?
Reveal
--action_env=CC=/usr/bin/clang-18 pins a value. Every machine injects the same CC, so identical sources can still compute matching action keys. --action_env=CC inherits CC from each developer's shell. A gcc laptop and a clang CI worker now describe different actions for the same source.
The trap is that both lines look like "allow CC into the action." They are opposite cache contracts: one standardizes the environment, the other makes the caller's environment part of the key. The print_env example below is the cheap way to make that invisible split visible.
That is the pin-versus-inherit split. The first form is safe for cache sharing because every machine sees the same value. The second form reintroduces the same per-machine divergence that --incompatible_strict_action_env was designed to prevent. Use the inheriting form only when the variable genuinely varies per machine and you accept the cache impact.3
The hermeticity-settings print_env genrule makes that difference observable: its cmd writes ${DEMO_VALUE:-unset}, so the same target produces unset without --action_env=DEMO_VALUE=fixed and fixed with it.
When an action requires a variable but the build never declares it, strict action environments turn the hidden host assumption into a normal build failure:
genrule(
name = "needs_env",
outs = ["token.txt"],
cmd = """
if [ "$${DEMO_TOKEN:-}" != "expected" ]; then
echo "missing DEMO_TOKEN in action environment" >&2
echo "fix: bazel build --action_env=DEMO_TOKEN=expected //env:needs_env" >&2
exit 1
fi
printf '%s\n' "$${DEMO_TOKEN}" > "$@"
""",
)
missing DEMO_TOKEN in action environment
fix: bazel build --action_env=DEMO_TOKEN=expected //env:needs_env
Read this as an execution-phase modeling bug: the action can build only if a host variable is present, but the target or invocation did not make that dependency explicit. The controlled fix is to pass a pinned value:
bazel build --action_env=DEMO_TOKEN=expected //env:needs_env
--host_action_env is the equivalent for actions with execution configurations, such as actions building tools and code generators for the selected execution platform. Despite the flag's historical name, that platform need not be the machine running Bazel, including in cross-compilation or remote-execution builds.6
Pin the action shell
The same genrule can get a different action key on two machines, because the shell that runs it is part of the action's command line and by default Bazel picks that shell from per-machine state. Genrule commands and string commands passed to ctx.actions.run_shell use --shell_executable when the flag is set. Otherwise it uses the BAZEL_SH environment variable from the invocation that started the Bazel server, and without that a hard-coded default for the operating system: /bin/bash on most systems, /usr/local/bin/bash on FreeBSD, and c:/msys64/usr/bin/bash.exe on Windows.10,11
A developer whose server started with a different BAZEL_SH gets a different genrule command line, and therefore a different action key, from everyone else. A Bazel binary built by a distribution can also change the default itself. T6.3.1 Why a Local Build Can Fail Elsewhere shows how the Nixpkgs package points it into /nix/store, a path that remote workers do not have. A checked-in flag removes both sources of drift and names the shell that every machine and worker image must provide:
build --shell_executable=/bin/bash
Pinning the usual default is cache-neutral. On Bazel 9.1.0 a genrule has the same action key with no flag and with --shell_executable=/bin/bash, and a different key with /bin/sh.11 Choose /bin/bash unless every shell command in the build is plain POSIX sh. The command-line reference warns that a shell not compatible with Bash can cause build failures.10 A NixCon demo repository chose --shell_executable=/bin/sh for portability, with the comment that the embedded default is "not great for cross-compatibility".12
The flag covers only shell commands, including a command-string --run_under prefix. Bazel's test wrapper on Linux and macOS starts through its own #!/usr/bin/env bash line, so a test worker needs Bash whatever this flag says.11 In the test-wrapper snippet, one aquery shows both kinds of action:
$ bazel aquery 'mnemonic("TestRunner|Genrule", //:all)' --shell_executable=/bin/sh
action 'Testing //:env_test'
Command Line: (exec external/bazel_tools/tools/test/test-setup.sh \
./env_test)
action 'Executing genrule //:shell_probe'
Command Line: (exec /bin/sh \
-c \
'source external/bazel_tools/tools/genrule/genrule-setup.sh; echo probe > bazel-out/k8-fastbuild/bin/shell_probe.txt')
Only the genrule switched to /bin/sh. 1.2.11 What Runs Before Your Test: the Test Wrapper explains what the test wrapper does and how a missing Bash shows up in a test failure.
Hermetic toolchains
Flags alone cannot make a build hermetic if the compiler itself comes from /usr/bin/gcc. Different machines ship different compiler versions, link against different system libraries, and embed different paths into binaries, all invisible to Bazel's action cache key.3,7
The fix is hermetic toolchains: download a specific compiler version as part of the build rather than relying on whatever the host has installed. Most modern rulesets provide this by default. For example, rules_java auto-downloads a JDK via remote_java_repository, and rules_go downloads a pinned Go SDK. C/C++ is the notable exception: rules_cc, which took over the C/C++ rules when Bazel 9.0 removed them from the core binary, still defaults to an auto-detected host compiler (local_config_cc), making it a common hermeticity gap.3,13
Toolchain authoring and registration are covered in 4.6 Toolchains & Platform Resolution. From a maintainer's perspective, the action item is simpler: check that every language ruleset in your MODULE.bazel uses a downloaded toolchain, not the system one.
A typical hermeticity block
Combining the flags into a shared workspace .bazelrc gives every developer and CI job the same hermeticity baseline:
build --incompatible_strict_action_env
build --sandbox_default_allow_network=false
On Bazel 9.0 and later the first line is redundant but harmless. On 8.x and earlier it is essential. The hermeticity-settings .bazelrc groups the env-pinning and sandbox-network lines under a build:strict named config so the same flags can be exercised on demand. The maintainer-workspace .bazelrc keeps a similar set on the unconditional build block — --spawn_strategy=sandboxed, pinned --java_runtime_version=remotejdk_11, and --sandbox_default_allow_network=false — so every command inherits the hermeticity baseline rather than relying on a named config. If actions still behave unexpectedly after setting these flags, execution-log comparison from 5.7 Cache & Execution is the next diagnostic step.
Diagnosing leaks after the flags are set
Even with these flags in place, non-determinism can still leak through timestamps, sort ordering, or tools that read system state. The execution log is the definitive tool for finding these leaks: run two clean builds, capture --execution_log_compact_file from each, and diff the results. Differences in action output hashes reveal exactly which action is non-deterministic and which inputs diverged.7,14
That workflow is covered in depth in 5.7 Cache & Execution. At the maintainer level, the important takeaway is that --incompatible_strict_action_env and --sandbox_default_allow_network=false eliminate the most frequent leak categories, but they are not a complete guarantee. Periodic execution-log comparison in CI is the way to catch what the flags miss.
Three settings close the most common hermeticity gaps: strict action env stops the host shell environment from leaking into actions, --sandbox_default_allow_network=false blocks undeclared network access, and hermetic toolchains remove the dependency on whatever compilers the host has installed. Put the non-default pieces in the shared workspace .bazelrc so every build inherits the same baseline. Add --shell_executable there too when machines or workers might disagree about the shell, and remember that it does not cover the Bash test wrapper.
Check your understanding · 3 questions
1.Two developers have different PATH values on their machines and build with strict action env off (--noincompatible_strict_action_env, the default on Bazel 8.x and earlier). How does this affect Bazel's action cache?
Select one answer
2.Which settings close the most common hermeticity gaps for a shared Bazel workspace? Select all that apply.
Select all that apply
3.True or false about hermeticity settings:
Choose True or False for each sentence
Footnotes
-
.bazelrc flags you should enable — recommends
--incompatible_strict_action_envand--sandbox_default_allow_network=falseas essential hermeticity flags ↩1 ↩2 ↩3 ↩4 -
Presets for bazelrc — curated preset files that bundle correctness flags including strict action env ↩
-
How to keep a Bazel project hermetic? — comprehensive guide covering
--incompatible_strict_action_env,--action_env, execution strategies, and toolchain hermeticity ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 -
Bazel — core implementation, documentation, and regression corpus — default action
PATHin the source of 9.0.0 and 9.3.1 (/bin:/usr/bin:/usr/local/bin) and of 10.0.0rc2 (/bin:/usr/bin:/sbin:/usr/sbinoutside FreeBSD and OpenBSD). The strict action env default isfalsein 8.5.0 andtruein 9.0.0. The environment listing and the disk-cache result were produced by running the same genrule on 9.3.1 and 10.0.0rc2. ↩1 ↩2 ↩3 -
Bazel 10.0.0rc1 release notes — Execution:
/usr/local/binremoved from the default strict actionPATH(override with--action_envand--host_action_env).LC_CTYPEdefaults toC.UTF-8for actions withuse_default_shell_env = True, and--action_env==LC_CTYPEunsets it. ↩1 ↩2 -
Command-Line Reference —
--host_action_envsets environment variables for actions with execution configurations.--action_envaccepts=nameto unset a variable. ↩1 ↩2 -
Bazel and action (non-) determinism — practical tips including
--nosandbox_default_allow_network,--strict_action_env,--action_env, and hermetic toolchains ↩1 ↩2 ↩3 -
Accessing external resources reliably with Bazel — a downloader
blockrule stops a repository fetch but not a genrule'swget, which fails only under--nosandbox_default_allow_network. Cached results are still reused because sandbox flags are not part of the cache key ↩ -
Commands and Options —
--action_envsyntax and semantics ↩ -
Command line reference —
--shell_executable: theBAZEL_SHfallback from the server-starting invocation, the per-OS default paths, and the warning that a shell not compatible with Bash can break builds ↩1 ↩2 -
Bazel — core implementation, documentation, and regression corpus — at tag
9.1.0,ShToolchain.javaprefers--shell_executableover the platform default, andStarlarkActionFactory.javauses the same path for stringrun_shellcommands. On the test path,TestStrategy.javauses it only to run a command-string--run_underprefix, andtest-setup.shstarts with#!/usr/bin/env bash. The action keys come frombazel aquery 'mnemonic("Genrule", //:shell_probe)'in the test-wrapper snippet, run without the flag, with/bin/bash, and with/bin/sh. The two command lines come from the snippet'sTestRunner|Genrulequery on Bazel 9.1.0. ↩1 ↩2 ↩3 -
Nix and Bazel - match made in heaven or hell? - Aleksander Gondek, Artur Stachecki (NixCon 2024) — the transcript's notes on the demo repository's stage-5
.bazelrc, which sets--shell_executable=/bin/shwith that comment ↩ -
Bazel 9 LTS — Bazel 9.0 completed Starlarkification by moving the built-in C++ rules into
rules_cc. A Bazel 9.1.0 probe withrules_cc0.2.14 selected@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-k8by default. ↩ -
Hermeticity — canonical definition and troubleshooting strategies for hermetic builds ↩