diff --git a/doc/hooks/zig.section.md b/doc/hooks/zig.section.md index 58515087c59a1..4bd65746e0ddb 100644 --- a/doc/hooks/zig.section.md +++ b/doc/hooks/zig.section.md @@ -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` diff --git a/doc/redirects.json b/doc/redirects.json index bc0bf55213846..d52ae8a8f0478 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -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": [ diff --git a/pkgs/by-name/fl/flow-control/package.nix b/pkgs/by-name/fl/flow-control/package.nix index 838dd3d628472..159ad3f3a4ca2 100644 --- a/pkgs/by-name/fl/flow-control/package.nix +++ b/pkgs/by-name/fl/flow-control/package.nix @@ -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 = { diff --git a/pkgs/by-name/gh/ghostty/package.nix b/pkgs/by-name/gh/ghostty/package.nix index 7c14a55ba528b..b08023c70e1cd 100644 --- a/pkgs/by-name/gh/ghostty/package.nix +++ b/pkgs/by-name/gh/ghostty/package.nix @@ -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; diff --git a/pkgs/by-name/ro/roc/package.nix b/pkgs/by-name/ro/roc/package.nix index 0081612abc779..687a63744e9c3 100755 --- a/pkgs/by-name/ro/roc/package.nix +++ b/pkgs/by-name/ro/roc/package.nix @@ -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; diff --git a/pkgs/by-name/zf/zf/package.nix b/pkgs/by-name/zf/zf/package.nix index 1f3cea51152da..1090bc7267c19 100644 --- a/pkgs/by-name/zf/zf/package.nix +++ b/pkgs/by-name/zf/zf/package.nix @@ -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 = '' diff --git a/pkgs/development/compilers/zig/generic.nix b/pkgs/development/compilers/zig/generic.nix index 3e9d5143824ba..98a331a32fbd4 100644 --- a/pkgs/development/compilers/zig/generic.nix +++ b/pkgs/development/compilers/zig/generic.nix @@ -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/"; diff --git a/pkgs/development/compilers/zig/hook.nix b/pkgs/development/compilers/zig/hook.nix deleted file mode 100644 index 2620414b6a2ed..0000000000000 --- a/pkgs/development/compilers/zig/hook.nix +++ /dev/null @@ -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 diff --git a/pkgs/development/compilers/zig/passthru.nix b/pkgs/development/compilers/zig/passthru.nix index 439df42d31d51..dbe371008f485 100644 --- a/pkgs/development/compilers/zig/passthru.nix +++ b/pkgs/development/compilers/zig/passthru.nix @@ -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; }; diff --git a/pkgs/development/compilers/zig/setup-hook.sh b/pkgs/development/compilers/zig/setup-hook.sh index 1f6d8567838ae..bbd322c5641cf 100644 --- a/pkgs/development/compilers/zig/setup-hook.sh +++ b/pkgs/development/compilers/zig/setup-hook.sh @@ -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 \ + zigCheckFlags zigCheckFlagsArray - local flagsArray=( - "-j$buildCores" - ) - concatTo flagsArray zigDefaultFlagsArray \ - zigCheckFlags zigCheckFlagsArray + if [ -z "${dontSetZigDefaultFlags:-}" ]; then + concatTo flagsArray \ + zigDefaultCpuFlag zigDefaultOptimizeFlag + fi - echoCmd 'zig check flags' "${flagsArray[@]}" - TERM=dumb zig build test "${flagsArray[@]}" --verbose + echoCmd 'zig check flags' "${flagsArray[@]}" + TERM=dumb zig build test "${flagsArray[@]}" --verbose - runHook postCheck + runHook postCheck } function zigInstallPhase { - runHook preInstall + runHook preInstall + + local buildCores=1 + + # Parallel building is enabled by default. + if [ "${enableParallelInstalling-1}" ]; then + buildCores="$NIX_BUILD_CORES" + fi - local buildCores=1 + local flagsArray=( + "-j$buildCores" + ) - # Parallel building is enabled by default. - if [ "${enableParallelInstalling-1}" ]; then - buildCores="$NIX_BUILD_CORES" - fi + concatTo flagsArray \ + zigBuildFlags zigBuildFlagsArray \ + zigInstallFlags zigInstallFlagsArray - local flagsArray=( - "-j$buildCores" - ) - concatTo flagsArray zigDefaultFlagsArray \ - zigBuildFlags zigBuildFlagsArray \ - zigInstallFlags zigInstallFlagsArray + if [ -z "${dontSetZigDefaultFlags:-}" ]; then + concatTo flagsArray \ + zigDefaultCpuFlag zigDefaultOptimizeFlag + fi - if [ -z "${dontAddPrefix-}" ]; then - # Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/` - flagsArray+=("${prefixKey:---prefix}" "$prefix") - fi + 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 + echoCmd 'zig install flags' "${flagsArray[@]}" + TERM=dumb zig build install "${flagsArray[@]}" --verbose - runHook postInstall + 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