Skip to main content

Module interpolate

Module interpolate 

Source
Expand description

${VAR} substitution for configuration values.

Config files are checked into repos and shared; secrets are not. So a value may name an environment variable instead of carrying one:

[provider]
api_key = "${OPENAI_API_KEY}"

Two forms, and both are deliberately small:

  • ${NAME} — the value of NAME; an unset variable is an error that names the key, never an empty string. A provider that silently gets "" for its key fails much later and much less clearly than a config that refuses to load.
  • ${NAME:-fallback} — the value, or fallback when NAME is unset or empty (the shell’s meaning of :-).

$$ escapes a literal $, so $${X} stays ${X}. There is no recursion — the result of a substitution is never itself substituted — and no command substitution. Anything else starting with $ is left alone, which keeps prices, $HOME-looking prose in a prompt, and regex fragments working.

Substitution happens on parsed TOML values, not on the file text: a comment that mentions ${HOME} is documentation, not a reference, and must not be able to break a config or leak a value into the wrong place.

Functions§

in_value
Substitute ${...} in every string inside value, in place.
substitute
Substitute in one string. path is the config key, for error messages.