zig: use setupHook attribute on zig derivation

By moving the zig setup hook to the zig derivation itself, we allow for
zig to splice correctly with `callPackage`, meaning that the correct zig
can be pulled in during builds when zig is in `nativeBuildInputs` (for
example).

This change retains the `zig.hook` attribute for backward compatibility
by just pointing to the zig derivation. This also removes
`zig_default_flags`, since now the setup hook is not a derivation that
can be overridden. Overriding the build flags can now be done by setting
`dontSetZigDefaultFlags = true`.
This commit is contained in:
Jared Baur
2026-01-09 14:07:46 -08:00
parent 45a1530683
commit 1dfa285940
6 changed files with 141 additions and 137 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": [
+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