vmintf/SimpleEnvs
A zig language for environment library.
A lightweight, secure, and idiomatic Zig library for loading and parsing .env
files, inspired by python-dotenv
. It provides a simple API to load environment variables into a process-local EnvMap
, with support for strings, numbers, and booleans. Designed with Zig's philosophy of explicitness, conciseness, and safety, it ensures robust security and memory management.
.env
Loading: Scans the current directory and up to 2 subdirectories for .env
files with loadAuto()
.i64
), and booleans using a Value
union.get
and getWithDefault
for safe access without forced unwrapping...
blocked).no_follow
).std.heap.GeneralPurposeAllocator
(GPA) for leak detection.deinit()
.Clone or copy the library to your project:
git clone https://github.com/vmintf/SimpleEnvs.git
Add it to your build.zig
:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "my-app",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Add SimpleEnvs as a module
const dotenv_module = b.addModule("env", .{
.root_source_file = b.path("src/root.zig"),
});
exe.root_module.addImport("env", dotenv_module);
b.installArtifact(exe);
}
Import and use in your code:
const env = @import("env");
Load a .env
file and read values:
const env = @import("env");
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
var env_map = try env.loadAuto(allocator);
defer env.deinit(&env_map); // Always call to free memory
if (env.get(env_map, "DB_HOST")) |host| {
std.debug.print("DB_HOST: {}\n", .{host}); // Outputs: DB_HOST: localhost
}
const port = env.getWithDefault(env_map, "DB_PORT", env.Value{ .Number = 8080 });
std.debug.print("DB_PORT: {}\n", .{port}); // Outputs: DB_PORT: 5432
}
Example .env
file:
DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DEBUG=true
Load a specific .env
file with custom options:
const env = @import("env");
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
var env_map = try env.load(allocator, .{
.path = "./config/.env",
});
defer env.deinit(&env_map);
if (env.get(env_map, "API_KEY")) |key| {
std.debug.print("API_KEY: {}\n", .{key});
}
}
env.deinit(&env_map)
to free memory. Use GeneralPurposeAllocator
to detect leaks.EnvMap
values externally.Run the tests to verify functionality:
zig test src/root.zig
Tests cover:
.env
file scanning and loading...
in paths to prevent unauthorized access..env
files are valid UTF-8 to avoid encoding issues.no_follow
to avoid following links.deinit()
and GPA for leak detection..env
file support with Diffie-Hellman key exchange and AES-256-GCM.We welcome contributions! To contribute:
git checkout -b feature/my-feature
).git commit -m "Add my feature"
).git push origin feature/my-feature
).Please include tests for new features and follow Zig's coding style.
MIT License. See LICENSE for details.
Inspired by python-dotenv
and designed for Zig's explicit and secure philosophy.