about summary refs log tree commit diff
path: root/pkgs/development/compilers
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/development/compilers')
-rw-r--r--pkgs/development/compilers/zig/0.10.nix43
-rw-r--r--pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch (renamed from pkgs/development/compilers/zig/002-0.10-macho-fixes.patch)0
-rw-r--r--pkgs/development/compilers/zig/0.10/default.nix109
-rw-r--r--pkgs/development/compilers/zig/0.10/hook.nix47
-rw-r--r--pkgs/development/compilers/zig/0.10/setup-hook.sh70
-rw-r--r--pkgs/development/compilers/zig/0.11.nix37
-rw-r--r--pkgs/development/compilers/zig/0.11/default.nix103
-rw-r--r--pkgs/development/compilers/zig/0.11/hook.nix47
-rw-r--r--pkgs/development/compilers/zig/0.11/setup-hook.sh70
-rw-r--r--pkgs/development/compilers/zig/0.12.nix37
-rw-r--r--pkgs/development/compilers/zig/0.12/default.nix103
-rw-r--r--pkgs/development/compilers/zig/0.12/hook.nix47
-rw-r--r--pkgs/development/compilers/zig/0.12/setup-hook.sh70
-rw-r--r--pkgs/development/compilers/zig/0.9.nix47
-rw-r--r--pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch (renamed from pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch)0
-rw-r--r--pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch (renamed from pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch)0
-rw-r--r--pkgs/development/compilers/zig/0.9/default.nix111
-rw-r--r--pkgs/development/compilers/zig/0.9/hook.nix47
-rw-r--r--pkgs/development/compilers/zig/0.9/setup-hook.sh70
-rw-r--r--pkgs/development/compilers/zig/shell-completions.nix42
20 files changed, 894 insertions, 206 deletions
diff --git a/pkgs/development/compilers/zig/0.10.nix b/pkgs/development/compilers/zig/0.10.nix
deleted file mode 100644
index d02b9ff1c976b..0000000000000
--- a/pkgs/development/compilers/zig/0.10.nix
+++ /dev/null
@@ -1,43 +0,0 @@
-{ lib
-, stdenv
-, fetchFromGitHub
-, cmake
-, llvmPackages
-, libxml2
-, zlib
-, coreutils
-, callPackage
-}@args:
-
-import ./generic.nix args {
-  version = "0.10.1";
-
-  hash = "sha256-69QIkkKzApOGfrBdgtmxFMDytRkSh+0YiaJQPbXsBeo=";
-
-  outputs = [ "out" "doc" ];
-
-  patches = [
-    # Backport alignment related panics from zig-master to 0.10.
-    # Upstream issue: https://github.com/ziglang/zig/issues/14559
-    ./002-0.10-macho-fixes.patch
-  ];
-
-  cmakeFlags = [
-    # file RPATH_CHANGE could not write new RPATH
-    "-DCMAKE_SKIP_BUILD_RPATH=ON"
-
-    # always link against static build of LLVM
-    "-DZIG_STATIC_LLVM=ON"
-
-    # ensure determinism in the compiler build
-    "-DZIG_TARGET_MCPU=baseline"
-  ];
-
-  postBuild = ''
-    ./zig2 run ../doc/docgen.zig -- ./zig2 ../doc/langref.html.in langref.html
-  '';
-
-  postInstall = ''
-    install -Dm644 -t $doc/share/doc/zig-$version/html ./langref.html
-  '';
-}
diff --git a/pkgs/development/compilers/zig/002-0.10-macho-fixes.patch b/pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch
index 95f0bdf701e29..95f0bdf701e29 100644
--- a/pkgs/development/compilers/zig/002-0.10-macho-fixes.patch
+++ b/pkgs/development/compilers/zig/0.10/001-0.10-macho-fixes.patch
diff --git a/pkgs/development/compilers/zig/0.10/default.nix b/pkgs/development/compilers/zig/0.10/default.nix
new file mode 100644
index 0000000000000..170edafa819cf
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.10/default.nix
@@ -0,0 +1,109 @@
+{
+  lib,
+  callPackage,
+  cmake,
+  coreutils,
+  fetchFromGitHub,
+  libxml2,
+  llvmPackages,
+  stdenv,
+  testers,
+  zlib,
+}:
+
+stdenv.mkDerivation (finalAttrs: {
+  pname = "zig";
+  version = "0.10.1";
+
+  src = fetchFromGitHub {
+    owner = "ziglang";
+    repo = "zig";
+    rev = finalAttrs.version;
+    hash = "sha256-69QIkkKzApOGfrBdgtmxFMDytRkSh+0YiaJQPbXsBeo=";
+  };
+
+  patches = [
+    # Backport alignment related panics from zig-master to 0.10.
+    # Upstream issue: https://github.com/ziglang/zig/issues/14559
+    ./001-0.10-macho-fixes.patch
+  ];
+
+  nativeBuildInputs = [
+    cmake
+    (lib.getDev llvmPackages.llvm)
+  ];
+
+  buildInputs =
+    [
+      libxml2
+      zlib
+    ]
+    ++ (with llvmPackages; [
+      libclang
+      lld
+      llvm
+    ]);
+
+  outputs = [
+    "out"
+    "doc"
+  ];
+
+  cmakeFlags = [
+    # file RPATH_CHANGE could not write new RPATH
+    (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
+    # always link against static build of LLVM
+    (lib.cmakeBool "ZIG_STATIC_LLVM" true)
+    # ensure determinism in the compiler build
+    (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline")
+  ];
+
+  env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache";
+
+  doInstallCheck = true;
+
+  strictDeps = true;
+
+  # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
+  # work in Nix's sandbox. Use env from our coreutils instead.
+  postPatch = ''
+    substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
+      --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
+  '';
+
+  postBuild = ''
+    ./zig2 run ../doc/docgen.zig -- ./zig2 ../doc/langref.html.in langref.html
+  '';
+
+  postInstall = ''
+    install -Dm644 -t $doc/share/doc/zig-$version/html ./langref.html
+  '';
+
+  installCheckPhase = ''
+    runHook preInstallCheck
+
+    $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig
+
+    runHook postInstallCheck
+  '';
+
+  passthru = {
+    hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
+    tests = {
+      version = testers.testVersion {
+        package = finalAttrs.finalPackage;
+        command = "zig version";
+      };
+    };
+  };
+
+  meta = {
+    description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
+    homepage = "https://ziglang.org/";
+    changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
+    license = lib.licenses.mit;
+    mainProgram = "zig";
+    maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members;
+    platforms = lib.platforms.unix;
+  };
+})
diff --git a/pkgs/development/compilers/zig/0.10/hook.nix b/pkgs/development/compilers/zig/0.10/hook.nix
new file mode 100644
index 0000000000000..298d50d9e5f9d
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.10/hook.nix
@@ -0,0 +1,47 @@
+{
+  lib,
+  makeSetupHook,
+  zig,
+}:
+
+makeSetupHook {
+  name = "zig-hook";
+
+  propagatedBuildInputs = [ zig ];
+
+  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 = [
+      "-Dcpu=baseline"
+      "-Drelease-safe=true"
+    ];
+  };
+
+  passthru = {
+    inherit zig;
+  };
+
+  meta = {
+    description = "A 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/0.10/setup-hook.sh b/pkgs/development/compilers/zig/0.10/setup-hook.sh
new file mode 100644
index 0000000000000..689ebec8a3077
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.10/setup-hook.sh
@@ -0,0 +1,70 @@
+# shellcheck shell=bash disable=SC2154,SC2086
+
+readonly zigDefaultFlagsArray=(@zig_default_flags@)
+
+function zigSetGlobalCacheDir {
+    ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
+    export ZIG_GLOBAL_CACHE_DIR
+}
+
+function zigBuildPhase {
+    runHook preBuild
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigBuildFlags "${zigBuildFlagsArray[@]}"
+    )
+
+    echoCmd 'zig build flags' "${flagsArray[@]}"
+    zig build "${flagsArray[@]}"
+
+    runHook postBuild
+}
+
+function zigCheckPhase {
+    runHook preCheck
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigCheckFlags "${zigCheckFlagsArray[@]}"
+    )
+
+    echoCmd 'zig 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 'zig 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/development/compilers/zig/0.11.nix b/pkgs/development/compilers/zig/0.11.nix
deleted file mode 100644
index b393f42c65136..0000000000000
--- a/pkgs/development/compilers/zig/0.11.nix
+++ /dev/null
@@ -1,37 +0,0 @@
-{ lib
-, stdenv
-, fetchFromGitHub
-, cmake
-, llvmPackages
-, libxml2
-, zlib
-, coreutils
-, callPackage
-}@args:
-
-import ./generic.nix args {
-  version = "0.11.0";
-
-  hash = "sha256-iuU1fzkbJxI+0N1PiLQM013Pd1bzrgqkbIyTxo5gB2I=";
-
-  outputs = [ "out" "doc" ];
-
-  cmakeFlags = [
-    # file RPATH_CHANGE could not write new RPATH
-    "-DCMAKE_SKIP_BUILD_RPATH=ON"
-
-    # always link against static build of LLVM
-    "-DZIG_STATIC_LLVM=ON"
-
-    # ensure determinism in the compiler build
-    "-DZIG_TARGET_MCPU=baseline"
-  ];
-
-  postBuild = ''
-    stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
-  '';
-
-  postInstall = ''
-    install -Dm444 -t $doc/share/doc/zig-$version/html langref.html
-  '';
-}
diff --git a/pkgs/development/compilers/zig/0.11/default.nix b/pkgs/development/compilers/zig/0.11/default.nix
new file mode 100644
index 0000000000000..0f19f455cbb0f
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.11/default.nix
@@ -0,0 +1,103 @@
+{
+  lib,
+  callPackage,
+  cmake,
+  coreutils,
+  fetchFromGitHub,
+  libxml2,
+  llvmPackages,
+  stdenv,
+  testers,
+  zlib,
+}:
+
+stdenv.mkDerivation (finalAttrs: {
+  pname = "zig";
+  version = "0.11.0";
+
+  src = fetchFromGitHub {
+    owner = "ziglang";
+    repo = "zig";
+    rev = finalAttrs.version;
+    hash = "sha256-iuU1fzkbJxI+0N1PiLQM013Pd1bzrgqkbIyTxo5gB2I=";
+  };
+
+  nativeBuildInputs = [
+    cmake
+    (lib.getDev llvmPackages.llvm)
+  ];
+
+  buildInputs =
+    [
+      libxml2
+      zlib
+    ]
+    ++ (with llvmPackages; [
+      libclang
+      lld
+      llvm
+    ]);
+
+  outputs = [
+    "out"
+    "doc"
+  ];
+
+  cmakeFlags = [
+    # file RPATH_CHANGE could not write new RPATH
+    (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
+    # ensure determinism in the compiler build
+    (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline")
+    # always link against static build of LLVM
+    (lib.cmakeBool "ZIG_STATIC_LLVM" true)
+  ];
+
+  env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache";
+
+  doInstallCheck = true;
+
+  strictDeps = true;
+
+  # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
+  # work in Nix's sandbox. Use env from our coreutils instead.
+  postPatch = ''
+    substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
+      --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
+  '';
+
+  postBuild = ''
+    stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
+  '';
+
+  postInstall = ''
+    install -Dm444 -t $doc/share/doc/zig-$version/html langref.html
+  '';
+
+  installCheckPhase = ''
+    runHook preInstallCheck
+
+    $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig
+
+    runHook postInstallCheck
+  '';
+
+  passthru = {
+    hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
+    tests = {
+      version = testers.testVersion {
+        package = finalAttrs.finalPackage;
+        command = "zig version";
+      };
+    };
+  };
+
+  meta = {
+    description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
+    homepage = "https://ziglang.org/";
+    changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
+    license = lib.licenses.mit;
+    mainProgram = "zig";
+    maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members;
+    platforms = lib.platforms.unix;
+  };
+})
diff --git a/pkgs/development/compilers/zig/0.11/hook.nix b/pkgs/development/compilers/zig/0.11/hook.nix
new file mode 100644
index 0000000000000..391140d4a7ff3
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.11/hook.nix
@@ -0,0 +1,47 @@
+{
+  lib,
+  makeSetupHook,
+  zig,
+}:
+
+makeSetupHook {
+  name = "zig-hook";
+
+  propagatedBuildInputs = [ zig ];
+
+  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 = [
+      "-Dcpu=baseline"
+      "-Doptimize=ReleaseSafe"
+    ];
+  };
+
+  passthru = {
+    inherit zig;
+  };
+
+  meta = {
+    description = "A 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/0.11/setup-hook.sh b/pkgs/development/compilers/zig/0.11/setup-hook.sh
new file mode 100644
index 0000000000000..689ebec8a3077
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.11/setup-hook.sh
@@ -0,0 +1,70 @@
+# shellcheck shell=bash disable=SC2154,SC2086
+
+readonly zigDefaultFlagsArray=(@zig_default_flags@)
+
+function zigSetGlobalCacheDir {
+    ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
+    export ZIG_GLOBAL_CACHE_DIR
+}
+
+function zigBuildPhase {
+    runHook preBuild
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigBuildFlags "${zigBuildFlagsArray[@]}"
+    )
+
+    echoCmd 'zig build flags' "${flagsArray[@]}"
+    zig build "${flagsArray[@]}"
+
+    runHook postBuild
+}
+
+function zigCheckPhase {
+    runHook preCheck
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigCheckFlags "${zigCheckFlagsArray[@]}"
+    )
+
+    echoCmd 'zig 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 'zig 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/development/compilers/zig/0.12.nix b/pkgs/development/compilers/zig/0.12.nix
deleted file mode 100644
index e234b600bc227..0000000000000
--- a/pkgs/development/compilers/zig/0.12.nix
+++ /dev/null
@@ -1,37 +0,0 @@
-{ lib
-, stdenv
-, fetchFromGitHub
-, cmake
-, llvmPackages
-, libxml2
-, zlib
-, coreutils
-, callPackage
-}@args:
-
-import ./generic.nix args {
-  version = "0.12.0";
-
-  hash = "sha256-RNZiUZtaKXoab5kFrDij6YCAospeVvlLWheTc3FGMks=";
-
-  outputs = [ "out" "doc" ];
-
-  cmakeFlags = [
-    # file RPATH_CHANGE could not write new RPATH
-    (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
-
-    # always link against static build of LLVM
-    (lib.cmakeBool "ZIG_STATIC_LLVM" true)
-
-    # ensure determinism in the compiler build
-    (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline")
-  ];
-
-  postBuild = ''
-    stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
-  '';
-
-  postInstall = ''
-    install -Dm444 langref.html -t $doc/share/doc/zig-$version/html
-  '';
-}
diff --git a/pkgs/development/compilers/zig/0.12/default.nix b/pkgs/development/compilers/zig/0.12/default.nix
new file mode 100644
index 0000000000000..831366419a934
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.12/default.nix
@@ -0,0 +1,103 @@
+{
+  lib,
+  callPackage,
+  cmake,
+  coreutils,
+  fetchFromGitHub,
+  libxml2,
+  llvmPackages,
+  stdenv,
+  testers,
+  zlib,
+}:
+
+stdenv.mkDerivation (finalAttrs: {
+  pname = "zig";
+  version = "0.12.0";
+
+  src = fetchFromGitHub {
+    owner = "ziglang";
+    repo = "zig";
+    rev = finalAttrs.version;
+    hash = "sha256-RNZiUZtaKXoab5kFrDij6YCAospeVvlLWheTc3FGMks=";
+  };
+
+  nativeBuildInputs = [
+    cmake
+    (lib.getDev llvmPackages.llvm)
+  ];
+
+  buildInputs =
+    [
+      libxml2
+      zlib
+    ]
+    ++ (with llvmPackages; [
+      libclang
+      lld
+      llvm
+    ]);
+
+  outputs = [
+    "out"
+    "doc"
+  ];
+
+  cmakeFlags = [
+    # file RPATH_CHANGE could not write new RPATH
+    (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
+    # ensure determinism in the compiler build
+    (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline")
+    # always link against static build of LLVM
+    (lib.cmakeBool "ZIG_STATIC_LLVM" true)
+  ];
+
+  env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache";
+
+  doInstallCheck = true;
+
+  strictDeps = true;
+
+  # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
+  # work in Nix's sandbox. Use env from our coreutils instead.
+  postPatch = ''
+    substituteInPlace lib/std/zig/system.zig \
+      --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
+  '';
+
+  postBuild = ''
+    stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
+  '';
+
+  postInstall = ''
+    install -Dm444 langref.html -t $doc/share/doc/zig-${finalAttrs.version}/html
+  '';
+
+  installCheckPhase = ''
+    runHook preInstallCheck
+
+    $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig
+
+    runHook postInstallCheck
+  '';
+
+  passthru = {
+    hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
+    tests = {
+      version = testers.testVersion {
+        package = finalAttrs.finalPackage;
+        command = "zig version";
+      };
+    };
+  };
+
+  meta = {
+    description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
+    changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
+    homepage = "https://ziglang.org/";
+    license = lib.licenses.mit;
+    mainProgram = "zig";
+    maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members;
+    platforms = lib.platforms.unix;
+  };
+})
diff --git a/pkgs/development/compilers/zig/0.12/hook.nix b/pkgs/development/compilers/zig/0.12/hook.nix
new file mode 100644
index 0000000000000..294aa851ce0d3
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.12/hook.nix
@@ -0,0 +1,47 @@
+{
+  lib,
+  makeSetupHook,
+  zig,
+}:
+
+makeSetupHook {
+  name = "zig-hook";
+
+  propagatedBuildInputs = [ zig ];
+
+  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 = [
+      "-Dcpu=baseline"
+      "--release=safe"
+    ];
+  };
+
+  passthru = {
+    inherit zig;
+  };
+
+  meta = {
+    description = "A 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/0.12/setup-hook.sh b/pkgs/development/compilers/zig/0.12/setup-hook.sh
new file mode 100644
index 0000000000000..689ebec8a3077
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.12/setup-hook.sh
@@ -0,0 +1,70 @@
+# shellcheck shell=bash disable=SC2154,SC2086
+
+readonly zigDefaultFlagsArray=(@zig_default_flags@)
+
+function zigSetGlobalCacheDir {
+    ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
+    export ZIG_GLOBAL_CACHE_DIR
+}
+
+function zigBuildPhase {
+    runHook preBuild
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigBuildFlags "${zigBuildFlagsArray[@]}"
+    )
+
+    echoCmd 'zig build flags' "${flagsArray[@]}"
+    zig build "${flagsArray[@]}"
+
+    runHook postBuild
+}
+
+function zigCheckPhase {
+    runHook preCheck
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigCheckFlags "${zigCheckFlagsArray[@]}"
+    )
+
+    echoCmd 'zig 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 'zig 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/development/compilers/zig/0.9.nix b/pkgs/development/compilers/zig/0.9.nix
deleted file mode 100644
index d648c9c2eb642..0000000000000
--- a/pkgs/development/compilers/zig/0.9.nix
+++ /dev/null
@@ -1,47 +0,0 @@
-{ lib
-, stdenv
-, fetchFromGitHub
-, cmake
-, llvmPackages
-, libxml2
-, zlib
-, coreutils
-, callPackage
-}@args:
-
-import ./generic.nix args {
-  version = "0.9.1";
-
-  hash = "sha256-x2c4c9RSrNWGqEngio4ArW7dJjW0gg+8nqBwPcR721k=";
-
-  patches = [
-    # Fix index out of bounds reading RPATH (cherry-picked from 0.10-dev)
-    ./000-0.9-read-dynstr-at-rpath-offset.patch
-    # Fix build on macOS 13 (cherry-picked from 0.10-dev)
-    ./001-0.9-bump-macos-supported-version.patch
-  ];
-
-  prePatch =
-    let
-      zig_0_10_0 = fetchFromGitHub {
-        owner = "ziglang";
-        repo = "zig";
-        rev = "0.10.0";
-        hash = "sha256-DNs937N7PLQimuM2anya4npYXcj6cyH+dRS7AiOX7tw=";
-      };
-    in
-    ''
-      cp -R ${zig_0_10_0}/lib/libc/include/any-macos.13-any lib/libc/include/any-macos.13-any
-      cp -R ${zig_0_10_0}/lib/libc/include/aarch64-macos.13-none lib/libc/include/aarch64-macos.13-gnu
-      cp -R ${zig_0_10_0}/lib/libc/include/x86_64-macos.13-none lib/libc/include/x86_64-macos.13-gnu
-      cp ${zig_0_10_0}/lib/libc/darwin/libSystem.13.tbd lib/libc/darwin/
-    '';
-
-  cmakeFlags = [
-    # file RPATH_CHANGE could not write new RPATH
-    "-DCMAKE_SKIP_BUILD_RPATH=ON"
-
-    # ensure determinism in the compiler build
-    "-DZIG_TARGET_MCPU=baseline"
-  ];
-}
diff --git a/pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch b/pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch
index 70633193f944d..70633193f944d 100644
--- a/pkgs/development/compilers/zig/000-0.9-read-dynstr-at-rpath-offset.patch
+++ b/pkgs/development/compilers/zig/0.9/000-0.9-read-dynstr-at-rpath-offset.patch
diff --git a/pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch b/pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch
index 07b90e223553b..07b90e223553b 100644
--- a/pkgs/development/compilers/zig/001-0.9-bump-macos-supported-version.patch
+++ b/pkgs/development/compilers/zig/0.9/001-0.9-bump-macos-supported-version.patch
diff --git a/pkgs/development/compilers/zig/0.9/default.nix b/pkgs/development/compilers/zig/0.9/default.nix
new file mode 100644
index 0000000000000..079773f00e88c
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.9/default.nix
@@ -0,0 +1,111 @@
+{
+  lib,
+  callPackage,
+  cmake,
+  coreutils,
+  fetchFromGitHub,
+  libxml2,
+  llvmPackages,
+  stdenv,
+  testers,
+  zlib,
+}:
+
+stdenv.mkDerivation (finalAttrs: {
+  pname = "zig";
+  version = "0.9.1";
+
+  src = fetchFromGitHub {
+    owner = "ziglang";
+    repo = "zig";
+    rev = finalAttrs.version;
+    hash = "sha256-x2c4c9RSrNWGqEngio4ArW7dJjW0gg+8nqBwPcR721k=";
+  };
+
+  patches = [
+    # Fix index out of bounds reading RPATH (cherry-picked from 0.10-dev)
+    ./000-0.9-read-dynstr-at-rpath-offset.patch
+    # Fix build on macOS 13 (cherry-picked from 0.10-dev)
+    ./001-0.9-bump-macos-supported-version.patch
+  ];
+
+  nativeBuildInputs = [
+    cmake
+    (lib.getDev llvmPackages.llvm)
+  ];
+
+  buildInputs =
+    [
+      libxml2
+      zlib
+    ]
+    ++ (with llvmPackages; [
+      libclang
+      lld
+      llvm
+    ]);
+
+  cmakeFlags = [
+    # file RPATH_CHANGE could not write new RPATH
+    (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
+    # ensure determinism in the compiler build
+    (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline")
+  ];
+
+  env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache";
+
+  doInstallCheck = true;
+
+  strictDeps = true;
+
+  prePatch =
+    let
+      zig_0_10_0 = fetchFromGitHub {
+        owner = "ziglang";
+        repo = "zig";
+        rev = "0.10.0";
+        hash = "sha256-DNs937N7PLQimuM2anya4npYXcj6cyH+dRS7AiOX7tw=";
+      };
+    in
+    ''
+      cp -R ${zig_0_10_0}/lib/libc/include/any-macos.13-any lib/libc/include/any-macos.13-any
+      cp -R ${zig_0_10_0}/lib/libc/include/aarch64-macos.13-none lib/libc/include/aarch64-macos.13-gnu
+      cp -R ${zig_0_10_0}/lib/libc/include/x86_64-macos.13-none lib/libc/include/x86_64-macos.13-gnu
+      cp ${zig_0_10_0}/lib/libc/darwin/libSystem.13.tbd lib/libc/darwin/
+    '';
+
+  # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
+  # work in Nix's sandbox. Use env from our coreutils instead.
+  postPatch = ''
+    substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
+      --replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
+  '';
+
+  installCheckPhase = ''
+    runHook preInstallCheck
+
+    $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig
+
+    runHook postInstallCheck
+  '';
+
+  passthru = {
+    hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
+    tests = {
+      version = testers.testVersion {
+        package = finalAttrs.finalPackage;
+        command = "zig version";
+      };
+    };
+  };
+
+  meta = {
+    description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
+    homepage = "https://ziglang.org/";
+    changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
+    license = lib.licenses.mit;
+    mainProgram = "zig";
+    maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members;
+    platforms = lib.platforms.unix;
+  };
+})
diff --git a/pkgs/development/compilers/zig/0.9/hook.nix b/pkgs/development/compilers/zig/0.9/hook.nix
new file mode 100644
index 0000000000000..298d50d9e5f9d
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.9/hook.nix
@@ -0,0 +1,47 @@
+{
+  lib,
+  makeSetupHook,
+  zig,
+}:
+
+makeSetupHook {
+  name = "zig-hook";
+
+  propagatedBuildInputs = [ zig ];
+
+  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 = [
+      "-Dcpu=baseline"
+      "-Drelease-safe=true"
+    ];
+  };
+
+  passthru = {
+    inherit zig;
+  };
+
+  meta = {
+    description = "A 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/0.9/setup-hook.sh b/pkgs/development/compilers/zig/0.9/setup-hook.sh
new file mode 100644
index 0000000000000..689ebec8a3077
--- /dev/null
+++ b/pkgs/development/compilers/zig/0.9/setup-hook.sh
@@ -0,0 +1,70 @@
+# shellcheck shell=bash disable=SC2154,SC2086
+
+readonly zigDefaultFlagsArray=(@zig_default_flags@)
+
+function zigSetGlobalCacheDir {
+    ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
+    export ZIG_GLOBAL_CACHE_DIR
+}
+
+function zigBuildPhase {
+    runHook preBuild
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigBuildFlags "${zigBuildFlagsArray[@]}"
+    )
+
+    echoCmd 'zig build flags' "${flagsArray[@]}"
+    zig build "${flagsArray[@]}"
+
+    runHook postBuild
+}
+
+function zigCheckPhase {
+    runHook preCheck
+
+    local flagsArray=(
+        "${zigDefaultFlagsArray[@]}"
+        $zigCheckFlags "${zigCheckFlagsArray[@]}"
+    )
+
+    echoCmd 'zig 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 'zig 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/development/compilers/zig/shell-completions.nix b/pkgs/development/compilers/zig/shell-completions.nix
deleted file mode 100644
index 44773cf9a553e..0000000000000
--- a/pkgs/development/compilers/zig/shell-completions.nix
+++ /dev/null
@@ -1,42 +0,0 @@
-{ lib
-, stdenv
-, fetchFromGitHub
-, installShellFiles
-, unstableGitUpdater
-}:
-
-stdenv.mkDerivation (finalAttrs: {
-  pname = "zig-shell-completions";
-  version = "0-unstable-2023-11-18";
-
-  src = fetchFromGitHub {
-    owner = "ziglang";
-    repo = "shell-completions";
-    rev = "31d3ad12890371bf467ef7143f5c2f31cfa7b7c1";
-    hash = "sha256-ID/K0vdg7BTKGgozISk/X4RBxCVfhSkVD6GSZUoP9Ls=";
-  };
-
-  nativeBuildInputs = [ installShellFiles ];
-
-  dontConfigure = true;
-  dontBuild = true;
-
-  installPhase = ''
-    runHook preInstall
-
-    installShellCompletion --bash --name zig.bash _zig.bash
-    installShellCompletion --zsh --name _zig _zig
-
-    runHook postInstall
-  '';
-
-  passthru.updateScript = unstableGitUpdater { };
-
-  meta = {
-    homepage = "https://github.com/ziglang/shell-completions";
-    description = "Shell completions for the Zig compiler";
-    license = lib.licenses.mit;
-    maintainers = with lib.maintainers; [ aaronjheng ];
-    platforms = lib.platforms.all;
-  };
-})