about summary refs log tree commit diff
path: root/pkgs
diff options
context:
space:
mode:
authorAnderson Torres <torres.anderson.85@protonmail.com>2023-07-15 00:57:49 -0300
committerAnderson Torres <torres.anderson.85@protonmail.com>2023-08-01 10:08:55 +0000
commita85a699e40708b452d36c70725396ff67dc48e66 (patch)
tree5f13629ec94a8fee3788940923e4cda8bf367baa /pkgs
parent0cd95a61f3bc7108f2c834abca9de050dac15485 (diff)
zigHook: init
A setup hook for using the Zig compiler in Nixpkgs.

*Warning*: the setup-hook shell script was linted with shellcheck!
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/development/compilers/zig/hook.nix17
-rw-r--r--pkgs/development/compilers/zig/setup-hook.sh90
-rw-r--r--pkgs/top-level/all-packages.nix2
3 files changed, 109 insertions, 0 deletions
diff --git a/pkgs/development/compilers/zig/hook.nix b/pkgs/development/compilers/zig/hook.nix
new file mode 100644
index 0000000000000..66b289695b014
--- /dev/null
+++ b/pkgs/development/compilers/zig/hook.nix
@@ -0,0 +1,17 @@
+{ lib
+, makeSetupHook
+, zig
+}:
+
+makeSetupHook {
+  name = "zig-hook";
+
+  propagatedBuildInputs = [ zig ];
+
+  passthru = { inherit zig; };
+
+  meta = {
+    description = "A setup hook for using the Zig compiler in Nixpkgs";
+    inherit (zig.meta) maintainers platforms badPlatforms broken;
+  };
+} ./setup-hook.sh
diff --git a/pkgs/development/compilers/zig/setup-hook.sh b/pkgs/development/compilers/zig/setup-hook.sh
new file mode 100644
index 0000000000000..f514180692eab
--- /dev/null
+++ b/pkgs/development/compilers/zig/setup-hook.sh
@@ -0,0 +1,90 @@
+# shellcheck shell=bash disable=SC2154,SC2086
+
+# This readonly zigDefaultBuildFlagsArray 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
+
+readonly zigDefaultFlagsArray=("-Drelease-safe=true" "-Dcpu=baseline")
+
+function zigSetGlobalCacheDir {
+    ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
+    export ZIG_GLOBAL_CACHE_DIR
+}
+
+function zigBuildPhase {
+    runHook preBuild
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigBuildFlags "${zigBuildFlagsArray[@]}"
+    )
+
+    echoCmd 'build flags' "${flagsArray[@]}"
+    zig build "${flagsArray[@]}"
+
+    runHook postBuild
+}
+
+function zigCheckPhase {
+    runHook preCheck
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigCheckFlags "${zigCheckFlagsArray[@]}"
+    )
+
+    echoCmd 'check flags' "${flagsArray[@]}"
+    zig build test "${flagsArray[@]}"
+
+    runHook postCheck
+}
+
+function zigInstallPhase {
+    runHook preInstall
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigBuildFlags "${zigBuildFlagsArray[@]}"
+        $zigInstallFlags "${zigInstallFlagsArray[@]}"
+    )
+
+    if [ -z "${dontAddPrefix-}" ]; then
+        # Zig does not recognize `--prefix=/dir/`, only `--prefix /dir/`
+        flagsArray+=("${prefixKey:---prefix}" "$prefix")
+    fi
+
+    echoCmd 'install flags' "${flagsArray[@]}"
+    zig build install "${flagsArray[@]}"
+
+    runHook postInstall
+}
+
+addEnvHooks "$targetOffset" zigSetGlobalCacheDir
+
+if [ -z "${dontUseZigBuild-}" ] && [ -z "${buildPhase-}" ]; then
+    buildPhase=zigBuildPhase
+fi
+
+if [ -z "${dontUseZigCheck-}" ] && [ -z "${checkPhase-}" ]; then
+    checkPhase=zigCheckPhase
+fi
+
+if [ -z "${dontUseZigInstall-}" ] && [ -z "${installPhase-}" ]; then
+    installPhase=zigInstallPhase
+fi
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 2a9362c02dcf7..ff1cb1d7361bf 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -25433,6 +25433,8 @@ with pkgs;
   };
   zig = zig_0_10;
 
+  zigHook = callPackage ../development/compilers/zig/hook.nix { };
+
   zimlib = callPackage ../development/libraries/zimlib { };
 
   zita-convolver = callPackage ../development/libraries/audio/zita-convolver { };