build.zig: detect build outside of Nix environment and provide warning

This commit is contained in:
Mitchell Hashimoto
2023-03-13 11:12:12 -07:00
parent d99cc0aec1
commit 8e8aabf4c0

View File

@ -69,6 +69,10 @@ pub fn build(b: *std.build.Builder) !void {
const wasm_target: WasmTarget = .browser; const wasm_target: WasmTarget = .browser;
// We use env vars throughout the build so we grab them immediately here.
var env = try std.process.getEnvMap(b.allocator);
defer env.deinit();
tracy = b.option( tracy = b.option(
bool, bool,
"tracy", "tracy",
@ -132,7 +136,6 @@ pub fn build(b: *std.build.Builder) !void {
// If we're in a nix shell we default to doing this. // If we're in a nix shell we default to doing this.
// Note: we purposely never deinit envmap because we leak the strings // Note: we purposely never deinit envmap because we leak the strings
const env = try std.process.getEnvMap(b.allocator);
if (env.get("IN_NIX_SHELL") == null) break :patch_rpath null; if (env.get("IN_NIX_SHELL") == null) break :patch_rpath null;
break :patch_rpath env.get("LD_LIBRARY_PATH"); break :patch_rpath env.get("LD_LIBRARY_PATH");
}; };
@ -210,6 +213,41 @@ pub fn build(b: *std.build.Builder) !void {
// Add the shared dependencies // Add the shared dependencies
_ = try addDeps(b, exe, static); _ = try addDeps(b, exe, static);
// If we're in NixOS but not in the shell environment then we issue
// a warning because the rpath may not be setup properly.
const is_nixos = is_nixos: {
if (!target.isLinux()) break :is_nixos false;
if (!target.isNativeCpu()) break :is_nixos false;
if (target.getOsTag() != builtin.os.tag) break :is_nixos false;
break :is_nixos if (std.fs.accessAbsolute("/etc/NIXOS", .{})) true else |_| false;
};
if (is_nixos and env.get("IN_NIX_SHELL") == null) {
const notice = b.addLog(
"\x1b[" ++ color_map.get("yellow").? ++
"\x1b[" ++ color_map.get("b").? ++
"WARNING: " ++
"\x1b[" ++ color_map.get("d").? ++
\\Detected building on and for NixOS outside of the Nix shell enviornment.
\\
\\The resulting ghostty binary will likely fail on launch because it is
\\unable to dynamically load the windowing libs (X11, Wayland, etc.).
\\We highly recommend running only within the Nix build environment
\\and the resulting binary will be portable across your system.
\\
\\To run in the Nix build environment, use the following command.
\\Append any additional options like (`-Doptimize` flags). The resulting
\\binary will be in zig-out as usual.
\\
\\ nix develop -c zig build
\\
++
"\x1b[0m",
.{},
);
exe.step.dependOn(&notice.step);
}
// If we're installing, we get the install step so we can add // If we're installing, we get the install step so we can add
// additional dependencies to it. // additional dependencies to it.
const install_step = if (app_runtime != .none) step: { const install_step = if (app_runtime != .none) step: {