diff --git a/pkgs/development/compilers/zig/0.10.nix b/pkgs/development/compilers/zig/0.10.nix
index 784cae916e34..7b5b65722b51 100644
--- a/pkgs/development/compilers/zig/0.10.nix
+++ b/pkgs/development/compilers/zig/0.10.nix
@@ -2,53 +2,23 @@
 , stdenv
 , fetchFromGitHub
 , cmake
-, coreutils
 , llvmPackages
 , libxml2
 , zlib
-}:
+, coreutils
+}@args:
 
-stdenv.mkDerivation (finalAttrs: {
-  pname = "zig";
+import ./generic.nix args "sha256-69QIkkKzApOGfrBdgtmxFMDytRkSh+0YiaJQPbXsBeo=" {
   version = "0.10.1";
 
-  src = fetchFromGitHub {
-    owner = "ziglang";
-    repo = "zig";
-    rev = finalAttrs.version;
-    hash = "sha256-69QIkkKzApOGfrBdgtmxFMDytRkSh+0YiaJQPbXsBeo=";
-  };
-
   outputs = [ "out" "doc" ];
 
-  nativeBuildInputs = [
-    cmake
-    llvmPackages.llvm.dev
-  ];
-
-  buildInputs = [
-    coreutils
-    libxml2
-    zlib
-  ] ++ (with llvmPackages; [
-    libclang
-    lld
-    llvm
-  ]);
-
   patches = [
     # Backport alignment related panics from zig-master to 0.10.
     # Upstream issue: https://github.com/ziglang/zig/issues/14559
     ./zig_14559.patch
   ];
 
-  # 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" "${coreutils}/bin/env"
-  '';
-
   cmakeFlags = [
     # file RPATH_CHANGE could not write new RPATH
     "-DCMAKE_SKIP_BUILD_RPATH=ON"
@@ -60,34 +30,11 @@ stdenv.mkDerivation (finalAttrs: {
     "-DZIG_TARGET_MCPU=baseline"
   ];
 
-  env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache";
-
   postBuild = ''
-    ./zig2 build-exe ../doc/docgen.zig
-    ./docgen ./zig2 ../doc/langref.html.in ./langref.html
+    ./zig2 run ../doc/docgen.zig -- ./zig2 ../doc/langref.html.in langref.html
   '';
 
   postInstall = ''
-    install -Dm644 -t $doc/share/doc/zig-${finalAttrs.version}/html ./langref.html
+    install -Dm644 -t $doc/share/doc/zig-$version/html ./langref.html
   '';
-
-  doInstallCheck = true;
-
-  installCheckPhase = ''
-    runHook preInstallCheck
-
-    $out/bin/zig test --cache-dir "$TMPDIR/cache-dir" -I $src/test $src/test/behavior.zig
-
-    runHook postInstallCheck
-  '';
-
-  meta = {
-    homepage = "https://ziglang.org/";
-    description =
-      "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
-    changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
-    license = lib.licenses.mit;
-    maintainers = with lib.maintainers; [ aiotter andrewrk AndersonTorres ];
-    platforms = lib.platforms.unix;
-  };
-})
+}
diff --git a/pkgs/development/compilers/zig/0.9.1.nix b/pkgs/development/compilers/zig/0.9.1.nix
index 908d9a49cd6c..1580508ca610 100644
--- a/pkgs/development/compilers/zig/0.9.1.nix
+++ b/pkgs/development/compilers/zig/0.9.1.nix
@@ -5,19 +5,12 @@
 , llvmPackages
 , libxml2
 , zlib
-}:
+, coreutils
+}@args:
 
-stdenv.mkDerivation (finalAttrs: {
-  pname = "zig";
+import ./generic.nix args "sha256-x2c4c9RSrNWGqEngio4ArW7dJjW0gg+8nqBwPcR721k=" {
   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)
     ./rpath.patch
@@ -26,37 +19,21 @@ stdenv.mkDerivation (finalAttrs: {
   ];
 
   # TODO: remove on next upgrade
-  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/
-  '';
-
-  nativeBuildInputs = [
-    cmake
-    llvmPackages.llvm.dev
-  ];
-
-  buildInputs = [
-    libxml2
-    zlib
-  ] ++ (with llvmPackages; [
-    libclang
-    lld
-    llvm
-  ]);
-
-  preBuild = ''
-    export HOME=$TMPDIR;
-  '';
+  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
@@ -65,22 +42,4 @@ stdenv.mkDerivation (finalAttrs: {
     # ensure determinism in the compiler build
     "-DZIG_TARGET_MCPU=baseline"
   ];
-
-  doCheck = true;
-
-  checkPhase = ''
-    runHook preCheck
-    ./zig test --cache-dir "$TMPDIR" -I $src/test $src/test/behavior.zig
-    runHook postCheck
-  '';
-
-  meta = {
-    homepage = "https://ziglang.org/";
-    description =
-      "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
-    license = lib.licenses.mit;
-    changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
-    maintainers = with lib.maintainers; [ aiotter andrewrk AndersonTorres ];
-    platforms = lib.platforms.unix;
-  };
-})
+}
diff --git a/pkgs/development/compilers/zig/generic.nix b/pkgs/development/compilers/zig/generic.nix
new file mode 100644
index 000000000000..b57942092a6c
--- /dev/null
+++ b/pkgs/development/compilers/zig/generic.nix
@@ -0,0 +1,67 @@
+{ lib
+, stdenv
+, fetchFromGitHub
+, cmake
+, llvmPackages
+, libxml2
+, zlib
+, coreutils
+, ...
+}:
+
+hash: args:
+
+stdenv.mkDerivation (finalAttrs: {
+  pname = "zig";
+
+  src = fetchFromGitHub {
+    owner = "ziglang";
+    repo = "zig";
+    rev = finalAttrs.version;
+    inherit hash;
+  };
+
+  nativeBuildInputs = [
+    cmake
+    llvmPackages.llvm.dev
+  ];
+
+  buildInputs = [
+    libxml2
+    zlib
+  ] ++ (with llvmPackages; [
+    libclang
+    lld
+    llvm
+  ]);
+
+  doInstallCheck = 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" "${coreutils}/bin/env"
+  '';
+
+  preBuild = ''
+    export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
+  '';
+
+  installCheckPhase = ''
+    runHook preInstallCheck
+
+    $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig
+
+    runHook postInstallCheck
+  '';
+
+  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;
+    maintainers = with lib.maintainers; [ aiotter andrewrk AndersonTorres figsoda ];
+    platforms = lib.platforms.unix;
+  };
+} // args)