zig-dotenv
I just published a lightweight .env
file parser for Zig! It’s designed to be simple, fast, and dependency-free.
What it does
- Parses
.env
files into a key-value map - Handles comments (
# comment
) - Supports quoted values (
"hello world"
) - Trims whitespace automatically
- Zero external dependencies
Key features
- Tiny: Under 100 lines of pure Zig
- Fast: Minimal allocations and overhead
- Simple API:
loadFile()
andget()
- that’s it - Memory safe: Proper allocation and cleanup
Example usage
const std = @import("std");
const dotenv = @import("dotenv");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var env = try dotenv.DotEnv.loadFile(allocator, ".env");
defer env.deinit();
const db_url = env.get("DATABASE_URL") orelse "default";
std.debug.print("Database: {s}\n", .{db_url});
}
Example .env file
# Database configuration
DATABASE_URL=postgres://localhost:5432/myapp
API_KEY=your_secret_key_here
# Feature flags
DEBUG=true
WELCOME_MESSAGE="Hello, World!"
Repository
The library is MIT licensed and ready to use. Feedback and contributions welcome!
Would love to hear what you think or if you find it useful in your projects.