How to patch harcoded paths to nix store paths when writing a packages

I’m trying to make a package where there are a bunch of lines in multiple files where you can find hardcoded references to /usr/local
What is the best way to path these out with the right /nix/store paths?

You can set postPatch to a script using substituteInPlace, so /usr/local/share/app becomes /nix/store/.../share/app:

stdenv.mkDerivation rec {
  # ...
  postPatch = ''
    for file in somefile somedir/*; do
      substituteInPlace $file \
        --replace /usr/local $out
  # ...