about summary refs log tree commit diff
path: root/pkgs/development/compilers/llvm
diff options
context:
space:
mode:
authorTristan Ross <tristan.ross@midstall.com>2024-03-28 11:24:06 -0700
committerTristan Ross <tristan.ross@midstall.com>2024-04-12 15:50:13 -0700
commit782fc2ebac4bb51f6d86f9e998143585edc7d212 (patch)
tree9280438c7e224adce5cd6ed32f5c00e1ef33d421 /pkgs/development/compilers/llvm
parent2989f99f0ffe3f185bdfbfe42f64e836bdd9e0ff (diff)
llvmPackages_{12,13,14,15,16,17,18,git}: use common libcxx
Diffstat (limited to 'pkgs/development/compilers/llvm')
-rw-r--r--pkgs/development/compilers/llvm/12/default.nix32
-rw-r--r--pkgs/development/compilers/llvm/12/libcxx/default.nix139
-rw-r--r--pkgs/development/compilers/llvm/13/default.nix19
-rw-r--r--pkgs/development/compilers/llvm/13/libcxx/default.nix135
-rw-r--r--pkgs/development/compilers/llvm/14/default.nix19
-rw-r--r--pkgs/development/compilers/llvm/15/default.nix27
-rw-r--r--pkgs/development/compilers/llvm/15/libcxx/default.nix145
-rw-r--r--pkgs/development/compilers/llvm/16/default.nix2
-rw-r--r--pkgs/development/compilers/llvm/16/libcxx/default.nix125
-rw-r--r--pkgs/development/compilers/llvm/17/default.nix12
-rw-r--r--pkgs/development/compilers/llvm/17/libcxx/default.nix136
-rw-r--r--pkgs/development/compilers/llvm/18/default.nix6
-rw-r--r--pkgs/development/compilers/llvm/18/libcxx/default.nix130
-rw-r--r--pkgs/development/compilers/llvm/common/libcxx/default.nix (renamed from pkgs/development/compilers/llvm/14/libcxx/default.nix)112
-rw-r--r--pkgs/development/compilers/llvm/git/default.nix6
-rw-r--r--pkgs/development/compilers/llvm/git/libcxx/default.nix130
16 files changed, 174 insertions, 1001 deletions
diff --git a/pkgs/development/compilers/llvm/12/default.nix b/pkgs/development/compilers/llvm/12/default.nix
index d35d463f0bb65..f88f7cf7fa521 100644
--- a/pkgs/development/compilers/llvm/12/default.nix
+++ b/pkgs/development/compilers/llvm/12/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake
 , preLibcCrossHeaders
-, fetchpatch
+, substitute, fetchFromGitHub, fetchpatch
 , libxml2, python3, isl, fetchurl, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
@@ -254,7 +254,35 @@ let
 
     libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
 
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      src = fetchFromGitHub {
+        owner = "llvm";
+        repo = "llvm-project";
+        rev = "refs/tags/llvmorg-${version}";
+        sparseCheckout = [
+          "libcxx"
+          "libcxxabi"
+          "llvm/cmake"
+          "llvm/utils"
+          "runtimes"
+        ];
+        hash = "sha256-etxgXIdWxMTmbZ83Hsc0w6Jt5OSQSUEPVEWqLkHsNBY=";
+      };
+      patches = [
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/12/libcxx/default.nix b/pkgs/development/compilers/llvm/12/libcxx/default.nix
deleted file mode 100644
index 7c6174711456c..0000000000000
--- a/pkgs/development/compilers/llvm/12/libcxx/default.nix
+++ /dev/null
@@ -1,139 +0,0 @@
-{ lib, stdenv, llvm_meta
-, fetchFromGitHub, runCommand, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = fetchFromGitHub {
-    owner = "llvm";
-    repo = "llvm-project";
-    rev = "refs/tags/llvmorg-${version}";
-    sparseCheckout = [
-      "libcxx"
-      "libcxxabi"
-      "llvm/cmake"
-      "llvm/utils"
-      "runtimes"
-    ];
-    hash = "sha256-etxgXIdWxMTmbZ83Hsc0w6Jt5OSQSUEPVEWqLkHsNBY=";
-  };
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include $out/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib $out/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/13/default.nix b/pkgs/development/compilers/llvm/13/default.nix
index 2f314e67bf906..a93a8819567c3 100644
--- a/pkgs/development/compilers/llvm/13/default.nix
+++ b/pkgs/development/compilers/llvm/13/default.nix
@@ -1,7 +1,7 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake
 , preLibcCrossHeaders
 , fetchpatch
-, libxml2, python3, isl, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, isl, fetchFromGitHub, substitute, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -291,7 +291,22 @@ in let
 
     libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
 
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = [
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
       monorepoSrc = src;
diff --git a/pkgs/development/compilers/llvm/13/libcxx/default.nix b/pkgs/development/compilers/llvm/13/libcxx/default.nix
deleted file mode 100644
index b39b4f37ff1c9..0000000000000
--- a/pkgs/development/compilers/llvm/13/libcxx/default.nix
+++ /dev/null
@@ -1,135 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/14/default.nix b/pkgs/development/compilers/llvm/14/default.nix
index 158cd60c03d2e..dc26d47f74865 100644
--- a/pkgs/development/compilers/llvm/14/default.nix
+++ b/pkgs/development/compilers/llvm/14/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, substitute, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -289,7 +289,22 @@ in let
 
     libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
 
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = [
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/15/default.nix b/pkgs/development/compilers/llvm/15/default.nix
index 3c4d656a8f322..8f49b7e0625e9 100644
--- a/pkgs/development/compilers/llvm/15/default.nix
+++ b/pkgs/development/compilers/llvm/15/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, fetchpatch, substitute, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -307,7 +307,30 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = [
+        # See:
+        #   - https://reviews.llvm.org/D133566
+        #   - https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1429146432
+        # !!! Drop in LLVM 16+
+        (fetchpatch {
+          url = "https://github.com/llvm/llvm-project/commit/57c7bb3ec89565c68f858d316504668f9d214d59.patch";
+          hash = "sha256-B07vHmSjy5BhhkGSj3e1E0XmMv5/9+mvC/k70Z29VwY=";
+        })
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/15/libcxx/default.nix b/pkgs/development/compilers/llvm/15/libcxx/default.nix
deleted file mode 100644
index 89181bcbdbbf2..0000000000000
--- a/pkgs/development/compilers/llvm/15/libcxx/default.nix
+++ /dev/null
@@ -1,145 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, fetchpatch, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    # See:
-    #   - https://reviews.llvm.org/D133566
-    #   - https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1429146432
-    # !!! Drop in LLVM 16+
-    (fetchpatch {
-      url = "https://github.com/llvm/llvm-project/commit/57c7bb3ec89565c68f858d316504668f9d214d59.patch";
-      hash = "sha256-B07vHmSjy5BhhkGSj3e1E0XmMv5/9+mvC/k70Z29VwY=";
-    })
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/16/default.nix b/pkgs/development/compilers/llvm/16/default.nix
index 2028b48bd0803..cdff909ba9fb1 100644
--- a/pkgs/development/compilers/llvm/16/default.nix
+++ b/pkgs/development/compilers/llvm/16/default.nix
@@ -322,7 +322,7 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/16/libcxx/default.nix b/pkgs/development/compilers/llvm/16/libcxx/default.nix
deleted file mode 100644
index 146424113ec4e..0000000000000
--- a/pkgs/development/compilers/llvm/16/libcxx/default.nix
+++ /dev/null
@@ -1,125 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, fetchpatch
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  sourceRoot = "${src.name}/runtimes";
-
-  outputs = [ "out" "dev" ];
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/17/default.nix b/pkgs/development/compilers/llvm/17/default.nix
index 42888dcc951af..9e3d8391bbeaf 100644
--- a/pkgs/development/compilers/llvm/17/default.nix
+++ b/pkgs/development/compilers/llvm/17/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, fetchpatch, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -307,7 +307,15 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+        # https://github.com/llvm/llvm-project/issues/64226
+        (fetchpatch {
+          name = "0042-mbstate_t-not-defined.patch";
+          url = "https://github.com/macports/macports-ports/raw/acd8acb171f1658596ed1cf25da48d5b932e2d19/lang/llvm-17/files/0042-mbstate_t-not-defined.patch";
+          hash = "sha256-jo+DYA6zuSv9OH3A0bYwY5TlkWprup4OKQ7rfK1WHBI=";
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/17/libcxx/default.nix b/pkgs/development/compilers/llvm/17/libcxx/default.nix
deleted file mode 100644
index 9ee4fd180d4b6..0000000000000
--- a/pkgs/development/compilers/llvm/17/libcxx/default.nix
+++ /dev/null
@@ -1,136 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, fetchpatch
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
-    # https://github.com/llvm/llvm-project/issues/64226
-    (fetchpatch {
-      name = "0042-mbstate_t-not-defined.patch";
-      url = "https://github.com/macports/macports-ports/raw/acd8acb171f1658596ed1cf25da48d5b932e2d19/lang/llvm-17/files/0042-mbstate_t-not-defined.patch";
-      hash = "sha256-jo+DYA6zuSv9OH3A0bYwY5TlkWprup4OKQ7rfK1WHBI=";
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/18/default.nix b/pkgs/development/compilers/llvm/18/default.nix
index f34d4f58a02a5..bc00ecf18d5fb 100644
--- a/pkgs/development/compilers/llvm/18/default.nix
+++ b/pkgs/development/compilers/llvm/18/default.nix
@@ -307,7 +307,11 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+        # https://github.com/llvm/llvm-project/issues/64226
+        ./libcxx/0001-darwin-10.12-mbstate_t-fix.patch
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/18/libcxx/default.nix b/pkgs/development/compilers/llvm/18/libcxx/default.nix
deleted file mode 100644
index d6c304c0b476a..0000000000000
--- a/pkgs/development/compilers/llvm/18/libcxx/default.nix
+++ /dev/null
@@ -1,130 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = [
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals (lib.versionAtLeast version "18" && !useLLVM && stdenv.hostPlatform.libc == "glibc" && !stdenv.hostPlatform.isStatic) [
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=gcc_s"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
-    # https://github.com/llvm/llvm-project/issues/64226
-    ./0001-darwin-10.12-mbstate_t-fix.patch
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/14/libcxx/default.nix b/pkgs/development/compilers/llvm/common/libcxx/default.nix
index 79eeadb1c5931..de895125f8832 100644
--- a/pkgs/development/compilers/llvm/14/libcxx/default.nix
+++ b/pkgs/development/compilers/llvm/common/libcxx/default.nix
@@ -1,8 +1,21 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
+{ lib
+, stdenv
+, llvm_meta
+, release_version
+, monorepoSrc ? null
+, src ? null
+, patches ? []
+, runCommand
+, substitute
+, cmake
+, lndir
+, ninja
+, python3
+, fixDarwinDylibNames
+, version
 , cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
+, libcxxrt
+, libunwind
 , enableShared ? !stdenv.hostPlatform.isStatic
 }:
 
@@ -13,16 +26,39 @@
 assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
 let
   basename = "libcxx";
+  pname = basename;
   cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
   runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
 
   # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
   useLLVM = stdenv.hostPlatform.useLLVM or false;
 
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
+  src' = if monorepoSrc != null then
+    runCommand "${pname}-src-${version}" {} (''
+      mkdir -p "$out/llvm"
+    '' + (lib.optionalString (lib.versionAtLeast release_version "14") ''
+      cp -r ${monorepoSrc}/cmake "$out"
+    '') + ''
+      cp -r ${monorepoSrc}/libcxx "$out"
+      cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
+      cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
+    '' + (lib.optionalString (lib.versionAtLeast release_version "14") ''
+      cp -r ${monorepoSrc}/third-party "$out"
+    '') + ''
+      cp -r ${monorepoSrc}/runtimes "$out"
+    '' + (lib.optionalString (cxxabi == null) ''
+      cp -r ${monorepoSrc}/libcxxabi "$out"
+    '')) else src;
+
+  cxxabiCMakeFlags = lib.optionals (lib.versionAtLeast release_version "18") [
+    "-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
+  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) (if lib.versionAtLeast release_version "18" then [
+    "-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
+    "-DLIBCXXABI_USE_COMPILER_RT=ON"
+  ] else [
     "-DLIBCXXABI_USE_COMPILER_RT=ON"
     "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
+  ]) ++ lib.optionals stdenv.hostPlatform.isWasm [
     "-DLIBCXXABI_ENABLE_THREADS=OFF"
     "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
   ] ++ lib.optionals (!enableShared) [
@@ -35,8 +71,12 @@ let
     "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
   ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
     "-DLIBCXX_HAS_MUSL_LIBC=1"
+  ] ++ lib.optionals (lib.versionAtLeast release_version "18" && !useLLVM && stdenv.hostPlatform.libc == "glibc" && !stdenv.hostPlatform.isStatic) [
+    "-DLIBCXX_ADDITIONAL_LIBRARIES=gcc_s"
   ] ++ lib.optionals useLLVM [
     "-DLIBCXX_USE_COMPILER_RT=ON"
+  ] ++ lib.optionals (useLLVM && lib.versionAtLeast release_version "16") [
+    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
   ] ++ lib.optionals stdenv.hostPlatform.isWasm [
     "-DLIBCXX_ENABLE_THREADS=OFF"
     "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
@@ -62,47 +102,12 @@ let
 
 in
 
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
+stdenv.mkDerivation (rec {
+  inherit pname version cmakeFlags patches;
 
-  patches = [
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
+  src = src';
 
-  postPatch = ''
-    # fix CMake error when static and LIBCXXABI_USE_LLVM_UNWINDER=ON. aren't
-    # building unwind so don't need to depend on it
-    substituteInPlace libcxx/src/CMakeLists.txt \
-      --replace-fail "add_dependencies(cxx_static unwind)" "# add_dependencies(cxx_static unwind)"
-    cd runtimes
-  '';
+  outputs = [ "out" "dev" ];
 
   preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
     patchShebangs utils/cat_files.py
@@ -119,8 +124,8 @@ stdenv.mkDerivation rec {
   # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
   # support linker scripts so the external cxxabi needs to be symlinked in
   postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
+    lndir ${lib.getDev cxxabi}/include $out/include/c++/v1
+    lndir ${lib.getLib cxxabi}/lib $out/lib
   '';
 
   passthru = {
@@ -138,4 +143,15 @@ stdenv.mkDerivation rec {
     # UIUC License (a BSD-like license)":
     license = with lib.licenses; [ mit ncsa ];
   };
-}
+} // (if (lib.versionOlder release_version "16" || lib.versionAtLeast release_version "17") then {
+  postPatch = (lib.optionalString (lib.versionAtLeast release_version "14" && lib.versionOlder release_version "15") ''
+    # fix CMake error when static and LIBCXXABI_USE_LLVM_UNWINDER=ON. aren't
+    # building unwind so don't need to depend on it
+    substituteInPlace libcxx/src/CMakeLists.txt \
+      --replace-fail "add_dependencies(cxx_static unwind)" "# add_dependencies(cxx_static unwind)"
+  '') + ''
+    cd runtimes
+  '';
+} else {
+  sourceRoot = "${src'.name}/runtimes";
+}))
diff --git a/pkgs/development/compilers/llvm/git/default.nix b/pkgs/development/compilers/llvm/git/default.nix
index cbb380260dee9..c4e22d902c0f1 100644
--- a/pkgs/development/compilers/llvm/git/default.nix
+++ b/pkgs/development/compilers/llvm/git/default.nix
@@ -312,7 +312,11 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+        # https://github.com/llvm/llvm-project/issues/64226
+        ./libcxx/0001-darwin-10.12-mbstate_t-fix.patch
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/git/libcxx/default.nix b/pkgs/development/compilers/llvm/git/libcxx/default.nix
deleted file mode 100644
index d6c304c0b476a..0000000000000
--- a/pkgs/development/compilers/llvm/git/libcxx/default.nix
+++ /dev/null
@@ -1,130 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = [
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals (lib.versionAtLeast version "18" && !useLLVM && stdenv.hostPlatform.libc == "glibc" && !stdenv.hostPlatform.isStatic) [
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=gcc_s"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
-    # https://github.com/llvm/llvm-project/issues/64226
-    ./0001-darwin-10.12-mbstate_t-fix.patch
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}