Package Management

37.1 build.zig.zon

ZON (Zig Object Notation) is a subset of Zig syntax. std.zon can parse it at runtime too.

.{
    .name = .myapp,                                   // enum literal (0.14+)
    .version = "0.1.0",
    .fingerprint = 0x9b2c3d4e5f6a7b8c,                // unique package identity (generated by zig init)
    .minimum_zig_version = "0.15.1",
    .dependencies = .{
        .zap = .{
            .url = "git+https://github.com/zigzap/zap#v0.10.0",
            .hash = "zap-0.10.0-...",                 // content hash (filled in by zig fetch)
        },
        .local_lib = .{ .path = "../local_lib" },      // local dependency
        .optional_dep = .{ .url = "...", .hash = "...", .lazy = true },   // fetched only if used
    },
    .paths = .{ "build.zig", "build.zig.zon", "src", "LICENSE" },
}

37.2 Adding and Using Dependencies

Packages are cached globally by hash (reproducible, works offline once fetched). There's no central registry: you depend on URLs (Git, tarballs).

Discovery: zigistry.dev, awesome-zig, the allyourcodebase GitHub org (C libraries packaged with build.zig).

zig fetch --save git+https://github.com/karlseguin/http.zig#master
zig fetch --save=clap https://github.com/Hejsil/zig-clap/archive/refs/tags/0.10.0.tar.gz
// build.zig
const httpz = b.dependency("httpz", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("httpz", httpz.module("httpz"));
// a C library packaged for zig build:
const zlib = b.dependency("zlib", .{ .target = target, .optimize = optimize });
exe.root_module.linkLibrary(zlib.artifact("z"));

37.3 Publishing a Package

Expose modules with b.addModule("name", ...) and artifacts with b.installArtifact. Keep .paths minimal. Tag releases. Document the minimum Zig version.

Lazy dependencies (.lazy = true + b.lazyDependency) for optional features.