Which goes first patchPhase or unpackPhase?

I have a nix derivation that inject some files to a source repo, via postUnpack. Then I wanted apply a patch to that file I injected (this tweak is necessary for my use case). To create the .patch file, my approach is to create a branch (branch-A) from the master of the repo I am trying to patch, adding the files I injected (to replicate my derivation). Then after that I checkout again another branch (branch-B) from branch-A, then apply my patch here to the injected file. I get the diff and when I apply it to the derivation, I encounter some errors:

  • 1st approach: using patches attribute → It builds but does not apply my patch
  • 2nd approach: via patchPhase and applying the patch manually —> It encountered a hunk error, meaning patch is not compatible (maybe it compares itself to the master/original repo and not the patched repo I made from unpackPhase).

It seems that the patchPhase only compare itself to the original repos, even though I made some changes in postUnpack. Can someone explain?

Unpack goes first, see: Nixpkgs 23.11 manual | Nix & NixOS

Thanks! It turns out I only have a mistake in creating the patch, but yes upon testing patchPhase catches up on unpackPhase.