Announcing config-shellexpand: Tiny library for configuration placeholders in config-rs

⚓ Rust    📅 2026-03-03    👤 surdeus    👁️ 2      

surdeus

I found myself needing to use placeholders in my configuration file, but config doesn't support them.

I found an open ticket about it and a draft PR, so I decided to write a small library that implements it by combining the file sources from config with shellexpand.

config.toml

value = ${NUMBER_FROM_ENV}
    
[section]
name = "${NAME_FROM_ENV}"

main.rs

use config_shellexpand::TemplatedFile;
use config::Config;
    
let config: Config = Config::builder()
    .add_source(TemplatedFile::with_name(path))
    .build();

When loading, the contents of the files are read into memory, then expanded with shellexpand, and finally loaded using config's FileFormat, like non-expanded files.

You can optionally provide a Context (with_name_and_context) that is passed on to shellexpand for variable lookups if you want to source them from somewhere other than the environment (the tests use this a lot).

It also works with strings if you provide the file format (just like it works in config).

1 post - 1 participant

Read full topic

🏷️ Rust_feed