zig: use setupHook attribute on zig derivation (#473413)

This commit is contained in:
Jared Baur
2026-01-09 22:16:27 +00:00
committed by GitHub
10 changed files with 162 additions and 162 deletions
+15 -11
View File
@@ -1,10 +1,10 @@
# zig.hook {#zig-hook}
# Zig {#zig}
[Zig](https://ziglang.org/) is a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software.
In Nixpkgs, `zig.hook` overrides the default build, check and install phases.
In Nixpkgs, `zig` overrides the default build, check and install phases.
## Example code snippet {#zig-hook-example-code-snippet}
## Example code snippet {#zig-example-code-snippet}
```nix
{
@@ -16,7 +16,7 @@ In Nixpkgs, `zig.hook` overrides the default build, check and install phases.
stdenv.mkDerivation {
# . . .
nativeBuildInputs = [ zig.hook ];
nativeBuildInputs = [ zig ];
zigBuildFlags = [ "-Dman-pages=true" ];
@@ -26,11 +26,11 @@ stdenv.mkDerivation {
}
```
## Variables controlling zig.hook {#zig-hook-variables-controlling}
## Variables controlling zig {#zig-variables-controlling}
### `zig.hook` Exclusive Variables {#zig-hook-exclusive-variables}
### `zig` Exclusive Variables {#zig-exclusive-variables}
The variables below are exclusive to `zig.hook`.
The variables below are exclusive to `zig`.
#### `dontUseZigBuild` {#dont-use-zig-build}
@@ -44,19 +44,23 @@ Disables using `zigCheckPhase`.
Disables using `zigInstallPhase`.
### Similar variables {#zig-hook-similar-variables}
#### `dontSetZigDefaultFlags` {#dont-set-zig-default-flags}
Disables using a set of default flags when performing zig builds.
### Similar variables {#zig-similar-variables}
The following variables are similar to their `stdenv.mkDerivation` counterparts.
| `zig.hook` Variable | `stdenv.mkDerivation` Counterpart |
| `zig` Variable | `stdenv.mkDerivation` Counterpart |
|---------------------|-----------------------------------|
| `zigBuildFlags` | `buildFlags` |
| `zigCheckFlags` | `checkFlags` |
| `zigInstallFlags` | `installFlags` |
### Variables honored by zig.hook {#zig-hook-variables-honored}
### Variables honored by zig {#zig-variables-honored}
The following variables commonly used by `stdenv.mkDerivation` are honored by `zig.hook`.
The following variables commonly used by `stdenv.mkDerivation` are honored by `zig`.
- `prefixKey`
- `dontAddPrefix`
+15 -6
View File
@@ -2713,16 +2713,20 @@
"waf-hook-honored-variables": [
"index.html#waf-hook-honored-variables"
],
"zig-hook": [
"zig": [
"index.html#zig",
"index.html#zig-hook"
],
"zig-hook-example-code-snippet": [
"zig-example-code-snippet": [
"index.html#zig-example-code-snippet",
"index.html#zig-hook-example-code-snippet"
],
"zig-hook-variables-controlling": [
"zig-variables-controlling": [
"index.html#zig-variables-controlling",
"index.html#zig-hook-variables-controlling"
],
"zig-hook-exclusive-variables": [
"zig-exclusive-variables": [
"index.html#zig-exclusive-variables",
"index.html#zig-hook-exclusive-variables"
],
"dont-use-zig-build": [
@@ -2734,10 +2738,15 @@
"dont-use-zig-install": [
"index.html#dont-use-zig-install"
],
"zig-hook-similar-variables": [
"dont-set-zig-default-flags": [
"index.html#dont-set-zig-default-flags"
],
"zig-similar-variables": [
"index.html#zig-similar-variables",
"index.html#zig-hook-similar-variables"
],
"zig-hook-variables-honored": [
"zig-variables-honored": [
"index.html#zig-variables-honored",
"index.html#zig-hook-variables-honored"
],
"xcbuildhook": [
+7 -8
View File
@@ -6,11 +6,6 @@
callPackage,
}:
let
zig_hook = zig_0_15.hook.overrideAttrs {
zig_default_flags = "-Dcpu=baseline -Doptimize=ReleaseSafe --color off";
};
in
stdenv.mkDerivation (finalAttrs: {
pname = "flow-control";
version = "0.6.0";
@@ -29,12 +24,16 @@ stdenv.mkDerivation (finalAttrs: {
} $ZIG_GLOBAL_CACHE_DIR/p
'';
nativeBuildInputs = [
zig_hook
];
nativeBuildInputs = [ zig_0_15 ];
passthru.updateScript = ./update.sh;
dontSetZigDefaultFlags = true;
zigBuildFlags = [
"-Dcpu=baseline"
"-Doptimize=ReleaseSafe"
];
env.VERSION = finalAttrs.version;
meta = {
+5 -7
View File
@@ -29,13 +29,7 @@
# https://github.com/ghostty-org/ghostty/blob/4b4d4062dfed7b37424c7210d1230242c709e990/PACKAGING.md#build-options
optimizeLevel ? "ReleaseFast",
}:
let
zig = zig_0_14;
zig_hook = zig.hook.overrideAttrs {
zig_default_flags = "-Dcpu=baseline -Doptimize=${optimizeLevel} --color off";
};
in
stdenv.mkDerivation (finalAttrs: {
pname = "ghostty";
version = "1.2.3";
@@ -66,7 +60,7 @@ stdenv.mkDerivation (finalAttrs: {
pandoc
pkg-config
removeReferencesTo
zig_hook
zig_0_14
# GTK frontend
glib # Required for `glib-compile-schemas`
@@ -93,10 +87,14 @@ stdenv.mkDerivation (finalAttrs: {
harfbuzz
];
dontSetZigDefaultFlags = true;
zigBuildFlags = [
"--system"
"${finalAttrs.deps}"
"-Dversion-string=${finalAttrs.version}"
"-Dcpu=baseline"
"-Doptimize=${optimizeLevel}"
]
++ lib.mapAttrsToList (name: package: "-fsys=${name} --search-prefix ${lib.getLib package}") {
inherit glslang;
+4 -3
View File
@@ -31,6 +31,10 @@ rustPlatform.buildRustPackage {
hash = "sha256-pPnOM4hpbAkGCV47aw5eHbpOujjFtJa3v/3/D8gybO8=";
};
dontUseZigBuild = true;
dontUseZigCheck = true;
dontUseZigInstall = true;
nativeBuildInputs = [
cmake
zig_0_13
@@ -53,9 +57,6 @@ rustPlatform.buildRustPackage {
cargoHash = "sha256-wJViSHcezoIchWe4Py9j+9U+YJUA5ja/x94UipuWO2g=";
# prevents zig AccessDenied error github.com/ziglang/zig/issues/6810
XDG_CACHE_HOME = "xdg_cache";
preBuild =
let
llvmVersion = builtins.splitVersion llvmPackages.release_version;
+5 -7
View File
@@ -8,12 +8,6 @@
callPackage,
}:
let
zig = zig_0_15;
zig_hook = zig.hook.overrideAttrs {
zig_default_flags = "-Dcpu=baseline -Doptimize=ReleaseFast --color off";
};
in
stdenv.mkDerivation (finalAttrs: {
pname = "zf";
upstreamVersion = "0.10.3";
@@ -29,16 +23,20 @@ stdenv.mkDerivation (finalAttrs: {
nativeBuildInputs = [
installShellFiles
zig_hook
zig_0_15
];
deps = callPackage ./deps.nix {
name = "${finalAttrs.pname}-cache-${finalAttrs.version}";
};
dontSetZigDefaultFlags = true;
zigBuildFlags = [
"--system"
"${finalAttrs.deps}"
"-Dcpu=baseline"
"-Doptimize=ReleaseFast"
];
postInstall = ''
+37 -3
View File
@@ -5,7 +5,6 @@
cmake,
llvmPackages,
xcbuild,
targetPackages,
libxml2,
ninja,
zlib,
@@ -126,17 +125,52 @@ stdenv.mkDerivation (finalAttrs: {
passthru = import ./passthru.nix {
inherit
lib
stdenv
callPackage
wrapCCWith
wrapBintoolsWith
overrideCC
targetPackages
;
zig = finalAttrs.finalPackage;
};
env = {
# This zig_default_optimize_flag below is meant to avoid CPU feature impurity in
# Nixpkgs. However, this flagset is "unstable": it is specifically meant to
# be controlled by the upstream development team - being up to that team
# exposing or not that flags to the outside (especially the package manager
# teams).
# Because of this hurdle, @andrewrk from Zig Software Foundation proposed
# some solutions for this issue. Hopefully they will be implemented in
# future releases of Zig. When this happens, this flagset should be
# revisited accordingly.
# Below are some useful links describing the discovery process of this 'bug'
# in Nixpkgs:
# https://github.com/NixOS/nixpkgs/issues/169461
# https://github.com/NixOS/nixpkgs/issues/185644
# https://github.com/NixOS/nixpkgs/pull/197046
# https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485
# https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653
zig_default_cpu_flag = "-Dcpu=baseline";
zig_default_optimize_flag =
if lib.versionAtLeast finalAttrs.version "0.12" then
"--release=safe"
else if lib.versionAtLeast finalAttrs.version "0.11" then
"-Doptimize=ReleaseSafe"
else
"-Drelease-safe=true";
};
setupHook = ./setup-hook.sh;
# while xcrun is already included in the darwin stdenv, Zig also needs
# xcode-select (provided by xcbuild) for SDK detection
propagatedNativeBuildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild ];
meta = {
description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
homepage = "https://ziglang.org/";
-60
View File
@@ -1,60 +0,0 @@
{
lib,
makeSetupHook,
zig,
stdenv,
xcbuild,
globalBuildFlags ? [ "-Dcpu=baseline" ],
}:
makeSetupHook {
name = "zig-hook";
propagatedBuildInputs = [
zig
]
# while xcrun is already included in the darwin stdenv, Zig also needs
# xcode-select (provided by xcbuild) for SDK detection
++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild ];
substitutions = {
# This zig_default_flags below is meant to avoid CPU feature impurity in
# Nixpkgs. However, this flagset is "unstable": it is specifically meant to
# be controlled by the upstream development team - being up to that team
# exposing or not that flags to the outside (especially the package manager
# teams).
# Because of this hurdle, @andrewrk from Zig Software Foundation proposed
# some solutions for this issue. Hopefully they will be implemented in
# future releases of Zig. When this happens, this flagset should be
# revisited accordingly.
# Below are some useful links describing the discovery process of this 'bug'
# in Nixpkgs:
# https://github.com/NixOS/nixpkgs/issues/169461
# https://github.com/NixOS/nixpkgs/issues/185644
# https://github.com/NixOS/nixpkgs/pull/197046
# https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485
# https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653
zig_default_flags =
let
releaseType =
if lib.versionAtLeast zig.version "0.12" then
"--release=safe"
else if lib.versionAtLeast zig.version "0.11" then
"-Doptimize=ReleaseSafe"
else
"-Drelease-safe=true";
in
globalBuildFlags ++ [ releaseType ];
};
passthru = { inherit zig; };
meta = {
description = "Setup hook for using the Zig compiler in Nixpkgs";
inherit (zig.meta) maintainers platforms broken;
};
} ./setup-hook.sh
+3 -3
View File
@@ -1,15 +1,15 @@
{
lib,
stdenv,
zig,
callPackage,
wrapCCWith,
wrapBintoolsWith,
overrideCC,
targetPackages,
}:
{
hook = callPackage ./hook.nix { inherit zig; };
# Provided for backward compatibility, as the `zig` derivation now sets
# setupHook.
hook = zig;
bintools-unwrapped = callPackage ./bintools.nix { inherit zig; };
bintools = wrapBintoolsWith { bintools = zig.bintools-unwrapped; };
+71 -54
View File
@@ -1,96 +1,113 @@
# shellcheck shell=bash
# shellcheck disable=SC2034
readonly zigDefaultFlagsArray=(@zig_default_flags@)
readonly zigDefaultCpuFlag=@zig_default_cpu_flag@
readonly zigDefaultOptimizeFlag=@zig_default_optimize_flag@
function zigSetGlobalCacheDir {
ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
export ZIG_GLOBAL_CACHE_DIR
ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
export ZIG_GLOBAL_CACHE_DIR
}
function zigBuildPhase {
runHook preBuild
runHook preBuild
local buildCores=1
local buildCores=1
# Parallel building is enabled by default.
if [ "${enableParallelBuilding-1}" ]; then
buildCores="$NIX_BUILD_CORES"
fi
# Parallel building is enabled by default.
if [ "${enableParallelBuilding-1}" ]; then
buildCores="$NIX_BUILD_CORES"
fi
local flagsArray=(
"-j$buildCores"
)
concatTo flagsArray zigDefaultFlagsArray \
zigBuildFlags zigBuildFlagsArray
local flagsArray=(
"-j$buildCores"
)
concatTo flagsArray \
zigBuildFlags zigBuildFlagsArray
echoCmd 'zig build flags' "${flagsArray[@]}"
TERM=dumb zig build "${flagsArray[@]}" --verbose
if [ -z "${dontSetZigDefaultFlags:-}" ]; then
concatTo flagsArray \
zigDefaultCpuFlag zigDefaultOptimizeFlag
fi
runHook postBuild
echoCmd 'zig build flags' "${flagsArray[@]}"
TERM=dumb zig build "${flagsArray[@]}" --verbose
runHook postBuild
}
function zigCheckPhase {
runHook preCheck
runHook preCheck
local buildCores=1
local buildCores=1
# Parallel building is enabled by default.
if [ "${enableParallelChecking-1}" ]; then
buildCores="$NIX_BUILD_CORES"
fi
# Parallel building is enabled by default.
if [ "${enableParallelChecking-1}" ]; then
buildCores="$NIX_BUILD_CORES"
fi
local flagsArray=(
"-j$buildCores"
)
concatTo flagsArray zigDefaultFlagsArray \
zigCheckFlags zigCheckFlagsArray
local flagsArray=(
"-j$buildCores"
)
concatTo flagsArray \
zigCheckFlags zigCheckFlagsArray
echoCmd 'zig check flags' "${flagsArray[@]}"
TERM=dumb zig build test "${flagsArray[@]}" --verbose
if [ -z "${dontSetZigDefaultFlags:-}" ]; then
concatTo flagsArray \
zigDefaultCpuFlag zigDefaultOptimizeFlag
fi
runHook postCheck
echoCmd 'zig check flags' "${flagsArray[@]}"
TERM=dumb zig build test "${flagsArray[@]}" --verbose
runHook postCheck
}
function zigInstallPhase {
runHook preInstall
runHook preInstall
local buildCores=1
local buildCores=1
# Parallel building is enabled by default.
if [ "${enableParallelInstalling-1}" ]; then
buildCores="$NIX_BUILD_CORES"
fi
# Parallel building is enabled by default.
if [ "${enableParallelInstalling-1}" ]; then
buildCores="$NIX_BUILD_CORES"
fi
local flagsArray=(
"-j$buildCores"
)
concatTo flagsArray zigDefaultFlagsArray \
zigBuildFlags zigBuildFlagsArray \
zigInstallFlags zigInstallFlagsArray
local flagsArray=(
"-j$buildCores"
)
if [ -z "${dontAddPrefix-}" ]; then
# Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/`
flagsArray+=("${prefixKey:---prefix}" "$prefix")
fi
concatTo flagsArray \
zigBuildFlags zigBuildFlagsArray \
zigInstallFlags zigInstallFlagsArray
echoCmd 'zig install flags' "${flagsArray[@]}"
TERM=dumb zig build install "${flagsArray[@]}" --verbose
if [ -z "${dontSetZigDefaultFlags:-}" ]; then
concatTo flagsArray \
zigDefaultCpuFlag zigDefaultOptimizeFlag
fi
runHook postInstall
if [ -z "${dontAddPrefix-}" ] && [ -n "$prefix" ]; then
# Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/`
flagsArray+=("${prefixKey:---prefix}" "$prefix")
fi
echoCmd 'zig install flags' "${flagsArray[@]}"
TERM=dumb zig build install "${flagsArray[@]}" --verbose
runHook postInstall
}
# shellcheck disable=SC2154
addEnvHooks "$targetOffset" zigSetGlobalCacheDir
addEnvHooks "$hostOffset" zigSetGlobalCacheDir
if [ -z "${dontUseZigBuild-}" ] && [ -z "${buildPhase-}" ]; then
buildPhase=zigBuildPhase
buildPhase=zigBuildPhase
fi
if [ -z "${dontUseZigCheck-}" ] && [ -z "${checkPhase-}" ]; then
checkPhase=zigCheckPhase
checkPhase=zigCheckPhase
fi
if [ -z "${dontUseZigInstall-}" ] && [ -z "${installPhase-}" ]; then
installPhase=zigInstallPhase
installPhase=zigInstallPhase
fi