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(Config)]for easy setup
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["derive"] }
Basic Usage
use ;
use Deserialize;
Advanced Configuration
use ;
use Deserialize;
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_file
.?;
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
CLI arguments use kebab-case conversion:
Example usage:
Supported Types
konfik supports all types that implement serde::Deserialize, including:
- Primitives:
bool,i32,u32,f64,String, etc. - Collections:
Vec<T>,HashMap<K, V>,BTreeMap<K, V>, etc. - Optional values:
Option<T> - Nested structs
- Enums
- Custom types with
Deserializeimplementation
Complex Types from Environment/CLI
Environment variables and CLI arguments can parse complex types:
# JSON arrays and objects
TAGS='["web", "api", "rust"]'
CONFIG='{"timeout": 30, "retries": 3}'
# CLI
Validation
Add custom validation logic:
let config = default
.with_validation
.?;
Error Handling
konfik provides detailed error information:
match load
Examples
Web Server Configuration
use Config;
use Deserialize;
// config.toml
// host = "0.0.0.0"
// port = 8080
// workers = 4
// database_url = "postgres://localhost/myapp"
// log_level = "info"
// cors_origins = ["https://example.com"]
// Environment override: PORT=3000
// CLI override: ./server --port 9000 --log-level debug
Database Configuration
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.