Crates.io | settings_loader |
lib.rs | settings_loader |
version | 0.15.0 |
source | src |
created_at | 2025-02-11 00:37:08.362169+00 |
updated_at | 2025-02-26 01:58:06.949357+00 |
description | Opinionated configuration settings load mechanism for Rust applications |
homepage | https://github.com/dmrolfs/settings-loader-rs |
repository | https://github.com/dmrolfs/settings-loader-rs |
max_upload_size | |
id | 1550833 |
size | 145,568 |
settings-loader-rs is a Rust library designed to unify configuration sources from multiple origins—including configuration files, command-line arguments, and environment variables—into a single, coherent application representation. The primary goal is to decouple configuration sourcing from the application code, enabling applications to retrieve configuration values seamlessly without concerning themselves with how or where the data originates.
.json
).toml
).yaml
, .yml
).hjson
).ron
)To use settings-loader-rs
in your Rust project, add it as a dependency in your Cargo.toml
:
[dependencies]
settings-loader = "0.14"
settings-loader-rs
provides additional feature flags for common settings structures:
database
– Includes predefined structures for database configuration settings.http
– Provides common settings for HTTP-based applications.To enable specific features, modify your Cargo.toml:
[dependencies]
settings-loader = { version = "0.14", features = ["database", "http"] }
This library requires Rust 2018 edition or later.
settings-loader-rs
loads configuration from multiple sources and merges them into a single application representation.
The sources include:
application.yaml
(Base Configuration)
http_api:
host: 0.0.0.0
port: 8000
timeout_secs: 120
rate_limit:
burst_size: 8
per_seconds: 0.5
database:
host: localhost
port: 5432
database_name: weather
require_ssl: false
max_connections: 10
max_lifetime_secs: 1800
acquire_timeout_secs: 120
idle_timeout_secs: 300
production.yaml
(Production environment overrides)
application:
host: 0.0.0.0
database:
host: postgres_1632546102
local.yaml
(Local environment overrides)
http_api:
host: 127.0.0.1
base_url: "http://127.0.0.1"
database:
username: postgres
password: postgres
require_ssl: false
secrets.yaml
(Secret credentials sourced from a secure repository during deployment - or not used in favor of
environment variables)
database:
username: my_user_name
password: my_secret_password
You can define a CLI options structure to support including command-line options in setting. For example, define a
CliOptions
struct in settings::cli_options.rs
specifying the available CLI options using the clap
crate. One role
for CliOptions
is to configured how settings are loaded:
Flag | Description |
---|---|
-c, --config <PATH_TO_CONFIG_FILE> |
Load an explicit configuration file, bypassing inferred configuration. |
--secrets <PATH_TO_SECRETS_FILE> |
Specify a path to a secrets configuration file. |
-e, --env <ENVIRONMENT> |
Specify the environment configuration override (local , production , etc.). |
-s, --search-path <SETTINGS_SEARCH_PATH> |
Override the filesystem path used to search for configuration files (separated by : ). |
local.yaml
as an environment override:cargo run -- --env local
cargo run -- --config ./custom_config.yaml
cargo run -- --secrets ./secrets.yaml
cargo run -- --search-path "./config:./resources"
Here’s an example of how an application can use settings-loader-rs
to load configurations dynamically based on
CLI options:
use anyhow::anyhow;
use clap::Parser;
use settings_loader::{LoadingOptions, SettingsLoader};
use my_app::settings::{CliOptions, Settings};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let options = CliOptions::parse();
if options.secrets.is_none() {
tracing::warn!("No secrets configuration provided. Passwords (e.g., for the database) should be confined to a secret configuration and sourced in a secure manner.");
}
let settings = load_settings(&options)?;
// ... define application that uses Settings ...
}
Parsing CLI Options: CliOptions::parse()
extracts configuration options from the command line.
Loading Configuration: load_settings(&options)
loads and merges settings from:
application.yaml
)production.yaml
or local.yaml
)In addition to configuration files, environment variables can be used to override settings dynamically. The
SettingsLoader::load()
function integrates environment-based configuration loading.
export APP_ENVIRONMENT=production
export DATABASE_USERNAME=my_user
export DATABASE_PASSWORD=my_secure_password
export HTTP_API_HOST=192.168.1.100
export HTTP_API_PORT=8080
APP_ENVIRONMENT
variable determines which environment-specific configuration file (production.yaml
,
local.yaml
, etc.) is used.DATABASE_USERNAME
) override values in application.yaml
and environment-specific files.application.yaml
(base config)local.yaml
or production.yaml
(environment-specific overrides)Run the application with environment variables applied:
APP_ENVIRONMENT=local DATABASE_USERNAME=custom_user cargo run