Zig2nix: nix flake for packaging, building and running Zig projects

Hello I created flake for bridging zig to nix.

Features:

  • automatically updated zig compiler builds
  • .zon file conversion to .json format (zon2json binary, fromZON function)
  • build.zig.zon conversion to a lock file describing all zig package manager artifacts and hashes recursively (zon2json-lock binary)
  • lock file conversion to nix derivation (zon2nix binary, deriveLockFile function). Note the zon2nix binary is not the same as nix-community/zon2nix.
  • provides zig-env that can setup a common runtime for you (x11, wayland, opengl, vulkan) where programs dlopen these libraries
  • provides packageForTarget and package functions that fully integrate with build.zig.zon projects. By default they produce binaries compatible with nix environment, but can be used to build for foreign environments as well.
  • tool for dumping external dependencies of a zig project (system libraries, frameworks) nix run github:Cloudef/zig2nix#deps.master
  • zig2nix project templates
  • and maybe more …

Eventually I plan to merge/rewrite the GitHub - Cloudef/nix-zig-stdenv: cross-compile nixpkgs with zig into this project as well, allowing cross-compiling nixpkgs using zig.

A child project also exists called mach-flake, that flakes the Mach Engine https://machengine.org/.
GitHub - Cloudef/mach-flake: Flake that allows you to get started with Mach engine quickly.

7 Likes

Cross compiling and using nixpkgs from binary cache for target is now supported. All flake platforms are tested. At some point I may add support for cross-compiling nixpkgs using zig itself (tracking issue https://github.com/Cloudef/zig2nix/issues/4)

1 Like

I added ability to print external dependencies of a zig project (system libs and frameworks). Zig does not have separation of system/external and local for frameworks, so it may show frameworks that aren’t external. However it’s still good tool if nixing random zig projects.

Introducing experimental feature that allows you to (cross)compile nixpkgs using zig!
nix build github:Cloudef/zig2nix#zigCross.x86_64-windows-gnu.zlib

Note that compiling to macos has some problems right now Cross-compiling to macos or building inside nix sandbox fails to find headers from -isystem path · Issue #18571 · ziglang/zig · GitHub