ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

Integrations

ConfigurationSourcesValidation and problemsSecrets

Operating

Reference

Configuration

Sources

The five sources of a Config, how names map to paths, and the file formats.

A source is added with Config::builder().source(..). The order added is the order of precedence: a later source overrides an earlier one, key by key.

SourceReadsOrigin recorded
Defaults::of::<T>()T::default()default
File::toml(path), File::json(path), File::yaml(path)A fileThe path, line and column
DotEnv::file(path)A .env fileThe path and line
Env::process(), Env::from_map(..)The environmentThe variable's name
Values::new().set(path, value)Values set in codeThe key

Defaults

Defaults::of::<T>() takes T: Default + Serialize and reads T::default() into the tree. .at("http") places it at a section; with no path it covers the whole configuration. Added first, it is the bottom layer.

Secret has no Serialize, so a section holding a Secret cannot use Defaults::of. examples/33-config keeps the defaults in a type of their own, with no database URL, and gives the URL no default:

examples/33-config/src/lib.rs
use serde::Serialize;
use ulo_config::Defaults;

/// The bottom layer. `Defaults::of` needs `Serialize`, which a `Secret` does not have, so the
/// defaults are a type of their own and the URL has none.
#[derive(Serialize)]
struct Fallbacks {
    greeting: &'static str,
    http: HttpFallbacks,
    database: DatabaseFallbacks,
}

The other route is #[serde(default)] on the field. The value serde fills in carries no origin.

Files

File::toml reads TOML 1.0. File::json reads strict JSON, with no comments and no trailing commas. Both report a line and column per value. File::yaml reads YAML 1.2 and needs the yaml cargo feature of ulo-config.

A file is required unless .optional() is written. A missing optional file is no failure, and a file that exists and does not parse still is.

YAML limits:

  • Anchors and aliases are allowed, bounded at 100,000 expanded nodes. A file past the bound is refused with a problem naming the line of the alias that passed it.
  • A merge key (<<) is a problem naming its line, never merged.
  • Scalars resolve by the YAML 1.2 core schema, so yes given to a bool is a type problem.

Environment variables and .env

Env::process().prefix("APP_").separator("__") and a .env file with the same options turn a name into a path by removing the prefix, splitting at the separator, and lowercasing each segment. The separator is __ by default. APP_HTTP__PORT becomes http.port:

examples/33-config/config/.env
# Read into the layer, never into the process environment.
APP_GREETING="hello from .env"
APP_HTTP__PORT=5000

Only names beginning with the prefix are read. With no prefix every variable in the process becomes a key, and each that no section reads is reported as unread. Set a prefix.

A value from the environment or a .env file is text. A typed read parses it as the number or boolean the field asks for. A field read through #[serde(flatten)], #[serde(untagged)] or a serde_json::Value cannot take a number from the environment.

A variable whose name or value is not UTF-8 is left out.

The .env reader is the crate's own and accepts this subset:

FormRule
A lineKEY=VALUE, with an optional export prefix followed by a space
Single quotesLiteral
Double quotes\n, \" and \\ as escapes
Comments# at the start of a line, or after whitespace in an unquoted value
ExpansionNone
Multi-line valuesNone

A key written twice in one file is a problem naming both lines. A later source overriding a key is layering and is not a problem.

Values set in code

Values::new().set("http.port", 8080) takes a path and a bool, integer, float or string. It is the layer for what only main knows, such as a port read from a flag.

Precedence in examples/33-config

The example's test gives each field to a different layer: the defaults supply timeout_secs, the file pool_size, the .env file the port, the environment the greeting and the explicit values the host.

examples/33-config/tests/layers.rs
{
  "greeting": "hello from the environment",
  "host": "127.0.0.1",
  "port": 5000,
  "pool_size": 10,
  "timeout_secs": 30,
  "database_url": "Secret([redacted])"
}

Configuration

Layer defaults, files, the environment and explicit values into one typed value, and have wire() report every problem with its source.

Validation and problems

What wire() reports for a bad configuration, how to add your own checks, and how unread keys are handled.

On this page

DefaultsFilesEnvironment variables and .envValues set in codePrecedence in examples/33-config