konfik
A flexible and composable configuration parser for Rust applications that supports multiple sources and formats.
Features
- 🔧 Multiple Sources: Load configuration from files, environment variables, and CLI arguments
- 📁 Multiple Formats: Support for JSON, YAML, and TOML configuration files
- 🎯 Priority System: CLI args > Environment variables > Config files
- ✅ Validation: Custom validation functions for your configuration
- 🚀 Zero Config: Works out of the box with sensible defaults
- 📦 Derive Macro: Simple
#[derive(Konfik)]for easy setup
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["derive"] }
= { = "4.5", = ["derive"] } # optional! only needed for cli arguments
Basic Usage
use ;
use Deserialize;
Advanced Configuration
use ;
use Deserialize;
use Parser;
Configuration Sources & Priority
konfik loads configuration from multiple sources in the following priority order (higher priority overrides lower):
- CLI Arguments (highest priority)
- Environment Variables
- Configuration Files (lowest priority)
Configuration Files
By default, konfik looks for these files in the current directory:
config.jsonconfig.yamlconfig.toml
You can specify custom files:
let config = default
.with_config_file
.with_config_files
.?;
Environment Variables
Environment variables are automatically mapped from your struct fields:
With a prefix:
let config = default
.with_env_prefix // MYAPP_DATABASE_URL, MYAPP_API_KEY, etc.
.?;
CLI Arguments
The CLI is integrated with clap. It detects at runtime which fields are still
missing and makes those required in the CLI:
Supported Types
Konfik supports all types.
Validation
Add custom validation logic:
let config = default
.with_validation
.?;
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Radicle
To clone this repository on Radicle, simply run:
rad clone rad:z2FpyXb6X6ENg3MvQPkMfqVN7LcD8