Lesson 5 of 9 · 25m
Configuration Layering Without Surprise
Combine defaults, files, environment variables, and flags with explicit precedence, validation, secret handling, and inspectable effective config.
Every mature CLI eventually becomes a small configuration management system. It starts with --config, then someone needs environment variables for CI, then XDG paths, then profiles, then remote secrets, then a debugging command because nobody knows which value won. Configuration is where operational trust goes to die unless precedence is explicit and inspectable.
Use a fixed precedence model
A practical precedence order is:
built-in defaults
< system config file
< user config file
< environment variables
< command-line flagsLater sources override earlier ones. Put this exact order in the manual and expose it in tool config explain. Do not create special cases because one integration was convenient. Special cases are permanent.
Model the final configuration as a typed struct
#[derive(Debug, Deserialize)]
struct FileConfig {
registry: Option<String>,
timeout_seconds: Option<u64>,
output_dir: Option<PathBuf>,
}
struct EffectiveConfig {
registry: String,
timeout: std::time::Duration,
output_dir: PathBuf,
}The file format uses optional values because absence means "inherit from a lower layer," not zero, false, or empty string. The effective config uses concrete types after merge and validation.
Validate after merging, not before
A file may legally omit registry because the flag supplies it. Validate individual syntax early if useful, but validate required combinations only after all layers merge. Otherwise you force users to duplicate values into every source.
anyhow::ensure!(!effective.timeout.is_zero(), "timeout_seconds must be greater than zero");
anyhow::ensure!(effective.registry.scheme() == "https", "registry must use https");Validation should include consequences, not merely syntax: zero timeouts, non-HTTPS endpoints, world-readable credential files, overlapping cache directories, and impossible concurrency values.
Environment variables are an interface
Use a consistent prefix and document every variable:
DEPLOYCTL_REGISTRY=https://registry.sudosecurity.org
DEPLOYCTL_TIMEOUT_SECONDS=30
DEPLOYCTL_TOKEN=file:///run/secrets/deployctl-tokenPrefer file indirection for secrets where possible. Process environments leak into crash reports, debugging output, systemd unit dumps, and sometimes child processes. If a token is required, do not echo it in config explain; print its source and a redacted fingerprint.
Make the winner visible
The single most valuable operational feature is an inspection command:
deployctl config explain --output json{"registry":{"value":"https://registry.sudosecurity.org","source":"flag"},"timeout_seconds":{"value":30,"source":"environment"}}Without this, every support ticket begins with archaeology.
Respect platform locations
On Linux use XDG directories, usually $XDG_CONFIG_HOME or ~/.config. On macOS and Windows use the platform-appropriate locations exposed by a crate such as directories. Do not scatter dotfiles through the home directory in 2026 unless compatibility requires it.
Field notes
- Empty string, absent value, and explicit null are different states; decide which ones your format supports.
- Reloading configuration while a long-running operation is underway creates inconsistent behavior; snapshot it at startup unless dynamic reload is deliberate.
- Configuration examples should be complete and small, not exhaustive fantasy files.
- Validate permissions on private key and token files, but remember filesystems differ on Windows and network mounts.
- Never let remote configuration silently widen local privileges, such as changing plugin paths or executable locations.
Was this lesson helpful?