1/velikoss/dotenv-zig v0.14
.env for Zig
dotenv zig 0.14.0
- dotenv is a super simple single file zig library for parsing a
.env
file. - implementation is < 100 lines
- Support for key-value pairs separated by
=
in the.env
file. - trims trailing spaces in key and values
- values in double or single quotes get stripped: "myvalue" -> myvalue
- does not check keys for syntax correctness
- dupes the key-values into a hashmap, so the input buffer may get deallocated without problems
- No dependencies
Usage
-
with zon: zig fetch --save "thisrepo/hash" in build.zig, add the module (the name of the module is "dotenv")
-
Create a
.env
file in your project or executable directory:# .env MY_ENV_VAR=hello ANOTHER_VAR=world
-
Use dotenv to read the environment variable:
const Env = @import("dotenv"); pub fn main() !void { const alloc = std.testing.allocator; // read the env file var file = try std.fs.cwd().openFile(".env", .{}); defer file.close(); const content = try file.readToEndAlloc(alloc, 1024 * 1024); defer alloc.free(content); // parse the env file var env: Env = try Env.init(alloc, content); defer env.deinit(); std.debug.print("{s}\n", .{env.get("password").?}); // Use the environment variables // ... }
-
Use dotenv to get a key from a dotenv file at comptime:
const Env = @import("dotenv"); pub fn main() !void { const content = @embedFile(".env"); try expect(try Env.parse_key("no key", content) == null); const password = try Env.parse_key("password", content); try expect(std.mem.eql(u8, password.?, "mysecretpassword")); }
Contributing
Contributions are welcome! Fork the repository and submit a pull request.
License
dotenv-zig is licensed under the MIT License. See LICENSE for details.
Package Contents
- licenses.txt
- zigmod.lock
- LICENSE
- build.zig
- zigmod.yml
- src/root.zig
- README.md
- build.zig.zon
- .gitignore
History
Published On | Tree @ Commit | Size | |
---|---|---|---|
v0.14 | Thu, 24 Apr 2025 21:04:26 UTC | Tree | 7.741 KB |
v0.13 | Tue, 08 Apr 2025 15:48:05 UTC | Tree | 6.544 KB |
v0.12 | Wed, 12 Jun 2024 15:34:02 UTC | Tree | 10.964 KB |
v0.11 | Wed, 12 Jun 2024 15:25:43 UTC | Tree | 10.912 KB |
v0.10 | Wed, 12 Jun 2024 15:22:48 UTC | Tree | 10.857 KB |
v0.9 | Wed, 12 Jun 2024 15:21:54 UTC | Tree | 10.875 KB |
v0.8 | Wed, 12 Jun 2024 14:37:08 UTC | Tree | 10.860 KB |
v0.7 | Wed, 12 Jun 2024 14:35:42 UTC | Tree | 10.813 KB |
v0.6 | Wed, 12 Jun 2024 14:33:30 UTC | Tree | 12.354 KB |
v0.5 | Wed, 12 Jun 2024 14:27:44 UTC | Tree | 12.329 KB |
v0.4 | Wed, 12 Jun 2024 14:27:32 UTC | Tree | 12.333 KB |
v0.3 | Wed, 12 Jun 2024 14:26:47 UTC | Tree | 12.337 KB |
v0.2 | Wed, 12 Jun 2024 09:33:53 UTC | Tree | 12.254 KB |
v0.1 | Wed, 12 Jun 2024 09:18:57 UTC | Tree | 12.152 KB |