I used to use this, but it won't work if you want to share the script with someone who doesn't use Nix. My preferred option recently has been to detect the presence of Nix in the shell script, and if so just rerun the script with the deps installed. That way it still works for anybody not using Nix, but also has pinned versioning for Nix users.
I haven't used this for anything complex, so it may need a little bit of tweaking.
[0] - https://jackson.dev/post/a-portable-nix-shell-shebang/