Skip to content

Serialization ​

env.zig can serialize configurations back to .env format.

Basic Serialization ​

zig
const output = try env.serialize();
defer allocator.free(output);
// output: "KEY=value\nOTHER=foo\n"

Save to File ​

zig
try env.save("output.env");

Serialization Options ​

Config OptionDefaultDescription
sortKeysfalseSort keys alphabetically (input slice is not mutated)
quoteSpacestrueQuote values needing quotes for a lossless parse(serialize(x)) round-trip
trailingNewlinetrueEmit trailing \n after the last entry; false omits it

Sorted Keys ​

zig
var env = envMod.Env.init(allocator, .{ .sortKeys = true });
// Keys will be sorted alphabetically in output

Custom Quoting ​

Values with spaces, quotes, or special characters are automatically escaped:

zig
try env.set("MESSAGE", "hello world");
const output = try env.serialize();
// MESSAGE="hello world"

Example Output ​

env
DATABASE_HOST=localhost
DATABASE_NAME=myapp
DATABASE_PORT=5432
LOG_LEVEL=info

Released under the MIT License.