cfgmatic
High-level configuration management framework for Rust with derive macros, environment variable support, and validation.
Overview
cfgmatic provides a unified, type-safe API for configuration management with support for:
- Declarative Configuration: Derive macros for automatic config loading
- Environment Variables: NPM-style naming (
MYAPP__DATABASE__URL) - Built-in Validation: Integration with
validatorcrate - File-based Configs: Support for TOML, JSON, YAML formats
- Reactive Configuration: Optional reactive streams for config changes
- Cross-platform: Works on Linux, macOS, and Windows
Features
default: Enablesfilesandenvfeaturesfiles: File-based configuration loading (viacfgmatic-files)env: Environment variable support with derive macros (viacfgmatic-macros)reactive: Reactive configuration with tokio streams
Installation
[]
= "0.1"
= { = "1", = ["derive"] }
Quick Start
Basic Environment Configuration
use ;
use Deserialize;
With Validation
use ;
use Deserialize;
use Validate;
Nested Structures
use ;
use Deserialize;
// Environment variables:
// export MYAPP__SERVER__PORT=3000
// export MYAPP__SERVER__HOST=0.0.0.0
// export MYAPP__DATABASE__URL=postgres://localhost/mydb
// export MYAPP__DATABASE__POOL_SIZE=20
File + Environment Loading
use ;
use Deserialize;
Reactive Configuration
use ;
use StreamExt;
async
NPM-style Environment Variables
Environment variables use double underscore (__) as a separator for nested fields:
# Flat structure
# Nested structure
Configuration Derive Macro Attributes
Struct-level Attributes
#[config(prefix = "PREFIX")]: Set environment variable prefix#[config(nested)]: Mark as nested structure (don't use prefix)
Field-level Attributes
#[config(env = "NAME")]: Custom environment variable name#[config(default = "value")]: Default value (must be string)#[config(nested)]: Nest field with prefix separator
Examples
The examples/ directory contains complete working examples:
# Basic environment configuration
# Complete example with all features
MYAPP__APP_NAME="MyApp" MYAPP__SERVER__PORT=8080
# Custom prefix
MYAPP__PORT=3000
# Environment loader
# File configuration
# Nested configuration
MYAPP__SERVER__PORT=3000
# Reactive configuration
# With validation
MYAPP__PORT=8080
Advanced Usage
Custom Validation
use ;
Multiple Configuration Sources
use ConfigManager;
let config: AppConfig = new
.with_env_prefix
.with_file?
.load?;
Conditional Configuration
Feature Flags
Default Features
files: Enable file-based configuration viacfgmatic-filesenv: Enable environment variable support viacfgmatic-macros
Optional Features
reactive: Enable reactive configuration streams (requirestokio)
Relationship to Other Crates
cfgmatic is part of a modular configuration framework:
- cfgmatic-paths: Platform-specific path discovery
- cfgmatic-files: File discovery and parsing
- cfgmatic-macros: Proc macros for derive macros (internal, not published)
- cfgmatic: High-level configuration framework (this crate)
Performance Considerations
- Configuration loading is one-time at startup
- Environment variable parsing is cached
- File loading uses efficient deserialization
- Reactive configuration uses tokio channels for minimal overhead
Error Handling
All functions return Result<T, Error> where Error is:
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.