Skip to content

Commit

Permalink
Make zig happy with /usr/bin/env
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Feb 15, 2025
1 parent 27ba0b2 commit 3c843d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
5 changes: 4 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const TBOOT_INITRD_NAME = "tboot-initrd";
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(
.{
.default_target = .{ .cpu_model = .baseline, .abi = .musl },
.default_target = .{
.cpu_model = .baseline,
.abi = .musl,
},
},
);

Expand Down
31 changes: 14 additions & 17 deletions pkgs/tinyboot/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
withTools,
zigForTinyboot,

bubblewrap,
callPackage,
lib,
openssl,
Expand All @@ -20,9 +21,12 @@ let
"musl" = "musl";
}
.${stdenv.hostPlatform.libc} or "none";

# Using zig-overlay (without the patches from nixpkgs) does not work well when
# doing sandboxed builds because of the following issue: https://github.com/ziglang/zig/issues/15898.
# Providing a /usr/bin/env for zig fixes some issues.
bwrap = "bwrap --ro-bind $(command -v env) /usr/bin/env --bind /nix/store /nix/store --bind /build /build --proc /proc --dev /dev";
in
# Using zig-overlay (without the patches from nixpkgs) does not work well when
# doing sandboxed builds because of the following issue: https://github.com/ziglang/zig/issues/15898
assert stdenv.hostPlatform.isStatic && stdenv.hostPlatform.libc == "musl";
assert withTools != withLoader;
stdenv.mkDerivation {
Expand All @@ -43,6 +47,7 @@ stdenv.mkDerivation {

nativeBuildInputs = [
pkg-config
bubblewrap
zigForTinyboot
] ++ lib.optional (!withTools) tinybootTools;

Expand All @@ -51,38 +56,30 @@ stdenv.mkDerivation {
++ lib.optional (withTools || stdenv.buildPlatform.canExecute stdenv.hostPlatform) xz # tboot-initrd
;

zigBuildFlags =
[
"--release=safe"
"-Dcpu=baseline"
"-Dtarget=${stdenv.hostPlatform.qemuArch}-${stdenv.hostPlatform.parsed.kernel.name}-${zigLibc}"
"-Ddynamic-linker=${stdenv.cc.bintools.dynamicLinker}"
"-Dloader=${lib.boolToString withLoader}"
"-Dtools=${lib.boolToString withTools}"
]
++ lib.optionals (firmwareDirectory != null) [
"-Dfirmware-directory=${firmwareDirectory}"
];

dontInstall = true;
doCheck = true;

configurePhase = ''
runHook preConfigure
export ZIG_GLOBAL_CACHE_DIR=$TEMPDIR
ln -s ${callPackage ../../build.zig.zon.nix { }} $ZIG_GLOBAL_CACHE_DIR/p
zigBuildFlags=("-Dloader=${lib.boolToString withLoader}" "-Dtools=${lib.boolToString withTools}" "--release=safe" "-Dtarget=${stdenv.hostPlatform.qemuArch}-${stdenv.hostPlatform.parsed.kernel.name}-${zigLibc}")
zigBuildFlags+=("-Ddynamic-linker=$(echo ${stdenv.cc.bintools.dynamicLinker})") # can contain a glob
${lib.optionalString (firmwareDirectory != null) ''
zigBuildFlags+=("-Dfirmware-directory=${firmwareDirectory}")
''}
runHook postConfigure
'';

buildPhase = ''
runHook preBuild
zig build install --prefix $out $zigBuildFlags
${bwrap} zig build install --prefix $out ''${zigBuildFlags[@]}
runHook postBuild
'';

checkPhase = ''
runHook preCheck
zig build test $zigBuildFlags
${bwrap} zig build test ''${zigBuildFlags[@]}
runHook postCheck
'';

Expand Down

0 comments on commit 3c843d0

Please sign in to comment.