Zig2nix: 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.

9 Likes

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

2 Likes

Packaging a zig project that produces C static library and header