Skip to content

Commit

Permalink
add nix to runtime with build options instead of a wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
figsoda committed Aug 10, 2023
1 parent ebd997a commit ebf5059
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const nix = b.option([]const u8, "nix", "Path to the Nix executable") orelse "nix";

const options = b.addOptions();
options.addOption([]const u8, "nix", nix);

const exe = b.addExecutable(.{
.name = "zon2nix",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.addOptions("options", options);
exe.linkLibC();
b.installArtifact(exe);

Expand All @@ -25,6 +31,7 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
unit_tests.addOptions("options", options);
unit_tests.linkLibC();
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
Expand Down
15 changes: 8 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
perSystem = { lib, pkgs, ... }:
let
inherit (lib)
makeBinPath
getExe
;
inherit (pkgs)
makeBinaryWrapper
nix
stdenv
zigHook
Expand All @@ -37,16 +36,18 @@
src = ./.;

nativeBuildInputs = [
makeBinaryWrapper
(zigHook.override {
zig = zig_0_11;
})
];

postInstall = ''
wrapProgram $out/bin/zon2nix \
--prefix PATH : ${makeBinPath [ nix ]}
'';
zigBuildFlags = [
"-Dnix=${getExe nix}"
];

zigCheckFlags = [
"-Dnix=${getExe nix}"
];
};
};
};
Expand Down
4 changes: 3 additions & 1 deletion src/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const fs = std.fs;
const json = std.json;
const log = std.log;

const nix = @import("options").nix;

const Dependency = @import("Dependency.zig");
const parse = @import("parse.zig").parse;

Expand All @@ -34,7 +36,7 @@ pub fn fetch(alloc: Allocator, deps: *StringHashMap(Dependency)) !void {

var child = try alloc.create(ChildProcess);
const ref = try fmt.allocPrint(alloc, "tarball+{s}", .{dep.url});
const argv = &[_][]const u8{ "nix", "flake", "prefetch", "--json", "--extra-experimental-features", "flakes nix-command", ref };
const argv = &[_][]const u8{ nix, "flake", "prefetch", "--json", "--extra-experimental-features", "flakes nix-command", ref };
child.* = ChildProcess.init(argv, alloc);
child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe;
Expand Down

0 comments on commit ebf5059

Please sign in to comment.