Unverified Commit fb8d932c authored by Leah Amelia Chen's avatar Leah Amelia Chen
Browse files

ghostty: limit the amount of build cores

For some bizarre reason Zig contains a bug where the compiler would crash
when a large enough file is embedded AND when too many cores are used
during compilation. This has been causing Hydra builds to repeatedly
fail, while Ofborg and manual testing by maintainers were never able to
reproduce the failure. However, given this is preventing Ghostty 1.2 from
landing in the binary cache, we need to work around it somehow. 24 cores
should be *more* than enough for now, but if it still doesn't work we can
lower the threshold even further.

Links to upstream discussions:
 * https://github.com/ziglang/zig/issues/25297
 * https://github.com/ziglang/zig/issues/22867
 * https://github.com/ghostty-org/ghostty/discussions/8676

Fixes #445326
parent f4f97de6
Loading
Loading
Loading
Loading
+41 −2
Original line number Diff line number Diff line
@@ -20,8 +20,10 @@
  pandoc,
  pkg-config,
  removeReferencesTo,
  util-linux,
  versionCheckHook,
  wrapGAppsHook4,
  writeShellApplication,
  zig_0_14,

  # Usually you would override `zig.hook` with this, but we do that internally
@@ -31,7 +33,44 @@
}:
let
  zig = zig_0_14;
  zig_hook = zig.hook.overrideAttrs {

  # HACK:
  # Work around a Zig bug where embedding a large enough file could crash
  # the compiler when too many cores are used, which causes Hydra builds to
  # reliably fail. See these links for more info:
  #
  #  * https://github.com/ziglang/zig/issues/25297
  #  * https://github.com/ziglang/zig/issues/22867
  #  * https://github.com/ghostty-org/ghostty/discussions/8676
  #
  # Note that the `-j` parameter does NOT fix this. It seems like the faulty
  # intern pool logic always depends on the full amount of available cores
  # instead of the value of `-j`, so we have to use `taskset` to trick Zig
  # into thinking it only has access to a limited amount of cores.
  zigWithLimitedCores = writeShellApplication {
    name = "zig";
    passthru = {
      inherit (zig) version meta;
    };
    runtimeInputs = [
      zig
      util-linux
    ];
    text = ''
      maxCores=$(nproc)
      # 32 cores seem to be the upper limit through empiric testing
      coreLimit=$((maxCores < 32 ? maxCores : 32))
      # Also take NIX_BUILD_CORES into account so the build respects the `--cores` argument
      effectiveCores=$((NIX_BUILD_CORES > coreLimit ? coreLimit : NIX_BUILD_CORES))
      taskset -c "0-$((effectiveCores - 1))" zig "$@"
    '';
  };

  zig_hook =
    (zig.hook.override {
      zig = zigWithLimitedCores;
    }).overrideAttrs
      {
        zig_default_flags = "-Dcpu=baseline -Doptimize=${optimizeLevel} --color off";
      };
in