4.7.1 Build Settings for Rule Authors

A build setting is a single piece of Bazel's configuration — one entry in the {key: value} map that Bazel calls a configuration. The built-in entries (--compilation_mode, --copt, --cpu, …) ship inside the Bazel binary. Starlark build settings are defined in .bzl files instead, so rule authors can add new typed configuration knobs without waiting for a Bazel release.1 3.2.4 Command Line Flags already introduced them from the user's side — typed replacements for --define, set on the command line with --//pkg:flag=value. This article is the rule-author's side: how to declare the setting, validate it, expose providers, and connect it to select() from 3.3.1 Configurable Attributes (select()) and to transitions in 4.7.2 Starlark Transitions.

Anatomy of a Build-Setting Rule

A build setting is a regular Starlark rule with one extra parameter on rule(): build_setting. The argument is a descriptor from the top-level config module that fixes the setting's type and decides whether users may set it from the command line.1,2

# example/buildsettings/build_settings.bzl
OptimizationProvider = provider(fields = ["level"])

_ALLOWED = ["none", "size", "speed"]

def _impl(ctx):
    value = ctx.build_setting_value
    if value not in _ALLOWED:
        fail("{}: expected one of {}, got '{}'".format(
            ctx.label, _ALLOWED, value,
        ))
    return OptimizationProvider(level = value)

optimization_level = rule(
    implementation = _impl,
    build_setting = config.string(flag = True),
)

Three things happen when build_setting is set on a rule:

  • Bazel automatically adds a mandatory build_setting_default attribute whose type matches the descriptor — optimization_level(name = "optimization", build_setting_default = "none") is how a BUILD file picks the baseline value.3
  • Inside the implementation, ctx.build_setting_value exposes the current value (after any command-line override or transition). Reading it from a non–build-setting rule is an error.4
  • If flag = True, the setting is callable as --//pkg:name=value on the command line and writable by transitions. Settings declared with flag = False can only be changed by transitions, which is the right choice for internal knobs that users should not flip directly.1

Anything else about the rule is unchanged. A build-setting rule has an implementation function, returns providers, and shows up in select() like any other target — its only specialty is owning one entry in the configuration map.

The config Type Descriptors

The config module exposes one descriptor per supported Starlark type. Each takes flag = True/False and decides what Bazel will accept on the command line:2

DescriptorStarlark typeNotes
config.bool()boolCLI: --//pkg:flag enables, --no//pkg:flag disables.1
config.int()int—
config.string()stringHistorically supported allow_multiple = True. That parameter is now deprecated — use config.string_list(repeatable = True) instead.2
config.string_list()list[string]Comma-separated on the command line (--//pkg:flag=foo,bar), or with repeatable = True accept multiple --//pkg:flag=... occurrences that each contribute one element.2
config.string_set()set[string]Same surface as string_list, but element order and duplicates are not preserved. Prefer this over string_list when order does not matter — it reduces unnecessary configuration forking.2

repeatable = True suits copt-style flags. rules_mojo, the Bazel rules for the Mojo programming language, declares its //:mojo_copt flag this way for two reasons: values from several --config lines stack instead of overwriting each other, and a compiler option that contains a comma is not split into two values.5

A rule that does not need command-line exposure can still benefit from typing. build_setting = config.int(flag = False) produces an int-typed configuration entry whose value can only be changed by a transition, which is exactly what an internal split-by-shard or "instrumented build" knob usually wants.

Validating Values and Returning Providers

ctx.build_setting_value only enforces the Starlark type (string, int, bool, list, set). Any further constraint — allowed values, ranges, mutually exclusive combinations — belongs in the implementation function, which can fail() with a useful error and/or transform the raw value into a richer provider for consumers.1

Two patterns are common:

  • Enum-style settings validate against a literal allow-list and return a custom provider carrying the parsed value (the OptimizationProvider example above). Other rules then declare attrs = {"optimization": attr.label(providers = [OptimizationProvider])} and read the value as ctx.attr.optimization[OptimizationProvider].level. When every target of the consuming rule should follow the setting, make that attribute private, for example "_optimization": attr.label(default = Label("//example:optimization"), providers = [OptimizationProvider]). Users then change the value only through the flag or a transition, not per target. Farid Zakaria's walkthrough uses exactly this shape for a style setting before adding a transition that restyles one subtree.6
  • Pass-through settings simply return a small wrapper provider so the value can be consumed uniformly through a normal label dependency.1

There is one important asymmetry. A consumer rule that depends on a build setting as a label sees whatever providers the implementation function returned. But other references to the same setting — for example, the settings dict passed to a transition implementation, or the matching done by config_setting — see only the basic Starlark-typed value, not anything the implementation function added.1 Validation that lives in the implementation function therefore protects consumers, not transitions. Transitions are free to write any value the descriptor's type permits.

Skylib Shortcuts for the Common Cases

Most projects do not need a custom implementation function for their flags. Skylib's common_settings.bzl ships pre-defined build-setting rules — string_flag is the common worked example — that return a simple BuildSettingInfo wrapper around the raw value. string_flag and string_setting also support a values allow-list for string enums. For richer validation on bools, ints, lists, or cross-field constraints, write a custom build-setting rule.7,1

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

string_flag(
    name = "environment",
    values = ["dev", "staging", "prod"],
    build_setting_default = "dev",
)

Use Skylib for plain string enums or simple typed knobs. Write your own build-setting rule when you need real validation, derived providers, or a more interesting type than the descriptors directly support.

Before adding a custom setting, check whether a built-in Bazel option or a Skylib common setting already fits the problem.1 A project-specific flag is public configuration surface: once rules, select() branches, and .bazelrc aliases depend on it, changing its type or meaning becomes a migration.

Command-Line Exposure and Aliases

When flag = True, the setting's full label is its flag name on the command line:1

bazel build //my/target --//example:environment=prod
bazel build //my/target --//example:boolean_flag        # bool: set true
bazel build //my/target --no//example:boolean_flag      # bool: set false

Long labels turn into noisy command lines fast. --flag_alias=alias=//pkg:target binds a build setting to a short name. It is meant to live in .bazelrc rather than being typed on every invocation:8,1

# .bazelrc
build --flag_alias=env=//example:environment
bazel build //my/target --env=prod

Changing a flag's value between invocations has a cost that is easy to miss. Bazel treats it like any other changed build option and discards the analysis cache, even when only one small target reads the setting. Farid Zakaria's build logged WARNING: Build option --//:style has changed, discarding analysis cache, and a Bazel 9.1.0 probe printed the same warning on every switch of a one-consumer string setting.6 A flag that developers flip on every other build therefore pays for a fresh analysis each time. Design flags so the everyday value can stay fixed in .bazelrc. When someone really alternates between two values, a separate --output_base per value keeps a warm Bazel server and analysis cache for each.9 3.6.2 Bazel Server Lifecycle in CI covers the same warning from the CI side.

Repeated aliases silently take the last definition, which makes "the build behaves differently than I expected after I changed .bazelrc" a real failure mode. Pick aliases that are unique across a project and prefer keeping them in one bazelrc layer. 3.2.4 Command Line Flags covers the user-side experience in more depth. The rule-author side is just deciding which settings deserve a friendly alias.

Target Scope vs Universal Scope

A build setting that should affect only the target configuration is what you want for product-facing choices such as "build this binary for prod" or "select this device configuration." Its value should not leak into the execution configuration used to build tools. scope = "target" states that intent. The value still travels through ordinary target-configured dependencies, and it resets when an exec transition builds a tool.10

What happens when you leave scope unset depends on the Bazel version. In Bazel 10, an unscoped setting behaves like scope = "target", which is what the Bazel 10 configuration docs describe. The stable 9.3 docs omit that Scope section.11 Bazel 9.x behaves the other way: an unscoped setting keeps its value in exec configurations, like scope = "universal". Bazel 10 made this change by flipping --incompatible_exclude_starlark_flags_from_exec_config to true.12,13 --experimental_propagate_custom_flag=//pkg:setting names one unscoped setting to carry into exec configurations anyway, but a declared scope takes precedence over it.13,14 In the Bazel 10.0.0rc4 release candidate, a scope can also come from the default of the setting rule's scope attribute. Skylib's setting rules, such as bool_flag and string_flag, default that attribute to "target" from Skylib 1.9.1. A Skylib 1.9.1+ flag with no scope therefore counts as target-scoped on 10.0.0rc4, and naming it in --experimental_propagate_custom_flag does not bring its value back. With Skylib 1.9.0, whose default is "universal", the same flag keeps its value in exec configurations on 10.0.0rc4 without being named.15 The setting-scope-propagation snippet records both runs. Declaring scope explicitly on settings whose tool behavior matters gives you the same result on both versions and with every Skylib release.

When a setting intentionally controls both the target being built and the tools that run during the build, set scope = "universal" on the build-setting target. Bazel 9.x already accepts the attribute, so this declaration works on both versions:10,11

bool_flag(
    name = "use_this_for_compiler_and_sources",
    scope = "universal",
)

Use this sparingly. A universal setting couples target and exec configurations, so it should represent a value that genuinely must be identical on both sides of the build, not a shortcut for avoiding an explicit toolchain or execution-platform decision.

Plugging Into select()

A typed build setting becomes a branch key for select() through config_setting's flag_values attribute:1

config_setting(
    name = "is_prod",
    flag_values = {"//example:environment": "prod"},
)

cc_library(
    name = "auth",
    srcs = select({
        ":is_prod": ["auth_prod.cc"],
        "//conditions:default": ["auth_dev.cc"],
    }),
)

The value in flag_values is always a string. Bazel parses it into the setting's declared type when matching.1 The rest of the select() story — //conditions:default, no_match_error, combining selects across dimensions — is covered in 3.3.1 Configurable Attributes (select()). From the rule author's point of view, the only obligation is to give the setting a type and a label that downstream config_setting targets can quote.

Label-Typed Settings: label_flag and label_setting

config.string, config.int, etc., are for value-typed settings. For label-typed configuration — "which target should this rule use for X?" — Bazel ships two built-in rules instead: label_flag (settable on the command line) and label_setting (changeable only by transitions).1 You cannot create new label-typed settings via build_setting = config.label(). The built-ins are the API.

These behave like a configurable indirection. A consumer depends on the label_flag as a regular label. Bazel resolves the dependency to whatever target the flag currently points at and forwards that target's providers. Transitions can rewrite the label dynamically, which is the modern Starlark replacement for many built-in late-bound default attributes.1

The Pigweed embedded-Bazel project uses this pattern to let any board provide its own FreeRTOSConfig.h without patching the upstream FreeRTOS source:16

# Upstream-checked BUILD file
cc_library(
    name = "freertos",
    deps = [":freertos_config"],
)

label_flag(
    name = "freertos_config",
    build_setting_default = ":default_freertos_config",
)

cc_library(
    name = "default_freertos_config",
    target_compatible_with = ["@platforms//:incompatible"],
)

A downstream board sets --@freertos//:freertos_config=//my/board:freertos_cfg (through a transition, or through platform-based flags in 4.7.5 Platform-based Flags) and freertos picks up the board-specific config without source patches. The default is intentionally incompatible so that an unconfigured build fails fast instead of silently producing an unusable artifact.

The same shape answers a common module-design question: a library or ruleset works with either of two implementations of one interface, such as two TLS libraries, and the consumer should choose. Cargo handles optional code through per-crate features, which L6.7 Cargo Features Mapping maps onto Bazel. A label_flag is narrower: the library's targets depend on one flag, and the consumer points it at the implementation it wants, typically from .bazelrc. Two limits of that contract follow from the official definition, and are worth stating in the library's docs:1

  • One choice per configuration. The flag is a single entry in the configuration map, so every target built in that configuration sees the same implementation. A library cannot ask for a specific implementation on its own dependency edge. Two targets in one build get different implementations only when a transition sets the flag differently for one of them (4.7.2 Starlark Transitions).
  • Only providers are forwarded. The flag passes along the providers of whatever target it points at and checks nothing else. A consuming attribute can require providers with attr.label(providers = [...]),17 but matching providers do not make two implementations interchangeable in API or behavior. List what an implementation must provide, and test each one you claim to support.

Use a label_flag when one implementation per build is the right answer. When a single build needs both implementations side by side, model them as separate targets instead.

The Legacy Bridge: fragments and configuration_field

Before Starlark build settings existed, rule authors could only read Bazel's built-in configuration through configuration fragments — typed accessors like ctx.fragments.cpp and ctx.fragments.coverage.18 A rule that reads any fragment must declare it on rule(). Reading an undeclared fragment is an analysis error.18

def _impl(ctx):
    copts = ctx.fragments.cpp.copts
    # ...

my_rule = rule(
    implementation = _impl,
    fragments = ["cpp"],
)

The companion API is configuration_field, which produces a late-bound default for a Label-typed attribute. The value is "late-bound" in the sense that it depends on the current configuration and is resolved at analysis time, not at load time.19 The classic example is the coverage merger tool: an implicit attribute whose default is whatever target --coverage_output_generator points at, and which is only built when coverage is requested:18

my_test = rule(
    test = True,
    implementation = _impl,
    attrs = {
        "_lcov_merger": attr.label(
            default = configuration_field(
                fragment = "coverage",
                name = "output_generator",
            ),
            executable = True,
            cfg = config.exec(exec_group = "test"),
        ),
    },
)

For new code, prefer build settings and toolchains: a label_flag is the Starlark-native replacement for most configuration_field patterns, and toolchains in 4.6.2 Defining, Registering & Accessing Toolchains replace many cases where rules used to reach into a cpp or java fragment. Declare fragments = [...] only when a rule genuinely needs a built-in fragment value that no Starlark API exposes yet.

Foundation for Transitions

The connection to 4.7.2 Starlark Transitions is direct: a Starlark transition declares which build settings it reads (inputs) and which it writes (outputs), both as labels.20 A transition() that writes //example:environment works only because the rule form of that setting exists in the build graph. The same is true for label_flag targets. In other words, the build-setting rule is the in-graph object that gives the transition a stable address — without it, there is nothing for inputs/outputs to point at. Built-in flags piggy-back on the same mechanism through the //command_line_option:* namespace, with --define and --config carved out for reasons covered in 4.7.2 Starlark Transitions.

The platform-based-flags snippet provides a runnable build-setting rule. Its config.bool(flag = True) declaration is the address later read by platform mappings.

key takeaway

A build setting is a typed entry in Bazel's configuration map, represented by a target. Define one with rule(build_setting = config.<type>(flag = True)), read it inside the implementation through ctx.build_setting_value, and validate / wrap it before returning providers. Use Skylib's string_flag / bool_flag / int_flag for plain knobs, set scope = "universal" only when the setting must apply to both target and exec configurations, use label_flag and label_setting when the value is a target reference (including a consumer's one-per-build choice between implementations of an interface), and reach for fragments + configuration_field only when bridging to legacy built-in configuration. Everything in this article is the foundation that 4.7.2 Starlark Transitions stands on — transitions need a labeled, typed setting before they can read or write anything.

Check your understanding · 6 questions

1.A rule author wants a typed configuration knob that users can flip on the command line and that downstream rules can read for analysis-time decisions. Which definition is correct?

Select one answer

2.Which statements about reading a build setting from a rule implementation are correct?

Select all that apply

3.Match each Bazel API to its primary role in the build-settings system:

Drag each answer onto the matching prompt, or click an answer and then click a prompt

Answers
config.string(flag = True)
label_flag
configuration_field(fragment = "coverage", name = "output_generator")
config_setting(flag_values = {...})

4.True or false: statements about the legacy fragments / configuration_field bridge and command-line addressing in transitions.

Choose True or False for each sentence

A rule that reads ctx.fragments.cpp.copts must declare fragments = ["cpp"] on its rule() call, otherwise the access is an analysis error.
configuration_field() is the modern Starlark replacement for label_flag. New code should prefer configuration_field() over label_flag.
A Starlark transition can address built-in Bazel flags through labels under the //command_line_option:* namespace.
--define is a legacy escape hatch and is not addressed through the Starlark build-setting label syntax.
A build-setting target can set scope = "universal" when the same setting should apply to both target and exec configurations.

5.A library module lets consumers choose its TLS implementation through label_flag(name = "tls_impl", ...), and its cc_library targets depend on :tls_impl. Which statements are true?

Select all that apply

6.A text rule reads a //:style string setting through a private _style label attribute. Developers switch between --//:style=upper and --//:style=lower on alternate builds and notice slow analysis. What explains it?

Select one answer

0 of 6 answered

Footnotes

  1. Configurations — user-defined build settings as a single entry in the configuration map, build_setting rule parameter, ctx.build_setting_value, implicit build_setting_default, command-line --//pkg:flag=value and --no//pkg:flag boolean syntax, flag_alias aliases, config_setting.flag_values integration, label-typed label_flag / label_setting, //command_line_option:* prefix and the deliberate --define / --config exclusions, and transition inputs/outputs by label. ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16

  2. config — descriptors config.bool, config.int, config.string, config.string_list, config.string_set with their flag and repeatable parameters. string_set recommendation over string_list to avoid configuration forking. allow_multiple deprecation in favor of string_list(repeatable = True). ↩1 ↩2 ↩3 ↩4 ↩5

  3. .bzl files — rule.build_setting adds a mandatory build_setting_default attribute whose type matches the descriptor. ↩

  4. ctx — ctx.build_setting_value exposes the current value. Reading it from a non–build-setting rule is an error. ↩

  5. rules_mojo repository map — mojo/build_settings.bzl defines repeatable_string_flag with config.string_list(flag = True, repeatable = True) and documents the --config stacking and no-comma-splitting rationale. ↩

  6. Bazel Knowledge: transition with style — a config.string(flag = True) style setting validated with fail(), returned as StyleProviderInfo, and read by a consumer text rule through a private _style label attribute; --//:style=upper restyles every target, the build log shows the analysis-cache discard warning, and an incoming rule(cfg = ...) transition later sets the value for one subtree. The repeated warning comes from a Bazel 9.1.0 probe that switched a one-consumer string setting between none, upper, and lower ↩1 ↩2

  7. Configurable Build Attributes — Skylib common_settings.bzl ships string_flag with a values allow-list and build_setting_default, plus analogous simple wrappers for common typed settings. ↩

  8. User's Manual — --flag_alias=alias_name=target_path binds a long build-setting label to a short alias. ↩

  9. Optimize Iteration Speed — many flag changes discard the analysis cache; keep a fixed flag set for a workflow, or use distinct output bases, each with its own Bazel server, for different flag sets ↩

  10. Bazel — core implementation, documentation, and regression corpus — Scope.java at 9.3.3 (also 9.0.0 and 9.2.0): the scope attribute accepts universal (the value changes only through an explicit transition), target (the value resets on exec transitions), and project ↩1 ↩2

  11. Bazel — core implementation, documentation, and regression corpus — the "Scope" section of docs/extending/config.mdx at 10.0.0rc1 and master: build settings apply only to the target configuration by default, and scope = "universal" applies them to target and exec configurations, with a Skylib bool_flag example. The 9.3.3 configuration docs do not have this section ↩1 ↩2

  12. Bazel 10.0.0rc1 release notes — --incompatible_exclude_starlark_flags_from_exec_config is flipped in 10.0, and bazelisk --migrate can test flipped flags on 9.x. ↩

  13. Bazel — core implementation, documentation, and regression corpus — with the flag at its 9.x default of false, settings without scope keep their value in exec configurations, like universal. With the flag on, only universal settings, project settings, and settings named in --experimental_propagate_custom_flag keep it. ↩1 ↩2

  14. Bazel — core implementation, documentation, and regression corpus — FunctionTransitionUtil.java at 9.3.1 and at 10.0.0rc4: with --incompatible_exclude_starlark_flags_from_exec_config on, universal and project settings always enter exec configurations, and only settings without a scope are checked against --experimental_propagate_custom_flag; when both apply, the declared scope takes precedence ↩

  15. Bazel — core implementation, documentation, and regression corpus, BuildOptionsScopeFunction.java at 10.0.0rc4 — reads the scope attribute's value even when it is not set explicitly; at 9.3.1 an attribute that is not set explicitly means unscoped. Bazel Skylib — standard Starlark utilities, rules, and testing helpers, common_settings.bzl — the scope default changed from universal in 1.9.0 to target in 1.9.1. The setting-scope-propagation snippet records 10.0.0rc4 runs with both Skylib versions ↩

  16. How to use Bazel for embedded development — label_flag for FreeRTOSConfig.h in upstream FreeRTOS without source patches, with an intentionally incompatible default target. ↩

  17. attr — attr.label(providers = ...): the providers that every dependency in the attribute must return. ↩

  18. Rules — configuration fragments require rule(fragments = [...]), undeclared fragment access is an error, and configuration_field() provides late-bound defaults (coverage _lcov_merger example). ↩1 ↩2 ↩3

  19. LateBoundDefault — late-bound default attribute values of type Label resolved at analysis time based on the current configuration. ↩

  20. transition — transitions declare inputs (build settings they may read) and outputs (build settings they may write) by label. The implementation function receives settings as a {label: value} dict. ↩