Non-deterministic behavior of a path variable in a flake when building with Rust

I have an application made in Rust, that uses a crate which is in the same repo. I am building the application using nix flakes. I set up the dependency in Cargo.toml to use path to the crate, to always get the correct version (../crate).
The directory structure looks like this:

  • / (root of the repo)
    • crate/
      • src/
      • Cargo.toml
    • app/
      • src/
      • Cargo.toml
    • flake.nix

However, when building with buildRustPackage, the tree is different. This results in the path in app/Cargo.toml to be incorrect. I’ve worked around that with this code:

flake.nix

src = ./.;
crate = ./crate;
postUnpack = ''
  mv *-source/app/* *-source/
'';
cargoPatches = [
  ./Cargo.toml.patch
];

Cargo.toml.patch

-crate = { path = "../crate" }
+crate = { path = "crate" }

And this worked, but felt hacky, so I changed it to this:

flake.nix

src = ./app;
crate = ./crate;
postUnpack = ''
  sed 's,^crate.*,crate = { path = "'$crate'"},' -i app/Cargo.toml
'';

(Cargo.toml.patch removed)

This worked and felt better, so I moved on. But, some time later, I tried to build the app, but it didn’t work. After some debugging, I noticed that the $crate variable is not set in postUnpack. I rolled back the change, it built, went back to after the change and it built as well.
So it seems that the $crate variable is only set if the package was already successfully built before. This doesn’t seem right. Am I doing something wrong? Is this a bug? What’s the best way to solve this issue?

Source code in question: GitHub - LoipesMas/vesmor: a fantasy video game console with vector graphics and a declarative statically typed functional programming language
Commit with the hacky but working solution: GitHub - LoipesMas/vesmor at 40301796f7077a062ea17fb5aafe07e02248b272
(vesmor is the app, vesmish is the crate)