How to vendor a dependency with cargo and buildRustPackage

:wave:
I learned something this week after many hours of frustration and winding paths of my nix incompetence.
So why not share the lesson here!

I maintain a Rust project that uses AWS Rust SDK. Recently, a dependency of a dependency of theirs has a build.rs which makes a network call. (Make bindgen optional · Issue #5 · awesomized/crc64fast-nvme · GitHub)
This fails using nix because nix sandboxes builds.

I wanted to use landonxjames’ helpful fork without forking all the way down.

I learned cargo has its own patching mechanism. Combine that with buildRustPackage cargoLock.outputHashes for victory!

my steps

  1. added this snippet to my workspace’s Cargo.toml:
[patch.'crates-io']
crc64fast-nvme = { git = 'https://github.com/landonxjames/crc64fast-nvme.git', branch = 'c-bind-flag' }
  1. run cargo fetch to ensure the Cargo.lock is updated
    • you need to commit the Cargo.lock changes in your version control
  2. in your buildRustPackage invocation, pass something like:
                cargoLock.lockFile = ./Cargo.lock;
                cargoLock.outputHashes = {
                  "crc64fast-nvme-1.1.1" = "sha256-CvfkaDSG9XLnw2KAmKx7DYK3ubsCfLhPdOQGSIDJaxc=";
                };

:tada: vendoring badge achieved.

This post’s jargon frightens me. One day, I hope it will save a few minutes or so on your nix travels.

3 Likes