A simple Zig library for loading environment variables from .env files.
Fetch the package with Zig:
zig fetch --save git+https://github.com/dayvster/zdotenvThen add it as a module in your build.zig:
const dotenv_mod = b.addModule("dotenv", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});- Loads key-value pairs from one or more
.envfiles - Ignores comments and blank lines
- Provides easy access to environment variables via a hash map
- Memory-safe: all allocations are freed with
deinit
See examples/basic.zig:
const std = @import("std");
const Dotenv = @import("dotenv").Dotenv;
pub fn main() !void {
const allocator = std.heap.page_allocator;
var dotenv = Dotenv.init(allocator);
defer dotenv.deinit();
try dotenv.load(&[_][]const u8{"./examples/example.env"});
if (dotenv.get("FOO")) |val| {
std.debug.print("FOO={s}\n", .{val});
} else {
std.debug.print("FOO not found\n", .{});
}
}With an example.env file:
FOO=bar
BAZ=qux
Dotenv.init(allocator)— create a new instanceDotenv.load(paths)— load one or more.envfilesDotenv.get(key)— get the value for a keyDotenv.deinit()— free all memory
Run all tests:
zig build test