Help with rust overlay for transitive dependency

Hi all,

I’ve been dealing with Build failure: cargo-expand (aarch64-darwin) · Issue #374458 · NixOS/nixpkgs · GitHub – a build failure of cargo-expand on aarch64-darwin based on a failure in bat, which it depends on.

I found that I was able to work around with the following patch in nixpkgs:

diff --git a/pkgs/by-name/ca/cargo-expand/package.nix b/pkgs/by-name/ca/cargo-expand/package.nix
index 22368dfa43e2..395a1bec6a09 100644
--- a/pkgs/by-name/ca/cargo-expand/package.nix
+++ b/pkgs/by-name/ca/cargo-expand/package.nix
@@ -1,4 +1,5 @@
 {
+  writeText,
   lib,
   rustPlatform,
   fetchFromGitHub,
@@ -15,7 +16,29 @@ rustPlatform.buildRustPackage rec {
     hash = "sha256-nbzQmZ8hAiU8K+/VHwbEniTsioCgQhbADIxV9tL3M1k=";
   };
 
-  cargoHash = "sha256-sOxLRNGIHsUBNWWRUo2qyeewL06rU5paq4LbXpksMzg=";
+  cargoHash = "sha256-QjuebG8G8dp1jyG+977kaHeLXnkAXM4+ML425qrpQYI=";
+
+  cargoPatches =
+    let
+      patch = writeText "cargo-expand-patch" ''
+        diff --git a/Cargo.lock b/Cargo.lock
+        index b92ebe7..dbcbb4b 100644
+        --- a/Cargo.lock
+        +++ b/Cargo.lock
+        @@ -96,8 +96,7 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+         [[package]]
+         name = "bat"
+         version = "0.25.0"
+        -source = "registry+https://github.com/rust-lang/crates.io-index"
+        -checksum = "2ab792c2ad113a666f08856c88cdec0a62d732559b1f3982eedf0142571e669a"
+        +source = "git+https://github.com/sharkdp/bat?rev=498df11a509051f23155039142b0d74c18e31b60#498df11a509051f23155039142b0d74c18e31b60"
+         dependencies = [
+          "ansi_colours",
+          "anyhow",
+      '';
+    in
+    [ patch ];
+  useFetchCargoVendor = true;
 
   meta = with lib; {
     description = "Cargo subcommand to show result of macro expansion";

I wanted to see if I could turn this into an overlay instead of directly patching nixpkgs, but it doesn’t seem to be working. I’ve been trying to use threads like this one as an example, but they don’t seem to be working.

You can see commented out lines of a few different iterations that I’ve tried (and haven’t been working):

  _: prev:
  {
    # https://github.com/NixOS/nixpkgs/issues/374458
    cargo-expand =
      let
        patch = prev.writeText "cargo-expand-patch" ''
          diff --git a/Cargo.lock b/Cargo.lock
          index b92ebe7..dbcbb4b 100644
          --- a/Cargo.lock
          +++ b/Cargo.lock
          @@ -96,8 +96,7 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
           [[package]]
           name = "bat"
           version = "0.25.0"
          -source = "registry+https://github.com/rust-lang/crates.io-index"
          -checksum = "2ab792c2ad113a666f08856c88cdec0a62d732559b1f3982eedf0142571e669a"
          +source = "git+https://github.com/sharkdp/bat?rev=498df11a509051f23155039142b0d74c18e31b60#498df11a509051f23155039142b0d74c18e31b60"
           dependencies = [
            "ansi_colours",
            "anyhow",
        '';
      in
      prev.cargo-expand.overrideAttrs (
        finalAttrs: prevAttrs:
        # drv:
        # prev.rustPlatform.buildRustPackage drv
        # // {
        {
          # nativeBuildInputs = [ prev.keepBuildTree ];
          cargoHash = "";
          # useFetchCargoVendor = true;
          # cargoDeps = drv.cargoDeps.overrideAttrs (
          #   prev.lib.const {
          #   }
          # );
          cargoDeps = prevAttrs.cargoDeps.overrideAttrs (old: {
            hash = finalAttrs.cargoHash;
            cargoPatches = [ patch ];
          });
          cargoPatches = [ patch ];
        }
      );
  }

What am I doing wrong? Is there a way to use overrideAttrs to accomplish this patch to cargo-expand’s Cargo.lock?

Many thanks in advance.