Build a pckg of a rust app without Cargo.lock


I am building a package of a rust software from a git repo, but this repo does not contain the Cargo.lock file.
To make the pckg, I use buildRustPackage and according to the nix manual, if there is no Cargo.lock file, we have to use a patch :

rustPlatform.buildRustPackage rec {
  # ...
  cargoPatches = [
    # a patch file to add/update Cargo.lock in the source code

The problem is that it is not specified what to put inside this file add-Cargo.lock.patch.
I tried with to put the whole Cargo.lock code locally generated by using diff -u /dev/null Cargo.lock > add-Cargo.lock.patch
But the build does not work because the Cargo.lock file does not exist.
So do I need to create firstly the file ? inside add-Cargo.lock.patch ?
I don’t really know, it’s the first time I use patches.

Hope you could help me :crossed_fingers:

If you’re in the git repo, and you’ve generated the lockfile, you first might need to remove the Cargo.lock file from .gitignore, then you can do git add Cargo.lock and then run git diff --cached Cargo.lock to generate a diff from the staged files.

Or if you want to go the other route, you could just vendor the Cargo.lock file itself, next to your .nix file and do

rustPlatform.buildRustPackage {
  # ...
  cargoLock.lockFile = ./Cargo.lock;

However, the best solution might be to ask upstream to add the lockfile to the repo, since it’s recommended anyways. Once they committed the fix, you can use fetchpatch to fetch the diffs of the commit adding the lockfile. This is better, since putting a huge patch files and lockfiles into nixpkgs can increase the size of the repo quite a bit.


Thanks for your answer.

For those who interested in a solution, as @TomaSajt said, if you can’t have a Carg.lock in the depo, you have to build it locally and add it near your default.nix file.


rustPlatform.buildRustPackage {

  cargoLock = {
      lockFile = ./Cargo.lock;
      outputHashes = {
        "cbindgen-0.26.0" = "sha256-FXfp6eICzLHJ7s87A5XsoHoTP6d/wuf3S+1Bt+K5ET4=";

    postPatch = ''
      ln -s ${./Cargo.lock} Cargo.lock

This is the config that works (from the NixOS manual).

1 Like