How to build a native Rust executable required for a build (mixed C++ and Rust project)

A CMake based C++ project (taskwarrior) that has a certain Rust written component, is using a Rust crate named cxx, and requires an executable named cxxbridge that is provided by the crate cxxbridge-cmd. All of the required crates should be available in the project’s Cargo.lock file and hence the source code of the crate that can build the executable cxxbridge is available in the cargoDeps of the main project.

The problem is that one of the CMakeLists.txt files used by the project lookup for a cxxbridge-cmd in $PATH and if that is failed they try to simply cargo install it. From some reason the cargo install command isn’t aware of the available cargoDeps and tries to download the cargo deps from the internet (and fails of course).

I don’t want to use the cxx-rs Nix package which provides the cxxbridge command, because upstream doesn’t recommend using a cxxbridge-cmd crate that potentially has a potentially different version from the cxx macros that are used in the main project (taskwarrior). See:

Is there a way to tell any cargo command to find the crates where the cargoSetupHook puts them? Current attempt is at:

Strategy I’ve employed before is to use multiple derivations when building things across multiple languages, would that work here? (Of course it makes overriding a bit harder.)

That sounds not bad. If you have an idea how to employ the cargoDeps derivation and build one of the crates from there, that would be very helpful.

The solution was pretty simple!

  postUnpack = ''
    export CARGO_HOME=$PWD/.cargo
  '';
2 Likes