I started knowing build.zig.zon from the great post from @edyu on package management in Zig. Unfortunately, my work environment requires a http proxy to access network. And by the time the post is written, zig build
does not properly honor http_proxy environment variables. Thus, I have to manually download all dependencies since August 2023. (I also created a post to talk about it.)
Previously I can live with it. I just write some small executable in Zig without external dependencies. However, when I started my hobby project zamgba, it becomes a problem that must be solved.
It was painful to repeat the manual download + hash generation approach. The problem is, the hash required by build.zig.zon needs to be fetched when the first time I download the package. Unfortunately, I have to add a new library that does not have hash generated by other projects. It appears to be a chick-and-egg problem. Fortunately I found a good approach. that we can combine the git submodule
+ build.zig.zon
.
The idea is to use .dependencies.<package_name>.path
instead of .dependencies.<package_name>.url
to define the project. My example project, cosumezamgba, shows how I use it:
.{
.name = "consumezamgba",
.version = "0.0.0",
.minimum_zig_version = "0.12.0",
.dependencies = .{
.zamgba = . {
.path = "./deps/zamgba",
},
},
.paths = .{
"",
},
}
We can see the project depends on ./deps/zamgba
. This refers to my main project, https://github.com/fuzhouch/zamgba, referenced by git submodule
. When building the example project, consumezamgba, just run one more command, git submodule update --init --recursive
. It will do the trick.
An extra benefit is, when using .path
, we don't need the .hash
field at all. Thus it completely weave the needs of generating hash.
This is not a new idea. Actually it's borrowed from the C/C++ world. Many existing C/C++ projects still use git submodule
to manage dependencies. It works pretty well in any environment, with or without HTTP proxy.
Besides, this is also a good approach to reference to a project that haven't got any stable version released, especially when we need local test or hack my dependencies. I can pin to a specific commit of my dependency with git, without the needs of looking for "Download a Zip" link on Github.
Hope it helps.
Top comments (2)
@fuzhouch this doesn't work.
This is how build.zig looks like
Any help in debugging this is appreciated!
Make sure you have:
In your
deps\utils\build.zig
file.