How to include a local dependency in a Rust build?


I am working on a Rust project that I package with nix and deploy to kubernetes using nix.

Right now I have the problem that I need to package a local dependency (a “path dependency” in Cargo.toml) so that it is available in the nix build.

Here I import the dependency for the Rust build from the parent directory, where the code of the dependency is of course including a proper default.nix. Maybe this is also wrong, as I guess libraries should be packaged differently in nixland.

The build obviously fails, because cargo cannot find the dependency. I don’t know how to solve this problem (except for publishing to a registry, which is what I do not want to do). Can someone help me with this?

1 Like

I think the most straightforward solution is to just include the dependency as a Git dependency (since it’s public anyway). [1] I would assume that buildRustPackage would work just fine with that.

On the off chance that it doesn’t or you have to keep it as a local dependency, another option would be to make the dependency a submodule such that it is is a subdirectory of the parent project and is present when Nix builds the package.

[1] Cargo: Specifying dependencies from git repositories
[2] buildRustPackage documentation

1 Like

Both ideas do not work.

Well, maybe they would technically, but they would be really impractical: Having a git dependency would require me to push every time before I can build the code. That’s just not cool. Also, if I am offline, I cannot build. Meh. Having a submodule of the repository itself just feels mega weird… (remember that everything is in one repository here)!

1 Like

Hmm, I understand now. I think the easiest way would be to patch the Cargo.toml during the build so that the path person-api-types points to the source of the personApiTypes argument/input.

You can access the path to the source of the personApiTypes derivation like this personApiTypes.src

I tried using sed to patch the Cargo.toml file during the patchPhase like this:

postPatch = ''
 sed -i "s|./../person-api-types|${personApiTypes.src}|g" Cargo.toml

But for some reason it isn’t working. I’ve played a bit with the path to the Cargo.toml file but without any success so far but I am still looking into it.

1 Like

example of patch to move relative path(s) to be subfolder and make combined src?
also seems hacky.

so i use git recrusive dep. so my current code depends on self git repo commit before.