Hello Everyone!
I’ve been working on a lightweight config library for Zig called zig-config. It is to made to handle configuration files in .env and .ini formats. It has features like variable substitution, section handling, merging, and serialization.
I am building this to learn and get into zig since I never really got into languages like rust or C but love the way zig types. So please give any feedback you got!
Key Features:
-
.envand.iniParsing: Supports comments, quoted values, empty lines, and section headers. -
Variable Substitution: Handles patterns like
${VAR},${VAR:-fallback},${VAR:+val}, and escaped variables like\$HOME. -
Typed Accessors: Has
getInt(),getFloat(),getBool()for type-safe retrieval. -
Section Handling: Go into specific sections using
getSection("section_name"). -
Merging Configs: Merge configs with strategies like
overwrite,skip_existing, orerror_on_conflict. -
Serialization: Write configs back to
.envor.inifiles. -
Error Handling: Comprehensive error types like
InvalidPlaceholderUnknownVariable, etc.
Usage Example:
Loading a .ini file:
; settings.ini
[database]
host=localhost
user=root
port=5432
const cfg = try Config.loadIniFile("settings.ini", allocator);
defer cfg.deinit();
const host = cfg.get("database.host") orelse "localhost";
const db = try cfg.getSection("database", allocator);
defer db.deinit();
const user = db.get("user") orelse "root";
Variable substitution:
HOST=localhost
PORT=8080
URL=http://${HOST}:${PORT}
FALLBACK=${NOT_SET:-default}
const url = try cfg.get("URL"); // http://localhost:8080
const fallback = try cfg.get("FALLBACK"); // "default"
Roadmap:
-
Integration with
std.process.getEnvMap()for process env loading. → just learned about this. -
Support for nested substitutions like
${A:-${B:-fallback}}. -
Enhanced error handling for circular references.
-
Generic accessor:
get(T: type, key, allocator) ?T. -
Fuzz testing for
.envand.iniparsing. -
Maybe: Add support for additional config formats (e.g. JSON, TOML?).
I am new to Zig and would greatly appreciate any feedback, suggestions, or contributions. Feel free to check out the repo here: GitHub - Niek-HM/zig-config: A Zig parser for .env, .ini, and .toml files with variable substitution, type-safe accessors, and merging support
Looking forward to your thoughts!