Structured configuration allows C++/Rust components to declare configuration schemas directly in their manifest. Benefits of using structured configuration include:
ffx tooling.For more details on the implementation and behavior of structured configuration, see its reference.
To use structured configuration in your component, you must update build rules, declare a schema, define values, and generate a client library.
To prevent cyclic dependencies when generating client libraries, define a fuchsia_component_manifest rule that compiles the component manifest. Pass this compiled manifest GN label into the fuchsia_component rule.
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/BUILD.gn" region_tag="component" adjust_indentation="auto" %}
You must declare a configuration schema in a component's manifest by adding configuration capabilities to the use block:
{ ... {% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/meta/config_example.cml" region_tag="config" %} }
Each configuration capability entry in use specifies:
config: The capability name used when routing configuration from a parent or product assembly.key: The field identifier generated in the component's structured configuration client library.type: The value's data type and constraints.availability (optional): Set to "optional" or "transitional" when the field has a default value in the manifest.default (optional): The fallback value used when the capability is routed from void (or unrouted with "transitional" availability).Structured config supports the following types:
booluint8, int8uint16, int16uint32, int32uint64, int64string (requires max_size property)vector (requires element and max_count properties)See the CML reference doc and Configuration capabilities for the complete syntax.
Once your component declares its configuration capabilities, values can be provided directly via default in the component manifest, routed from a parent component, or configured using Software Assembly.
Googlers only: if your component is configured differently for eng and non-eng build types, you must add verification of your structured configuration before you check it in.
If your component's configuration varies between products, see the documentation for Assembling Structured Configuration.
To package the component, add the fuchsia_component rule as a dependency of a fuchsia_package:
{C++}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/BUILD.gn" region_tag="package" adjust_indentation="auto" %}
{Rust}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/BUILD.gn" region_tag="package" adjust_indentation="auto" %}
The build system verifies your component's configuration schema. A component with a faulty configuration (for example: invalid type constraints or mismatched default values) will fail to build.
Component manager validates a component's configuration when the component is started.
Use ffx component show to print out a component's configuration key-value pairs. The component does not have to be running for this to work.
$ ffx component show config_example
Moniker: /core/ffx-laboratory:config_example
Component State: Resolved
...
Configuration: delay_ms -> Uint64(100)
greeting -> String("World")
...
Components with unresolved or invalid required configuration capabilities will fail to start.
Components read their resolved configuration values with a generated library. Generate a library using the following build templates:
{C++}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/BUILD.gn" region_tag="binary" adjust_indentation="auto" %} {% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/BUILD.gn" region_tag="library" adjust_indentation="auto" %}
{Rust}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/BUILD.gn" region_tag="binary" adjust_indentation="auto" %} {% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/BUILD.gn" region_tag="library" adjust_indentation="auto" %}
Use the following functions from the library to read configuration values:
{C++}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/main.cc" region_tag="imports" adjust_indentation="auto" %} ... {% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/main.cc" region_tag="get_config" adjust_indentation="auto" %}
{Rust}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/src/main.rs" region_tag="imports" adjust_indentation="auto" %} ... {% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/src/main.rs" region_tag="get_config" adjust_indentation="auto" %}
You can export a component‘s configuration to Inspect so that it is available in crash reports. The client libraries have functions to export a component’s configuration to an Inspect tree:
{C++}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/cpp/main.cc" region_tag="inspect" adjust_indentation="auto" %}
{Rust}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/rust/src/main.rs" region_tag="inspect" adjust_indentation="auto" %}
Use ffx inspect show to print out the component's exported configuration:
$ ffx inspect show core/ffx-laboratory\*config_example
core/ffx-laboratory\:config_example:
...
payload:
root:
config:
greeting = World
Components receive configuration values routed as configuration capabilities from their parent component, or fall back to their manifest default value when availability is "optional" (and routed from void) or "transitional" (and unrouted).
Note: When a component receives configuration capabilities from external parent components, it must take care to evolve its schema in coordination with any external providers of that configuration.
Parent components can define configuration capabilities in their capabilities block and offer them to static children via offer declarations, or offer them dynamically to children launched in a collection with allowed_offers: "static_and_dynamic".
First, the child declares the configuration capability in its use block:
{ ... {% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config_from_parent/rust/meta/config_example.cml" region_tag="config" %} }
When launching the child in a collection with the Realm protocol, pass a dynamic configuration capability offer in CreateChildArgs:
{C++}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config_from_parent/integration_test/cpp/test.cc" region_tag="create_child" adjust_indentation="auto" %}
{Rust}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config_from_parent/integration_test/rust/lib.rs" region_tag="create_child" adjust_indentation="auto" %}
See the full example for details.
ffx componentYou can override configuration capability values for a component from the command line using ffx component config set.
If you run the above configuration example without any overrides you can see it print the default configuration to its logs:
$ ffx component run /core/ffx-laboratory:hello-default-value fuchsia-pkg://fuchsia.com/rust_config_from_parent_example#meta/config_example.cm --follow-logs ... [02655.273631][ffx-laboratory:hello-default-value] INFO: Hello, World! (from Rust)
Using ffx component config set --reload allows you to override that greeting and reload the component instance with the override in effect:
$ ffx component config set --reload /core/ffx-laboratory:hello-default-value "greeting=parent component" ... [02622.752978][ffx-laboratory:hello-default-value] INFO: Hello, parent component! (from Rust)
Each configuration field is specified as KEY=VALUE where KEY is the key of a configuration capability declared in the component's manifest and VALUE is a string matching the type of the field.
You can use Realm Builder to define and route configuration capabilities to a component in integration tests.
TODO(https://fxbug.dev/42053123) Add a section covering use with subpackaged components.
{C++}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/integration_test/cpp/test.cc" region_tag="config_replace" adjust_indentation="auto" %}
{Rust}
{% includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/components/config/integration_test/rust/lib.rs" region_tag="config_replace" adjust_indentation="auto" %}
Realm Builder validates the capability value against the component‘s configuration schema. Any optional or transitional configuration capabilities that are not routed in the test realm (or are routed from void) resolve to the default values declared in the component’s manifest.