Can I use packages inside build.zig?

I’ve done this.

When you import a dependency in a build.zig file, you will not import the dependency’s module (ie the root file, usually src/main.zig) but rather their build.zig.

For example this is how my static file generator works: in your website’s project you import zine and from that you will be able to call public functions from its build file:

My blog:

Zine’s implementation of addWebsite:

In another part of my static website generator I wanted to use a package at buildtime, so to make it work I just made sure to import in the dependency’s build.zig the decls that I wanted to use at build time:

This is the build.zig that belongs to the frontmatter package:

As you can see I’m importing into it decls from frontmatter.zig, and this is how I use the exposed symbols in another build script (it’s called markdown.zig but gets imported and used by Zine’s main build.zig script):

I asked Andrew if this was the intended way and he confirmed it was. The reason why the package is not normally fully available at build time is because any code that depends on build-time artifacts will not be usable, since while evaluating the build script the build has yet to happen. So only fully self-contained code can be used at build time.

5 Likes