# Mogh Config
Module for comprehensive loading of strongly typed configuration files using `std::fs` and `serde`.
- Supports parsing JSON, YAML, TOML, and env files (`.env`, `*.env`).
- Supports merging final configuration from multiple supplied files / directories.
- Supports `${ENV_VAR}` and `$(command)` interpolation in values of
local toml / yaml / json files (not in env files or in files loaded
from Cicada, whose values are secrets taken verbatim). `$(command)` runs
a single command word (letters, digits, `_`) without arguments, eg.
`$(hostname)`; anything else, like `$(cat /run/secrets/db)` or
`${VAR:-default}`, is kept as written, with a warning naming the key.
- Coerces string values into the field's type the way `envy` reads the
process environment: numbers, booleans, comma separated lists into
`Vec`s, `Option`, enum variants. Not inside `#[serde(flatten)]` fields
or untagged / internally tagged enums, which serde buffers as-is.
Priority (later overrides earlier): `paths` in order given. Within a directory,
its own files (by wildcard, then name; a file matching several wildcards takes
the last one it matches), then each path in the include file in the order
listed, recursively, so includes override the directory's own files. A file
reached twice (listed as a path and found by a directory scan) is loaded once,
at its later position. A wildcard which doesn't compile is an error
(`Error::InvalidWildcard`), never a dropped filter.
Missing and broken files: a path which doesn't exist is skipped, so an app can
list optional paths. Every file which exists must load, whether listed, found
by a directory scan or named in an include file: one which can't be read or
doesn't parse is an error naming the file (and the line, never a value in it),
and so is a directory or include file which can't be read. Skipping it would
start the app without the settings in it. A file holding no settings (blank,
comments only, a yaml or json `null` document) is an empty source. A file a
directory scan finds which is no config file type (a `config.toml.bak` matching
`*config.*`) is skipped with a warning; listed, or named in an include file, it
is an error (`Error::UnsupportedFileType`).
Unknown keys: a key of a toml, yaml or json file which no field of the config
type takes (a typo such as `disable_user_registraton`, a renamed or removed
field) is ignored, with a warning naming the file and the key's path, never the
value: `WARN: "/config/core.config.toml" | 'disable_user_registraton' is no
config field, it is ignored (...)`. The warnings go to stdout like the loader's
others, as the loader runs before an app's logger. Env files, `cicada:` sources
and the process environment are not checked: keys of other programs are normal
there (a compose project's `.env`, the secrets of an environment). A table the
files may spell flat (`EnvSource::flat_prefix`) is checked in its table: an
unknown `oidc_enabeld` is reported as `oidc.enabeld`. A key only an earlier file
holds, which a later file replaced (without `merge_nested`), is no longer in the
config and not reported. `load_reporting` / `load_with_env_reporting` print
nothing and return the keys with the config (`Loaded { config, unknown_keys }`,
each a `mogh_config::UnknownKey` with its `file` and `path`): an app logs them
through its own logger once it is up (`warn!("{key}")`, the `Display` is the
warning's text), and a test asserts that a config file the app ships has none.
```rust,no_run
use std::path::Path;
use mogh_config::ConfigLoader;
#[derive(serde::Deserialize)]
struct Config {
title: String,
aliases: Vec<String>,
endpoint: String,
use_option: bool,
}
let config = (ConfigLoader {
// Read config files from a directory
paths: &[Path::new("./configs")],
match_wildcards: &["*config*.toml"],
// It won't recurse into subdirectories unless they include '.configinclude' file
include_file_name: ".configinclude",
merge_nested: true,
extend_array: true,
debug_print: true,
})
.load::<Config>()
.expect("Failed to parse config from path");
```
Merging: each source merges over the ones before it, and is never dropped
over a type conflict. With `merge_nested`, two objects merge key by key; with
`extend_array`, an array extends the one before it. A `null` (a yaml section
with every line commented out) keeps the object or array before it. Any other
value replaces the one before it, as it does without those flags, and a value
the config type can't take fails the final deserialization with its path and
type (never the value). `mogh_config::merge_objects` keeps its stricter rules
(a type mismatch is an error) for callers merging anything else.
## The process environment
`ConfigLoader::load_with_env(&EnvSource)` reads the process environment as the
last source, over the config files: each variable of the `EnvSource`'s table
overrides one config field. An app declares its variables once, by config path,
instead of an `Env` struct mirroring its config and a line per field copying it
over:
```rust,no_run
use std::path::Path;
use mogh_config::{ConfigLoader, EnvSource};
#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
struct Database {
address: String,
password: String,
}
#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
struct Config {
host: String,
port: u16,
allowed_ips: Vec<String>,
database: Database,
}
let env = EnvSource::new("APP_")
// APP_HOST, APP_PORT, APP_ALLOWED_IPS, APP_DATABASE_ADDRESS,
// APP_DATABASE_PASSWORD: a variable for every field.
.fields_of(&Config::default())
// Or by path: .fields(["host", "port", "database.address"])
// A name of its own.
.var("APP_MONGO_URI", "database.address")
// An old name, still read.
.alias("APP_LISTEN_PORT", "APP_PORT");
let config = (ConfigLoader {
paths: &[Path::new("./config")],
match_wildcards: &["*config.*"],
include_file_name: ".configinclude",
merge_nested: true,
extend_array: false,
debug_print: false,
})
.load_with_env::<Config>(&env)
.expect("Failed to load the config");
```
The rules:
| | |
| --- | --- |
| Names | The prefix, then the config path's segments uppercased and joined by `_`: `host` is `APP_HOST`, `database.address` is `APP_DATABASE_ADDRESS`. `nested_separator("__")` joins them with `__` instead (`APP_DATABASE__ADDRESS`). `fields_of(&config)` makes one for every value of the serialized config which is no object (`without([...])` leaves paths out), `fields([...])` one per path, `var(name, path)` gives one a name of its own. Names match in any case (`app_port`), as `envy` reads them. |
| Unknown variables | Ignored, also with the prefix (the `APP_CONFIG_PATHS` the app reads itself, a typo). |
| Values | Strings, parsed into the field's type as env file values are: numbers and booleans trimmed, lists split on commas (entries trimmed, empty ones dropped), a unit enum variant by name. A field which reads a string itself (its own `deserialize_with` going through `deserialize_any`) gets the whole value, eg. a list it splits on `;`. |
| Type errors | `Error::ParseEnv`, naming the variable as it was set (`APP_PORT`, `APP_PORT_FILE`) and the field, never the value. |
| Blank | A variable set to nothing but whitespace and commas counts as unset: the config file's value stays (compose templates optional variables as `${APP_X:-}`). A string or a list can't be emptied from the environment (for an allow list, empty can mean everyone), unless the variable is listed in `allow_empty([...])`: then a blank value sets the empty value (a security header left out when empty). |
| `_FILE` | Every variable `NAME` can be given as `NAME_FILE`, the path of a file holding the value (a docker secret): its contents, trimmed. It wins over `NAME`. A blank `NAME_FILE` counts as unset (`mogh_secret_file`'s rule), and so does a file holding nothing but whitespace and commas. A file which can't be read is `Error::EnvFile`, naming the variable and the path. `NAME_FILE` is a variable of its own when the table has one by that name (a field `health_file`). For the variables of `file_spec([...])` the file is not read: the field gets `file:<path>` (a key file the app reads, creates or rotates itself). |
| Aliases | `alias(old, name)` reads `old` (and `old_FILE`) for `name` when `name` (and `name_FILE`) is unset. Both set: `name` wins, `old` is ignored with a warning naming both, never the values. `fallback(other, name)` does the same for a variable of other programs (`TZ`), without the warning or a `_FILE`. |
| Overrides | A value replaces the field: a list too (`extend_array` doesn't extend it), a nested field only itself (the other fields of its object stay, whatever `merge_nested` says). A field the config type also takes under another key (`#[serde(alias = "mongo")] database`) is declared with `key_alias("database", "mongo")`: a file's value under the other key then moves to the field's own before the environment's values go in (else the field would be there twice, which serde refuses). |
| Flat tables | `flat_prefix("oidc", "oidc_")` lets a config file spell the fields of the table `oidc` as top-level keys `oidc_enabled`, `oidc_client_id`, ... (an env file's `OIDC_ENABLED=true` too). Each source is rewritten as it is parsed, before the sources merge, so files may spell a field differently. The longest prefix wins, a top-level key which is a field of the config (the first segment of a variable's path, or a rule's path) is never a flat spelling, and one file spelling a field both ways is `Error::FlatKeyTwice`, naming the file and both keys. The variables are named after the field's path either way. |
| The table | Checked when it is read: a name twice, a path inside another's, an alias of no variable is `Error::InvalidEnvSource`, the app's mistake. `names()` lists the table's variables (eg. for docs, or a test that a renamed field kept its variable). |
## The loader's own settings
The settings of the loading itself (which files) come from the environment too,
read before the config files. `LoaderEnv::new(prefix)` is the one table every
app reads them with, named with the app's prefix:
| Variable | Setting | Default |
| --- | --- | --- |
| `<P>CONFIG_PATHS`, or `<P>CONFIG_PATH` | `config_paths`: the files or directories, a comma separated list | none, `default_paths([..])` |
| `<P>CONFIG_KEYWORDS`, or `<P>CONFIG_KEYWORD` | `config_keywords`: the wildcards of the files in the directories, a comma separated list | `*config.*`, `default_keywords([..])` |
| `<P>MERGE_NESTED_CONFIG` | `merge_nested_config` | `true`, `default_merge_nested(..)` |
| `<P>EXTEND_CONFIG_ARRAYS` | `extend_config_arrays` | `true`, `default_extend_arrays(..)` |
| `<P>CONFIG_DEBUG` | `config_debug`: the loading prints what it loads | `false` |
Each is read by the rules of the table above (names in any case, blank is
unset, `_FILE`, a value the setting can't take is `Error::ParseEnv` naming the
variable). The singular names are read while the plural ones are unset.
`allow_empty_paths()` makes a blank `<P>CONFIG_PATHS` load no config file rather
than the default paths (an image's `/config`). `debug(LoaderDebug::..)` picks
what sets `config_debug`: `<P>CONFIG_DEBUG` (the default), the app's own log
level `<P>LOGGING_LEVEL` at `debug` or `trace` (one of mogh_logger's levels,
else an error), or no variable (the app decides, eg. from a command line
argument). `names()` and `alias_names()` list the variables, eg. for a test
that the documented variables are read.
```rust
use std::path::PathBuf;
use mogh_config::{ConfigLoader, LoaderEnv};
let settings = LoaderEnv::new("APP_")
.default_paths(["/config"])
.allow_empty_paths()
// `.read()` reads the process environment.
.read_vars([("APP_CONFIG_KEYWORDS", "*config.*, *.env")])
.expect("Failed to read the config loading variables");
let paths = settings
.config_paths
.iter()
.map(PathBuf::as_path)
.collect::<Vec<_>>();
let keywords = settings
.config_keywords
.iter()
.map(String::as_str)
.collect::<Vec<_>>();
let loader = ConfigLoader {
paths: &paths,
match_wildcards: &keywords,
include_file_name: ".appinclude",
merge_nested: settings.merge_nested_config,
extend_array: settings.extend_config_arrays,
debug_print: settings.config_debug,
};
# let _ = loader;
```
`ConfigLoader::from_env()` is a loader of the same variables without a prefix
(`CONFIG_PATHS`, `CONFIG_KEYWORDS`, `MERGE_NESTED_CONFIG`,
`EXTEND_CONFIG_ARRAYS`, `CONFIG_DEBUG`), with `ConfigLoader::default()`'s values
as the defaults and its include file name. The loader borrows its lists, so what
is read is kept for the rest of the process: read the environment once, at
startup. `ConfigLoader::from_vars` reads given variables instead (tests). Before
5.0 it read `CONFIG_MATCH_WILDCARDS`, `CONFIG_INCLUDE_FILE_NAME`,
`CONFIG_MERGE_NESTED`, `CONFIG_EXTEND_ARRAY` and `CONFIG_DEBUG_PRINT`, which are
no longer read.
```rust,no_run
use mogh_config::ConfigLoader;
#[derive(serde::Deserialize)]
struct Config {
title: String,
}
// CONFIG_PATHS=/etc/app CONFIG_KEYWORDS=*config.* MERGE_NESTED_CONFIG=true
let config = ConfigLoader::from_env()
.expect("Failed to read the loader variables")
.load::<Config>()
.expect("Failed to load the config");
```
## Env files
An env file is a flat configuration source: `NAME=value` lines, `#`
comments, optional `export`, values unquoted, single quoted (literal) or
double quoted with `\n` / `\r` / `\t` / `\"` / `\\` / `\uXXXX` escapes. Names are lowercased
to match struct fields (`DB_PASSWORD=x` fills `db_password`) and dots
nest (`DATABASE.ADDRESS=x` fills `database.address`), values are strings
coerced into the field's type, so the same struct reads a toml file, an
env file, or both merged. A name with an empty segment cannot nest
(`.dockerconfigjson`, `A..B`, `A.`), so it is kept whole as one flat key,
lowercased (`.dockerconfigjson`, `a..b`; reach it with
`#[serde(rename = ".dockerconfigjson")]`). Nesting stops at 32 segments: a
longer name is one flat key too. A name that is both a value and
an object (`DATABASE=x` next to `DATABASE.ADDRESS=y`), or two names equal
once lowercased (`DB` and `db`), is an error naming the line and both
names. Values are never interpolated: they are secrets, not templates.
A parse error names the line (and entry) and what was expected, never
the value: not even the character after an unsupported escape.
```sh
# .env, listed in the paths after the defaults
PORT=8080
ALLOWED_HOSTS=a.example.com,b.example.com
DATABASE.ADDRESS=db.example.com:5432
DATABASE.USERNAME=app
DATABASE.PASSWORD="hunter2"
```
A comma separated value from an env file merged onto an array from an earlier
source extends it under `extend_array` (`HOSTS=b,c` onto `["a"]` gives
`["a", "b", "c"]`), and replaces it otherwise (the final deserialization
splits it into the list).
List an env file as a path, or match it with a directory wildcard
(`*.env`, `.env`): a directory scanned without wildcards skips env files,
so a compose `.env` next to the config files is not read by accident.
`.env` has no extension for `Path::extension`, and is recognized by name.
Both directions of the grammar live in
[`mogh_env_parser`](https://crates.io/crates/mogh_env_parser), re-exported here.
`mogh_config::parse_env_file` reads a file into its entries: the line, the
name, the value, and the description held by the `#` comment lines directly
above the entry (a blank line detaches them). `mogh_config::serialize_env_file`
writes entries back, each description as comment lines above its entry, a value
holding `$` single quoted when it can be (Compose, systemd and dotenv libraries
take it literally then), any other value double quoted when it wouldn't survive
unquoted, a character another dotenv parser ends a line at (VT, FF, FS, GS, RS,
NEL, U+2028, U+2029) written as its `\uXXXX` escape. A serialized file parses
back to the same entries, each description
as `mogh_config::normalize_env_description` has it: every line break a `\n`
(`\r\n`, and a lone `\r` or another character some dotenv parser ends a line
at, so no text of a description is an entry to them), no line ending in
whitespace (a comment line can't keep it). The parser reads descriptions in
that form too. An app storing descriptions normalizes them with it when they
are written, so an export / import round trip gives them back unchanged. A name
the syntax can't hold (`mogh_config::representable_env_name`) or a name given
twice is an error. Cicada Core renders its exports with `serialize_env_file`,
so a `cicada://.env` source is read by the parser of the same grammar, and
`cicada_loader::load_env_as` reads an environment with the same parser and
coercion (`mogh_env_parser::from_str`), so it fills a struct exactly as a
`cicada://.env` source does.
## Features
- `cicada` (off by default since 3.0): load config from [Cicada](https://github.com/moghtech/cicada)
with `cicada:` paths, the syntax of the
[`cicada_loader`](https://crates.io/crates/cicada_loader) crate
(`cicada_loader::CicadaPath`): `cicada://filesystem/config.yaml?env=prod` is a
file interpolated with the `prod` environment, `cicada://.env?env=base+prod`
the environments themselves as an env file, and `+` layers environments (later
wins). This crate tells such a path by its `cicada:` scheme and hands it to the
loader as written, which parses and loads it; the file's name inside Cicada
picks the format (`config.yaml` as yaml, `.env` as an env file), as for a local
file. The feature pulls the Cicada client into the build, so it is opt in:
`mogh_config = { version = "4", features = ["cicada"] }`.
A `cicada:` line in an include file is a source like a `cicada:` path.
Loading a `cicada:` source is blocking network I/O (reqwest's blocking
client: the device's key check, onboarding or rotation, then the file), so
call `ConfigLoader::load` outside an async runtime: before building the
runtime (a plain `fn main` which loads the config, then starts tokio), or in
`tokio::task::spawn_blocking`. On a runtime thread it panics in debug builds
and blocks the worker in release builds.
Without the feature a `cicada:` path is an error, not a skipped path. With
it, a `cicada:` source which fails to load (`Error::CicadaLoad`: no path the
loader takes, Core unreachable, the device not onboarded or not granted the
environments, the file missing) or to parse is an error too, rather than
starting the app with its defaults, like a local file which doesn't parse.
The loader is re-exported as `mogh_config::cicada_loader`, so an application
using its own API (starting the background with `cicada_loader::spawn()`,
reacting to changes with `on_change` / `subscribe`, `load_path_as`,
`load_env_as`) reaches it there, at the one version `mogh_config` uses, with
no second dependency to keep in line: two loader versions in one process
would each initialize a device from the same `CICADA_*` environment and key
file. A `cicada_loader` minor (`0.x`) bump ships as a `mogh_config` minor
release (its notes name the loader's changes), and a loader patch bump as a
patch release. The background starts on its own when something needs it: a
change hook, `CICADA_EXIT_ON_CHANGE`, `CICADA_KEY_ROTATION_INTERVAL`, or a
device key which expires under a max age.