Update generate.sh to run using nix-shell. Also make it fail with meaningful output instead of writing empty output files. This is extracted from https://github.com/NixOS/nixpkgs PR 58131. This relies on the shebang being used.