Something went wrong. Try again.
Converts the dependencies in a Zig build.zig.zon into a Nix expression, so Zig projects build under Nix without network access.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134// SPDX-FileCopyrightText: 2025 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const nix_prefetch_git = b.option([]const u8, "nix-prefetch-git", "Path to nix-prefetch-git") orelse "nix-prefetch-git"; const nix_prefetch_url = b.option([]const u8, "nix-prefetch-url", "Path to nix-prefetch-url") orelse "nix-prefetch-url"; const nixfmt = b.option([]const u8, "nixfmt", "Path to nixfmt") orelse "nixfmt";
const options = b.addOptions(); options.addOption([]const u8, "nix_prefetch_git", nix_prefetch_git); options.addOption([]const u8, "nix_prefetch_url", nix_prefetch_url); options.addOption([]const u8, "nixfmt", nixfmt);
const root_mod = b.addModule( "zon2nix", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }, );
const main_mod = b.createModule( .{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }, );
const run_step = b.step("run", "Run the app"); const test_step = b.step("test", "Run unit tests"); const test_valgrind_step = b.step("test-valgrind", "Run tests under valgrind"); const run_valgrind_step = b.step("run-valgrind", "Run executable under valgrind");
{ const exe = b.addExecutable(.{ .name = "zon2nix", .root_module = main_mod, }); exe.root_module.addImport("zon2nix", root_mod); exe.root_module.addOptions("options", options);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); run_step.dependOn(&run_cmd.step); }
{ const root_exe = b.addTest(.{ .root_module = root_mod, }); const run_root_cmd = b.addRunArtifact(root_exe);
// `main_mod`, not `root_mod`. Both of these named the root module, // so the tests in `src/main.zig` were compiled once as part of the // library and never run at all. const main_exe = b.addTest(.{ .root_module = main_mod, }); main_exe.root_module.addImport("zon2nix", root_mod); main_exe.root_module.addOptions("options", options); const run_main_cmd = b.addRunArtifact(main_exe); test_step.dependOn(&run_root_cmd.step); test_step.dependOn(&run_main_cmd.step); }
{ const mod = b.createModule( .{ .root_source_file = b.path("src/root.zig"), .target = baseline(b.graph.io, target), .optimize = optimize, }, ); const exe = b.addTest(.{ .root_module = mod, }); test_step.dependOn(&exe.step);
const run = b.addSystemCommand(&.{ "valgrind", "--leak-check=full", "--num-callers=50", "--gen-suppressions=all", }); run.addArtifactArg(exe); test_valgrind_step.dependOn(&run.step); }
{ const mod = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = baseline(b.graph.io, target), .optimize = optimize, }); const exe = b.addExecutable(.{ .name = "zon2nix", .root_module = mod, }); exe.root_module.addImport("zon2nix", root_mod); exe.root_module.addOptions("options", options); const run_cmd = b.addSystemCommand(&.{ "valgrind", "--leak-check=full", "--num-callers=50", "--gen-suppressions=all", }); run_cmd.addArtifactArg(exe); run_cmd.addPassthruArgs();
run_valgrind_step.dependOn(&run_cmd.step); }}
fn baseline(io: std.Io, target: std.Build.ResolvedTarget) std.Build.ResolvedTarget { var query = target.query; query.cpu_model = .baseline;
return .{ .query = query, .result = std.zig.system.resolveTargetQuery(io, query) catch @panic("unable to resolve baseline query"), };}