OmegaConf.NET is a hierarchical, composable configuration framework for C# and .NET inspired by Python's Hydra and OmegaConf.
It provides tree-based configuration composition, automatic inheritance cascading, in-config mutation operators (+=, -=, *=, /=, &=, |=, !=), strict structured typing, variable interpolation, parameter sweeps, and native Microsoft.Extensions.Configuration integration.
- Hierarchical Composition: Organize configurations into modular Config Groups (e.g.,
db/,model/,env/) and compose them viadefaultslists. - Auto-Cascading & Explicit Inheritance: Nested configurations automatically cascade on base files sharing the same name (
db/staging/postgres.yamlinherits fromdb/postgres.yaml). Also supports explicit_inherits_: "../base.yaml". - In-Config Mutation Operators: Key suffixes modify inherited values (
+=,-=,*=,/=,&=,|=,!=). - Format Agnostic: Unified AST parsing and serialization across YAML, JSON / JSONC, and TOML.
- Variable Interpolation:
${path.to.key}with native type preservation (numbers remain numbers, booleans remain booleans).- List indexing:
${servers[0]}or${servers.0}. - Sibling and parent navigation:
${.sibling}and${..parent}. - Built-in math and logic evaluation via
${eval:...}. - Environment variables:
${env:VAR, fallback}and${oc.env:VAR, fallback}. - Extensible custom C# resolvers.
- Cycle detection and circular dependency prevention.
- Mandatory Value Markers (
???): Mark keys that must be supplied by child layers or command-line arguments. - Command-Line Overrides: Override group selections (
db=production/postgres), drop groups (~db), mutate values (threads+=2,debug!=true), or enforce strict mode (+key=val,++key=val). - Parameter Sweeps (Multi-Run): Run Cartesian product sweeps across comma-separated values or generator functions (
range(),linspace(),choice(),glob()). - Structured Configs: Bind AST nodes to C# POCOs and positional
recordprimary constructors with automatic casing translation (PascalCasetosnake_case,kebab-case, or exact match). - .NET Integration: First-class
IConfigurationSourceandConfigurationProviderfor ASP.NET Core and Microsoft DI.
dotnet add package OmegaConf.NET dotnet add package OmegaConf.NET.Extensions
configs/
├── config.yaml # Root entrypoint
└── db/
├── postgres.yaml # Base defaults
├── sqlite.yaml
├── staging/
│ └── postgres.yaml # Staging overrides (cascades on db/postgres.yaml)
└── production/
└── postgres.yaml # Production overrides with operators
defaults: - db: staging/postgres - _self_ app_name: "CatalogService" active_workers: 4
driver: postgres port: 5432 max_connections: 50 ssl: false timeout_seconds: 30
host: "staging-db.internal" max_connections: 20
host: "prod-db.internal" max_connections*=: 4 # 50 * 4 = 200 ssl: true timeout_seconds+=: 30 # 30 + 30 = 60
using OmegaConf.NET.Attributes; public class DatabaseConfig { public string Driver { get; set; } = "postgres"; public string Host { get; set; } = "localhost"; public int Port { get; set; } = 5432; public int MaxConnections { get; set; } public bool Ssl { get; set; } public int TimeoutSeconds { get; set; } } public class AppConfig { [ConfigRequired] public string AppName { get; set; } = ""; public DatabaseConfig? Db { get; set; } public int ActiveWorkers { get; set; } = 4; } // Positional records are also supported: public record ServiceRecordConfig( [property: ConfigRequired] string AppName, DatabaseConfig? Db, int ActiveWorkers = 4);
using OmegaConf.NET; AppConfig config = OmegaConfBuilder.Create<AppConfig>() .SetRoot("configs") .AddCliArgs(args) // CLI overrides, e.g. db=production/postgres active_workers+=2 .Build(); Console.WriteLine($"App: {config.AppName}"); Console.WriteLine($"Database: {config.Db?.Host}:{config.Db?.Port} (Connections: {config.Db?.MaxConnections})");
Operators are declared as key suffixes in configuration files or passed via CLI overrides:
| Operator | Action | Target Types | Example |
|---|---|---|---|
= |
Assignment / replace | Any | port=: 5432 |
+= |
Add / concatenate / append | Numbers, Strings, Lists, Objects | timeout+=: 10, tags+=: ["gpu"] |
-= |
Subtraction / item removal | Numbers, Lists | retry_count-=: 1, items-=: ["old"] |
*= |
Multiplication / repeat | Numbers, Strings | workers*=: 2, "ha"*=: 3 |
/= |
Division | Numbers | rate/=: 2 |
&= |
Logical / bitwise AND | Booleans, Integers | strict_mode&=: false |
| ` | =` | Logical / bitwise OR | Booleans, Integers |
!= |
Boolean toggle / invert | Booleans | debug!=: true |
# Switch config group choice dotnet run -- db=production/postgres # Drop a config group dotnet run -- ~db # Override properties with mutation operators dotnet run -- db.max_connections=500 db.timeout_seconds+=15 active_workers+=4 debug!=true # Struct mode (safe add / upsert) dotnet run -- +db.pool_size=20 ++db.port=5433
Generate and run combinations across parameters:
dotnet run -- -m db=staging/postgres,production/postgres active_workers=2,4,8
Programmatic execution:
IReadOnlyList<AppConfig> configs = OmegaConfBuilder.Create<AppConfig>() .SetRoot("configs") .AddCliArgs(args) .BuildSweeps();
server: host: "10.0.0.1" port: 8080 # String interpolation api_url: "http://${server.host}:${server.port}/api" # Native type preservation (resolves to int 8080, not string) target_port: "${server.port}" # Fallback default syntax cache_ttl: "${cache.timeout:300}" # List element indexing servers: ["alpha.local", "beta.local"] primary_server: "${servers[0]}"
db: host: "db.internal" staging: port: 5433 # '.port' resolves to sibling (5433); '..host' resolves to parent db.host connection: "Server=${..host};Port=${.port}"
# Environment variables database_url: "${env:DATABASE_URL, postgresql://localhost:5432/dev}" # Math & boolean evaluation total_batch: "${eval: ${batch_size} * ${num_gpus}}" # Safe path selection & string decoding optional_port: "${oc.select:db.port, 5432}" decoded_port: "${oc.decode:${oc.env:PORT_STR, 8080}}" run_dir: "outputs/${now:yyyy-MM-dd}"
Custom resolver registration:
builder.RegisterResolver("slugify", raw => raw.ToLowerInvariant().Replace(' ', '-'));
using OmegaConf.NET.Diff; using OmegaConf.NET.Serialization; // Diff two configuration nodes ConfigDiff diff = ConfigDiffer.Compare(stagingNode, productionNode); Console.WriteLine(diff.ToString()); // Export back to YAML, JSON, or TOML string yaml = productionNode.ToYaml(); string json = productionNode.ToJson(indented: true); string toml = productionNode.ToToml();
Generate standard Draft-07 schemas for editor autocompletion:
using OmegaConf.NET.Schema; JsonSchemaGenerator.GenerateSchemaFile<AppConfig>("configs/config.schema.json");
Add to config.yaml for IDE validation:
# yaml-language-server: $schema=./config.schema.json app_name: "CatalogService"
using OmegaConf.NET.Extensions; var builder = WebApplication.CreateBuilder(args); // Register as IConfiguration provider builder.Configuration.AddOmegaConf(omega => { omega.SetRoot("configs"); omega.AddCliArgs(args); }); // Or register strongly typed options in DI builder.Services.ConfigureOmegaConf<AppConfig>(omega => { omega.SetRoot("configs"); omega.AddCliArgs(args); });
- QuickStart: Minimal runnable console application in
samples/QuickStart. - HydraExamples: Complete C# port of the official 34 Python Hydra examples in
samples/HydraExamples.
dotnet test -m:1- AGENTS.md: Architecture manual and multi-targeting directives for AI coding agents.
- SKILL.md: Authoritative agent skill guide with syntax tables, gotchas, and composition rules.
This project is licensed under the MIT License.