In [1] we see that nix-shell is implemented by running a command inside a
builder. This builder is a stdenv builder, as we see in [2] and [3]. The
stdenv builder invokes the function we see in [4] for every package that is a
build input (i.e. given to nix-shell with -p). This function is called in [5]
which is called in [6].
Replicating this behavior will require a similar Bash script; since setup
hooks—sourced at [7]—are written in Bash.
[1]: https://github.com/NixOS/nix/blob/7c6391ddc730519a632cc0ee526c94a04812d871/src/nix-build/nix-build.cc#L252
[2]: https://github.com/NixOS/nixpkgs/blob/ac6e90696887fab3ab0152acd7294c008a3a1e50/pkgs/build-support/trivial-builders.nix#L27
[3]: https://github.com/NixOS/nixpkgs/blob/ac6e90696887fab3ab0152acd7294c008a3a1e50/pkgs/build-support/trivial-builders.nix#L6
[4]: https://github.com/NixOS/nixpkgs/blob/a80982b4c9e5dc9a34973638d62b0c748bef4eec/pkgs/stdenv/generic/setup.sh#L482
[5]: https://github.com/NixOS/nixpkgs/blob/a80982b4c9e5dc9a34973638d62b0c748bef4eec/pkgs/stdenv/generic/setup.sh#L520
[6]: https://github.com/NixOS/nixpkgs/blob/a80982b4c9e5dc9a34973638d62b0c748bef4eec/pkgs/stdenv/generic/setup.sh#L538
[7]: https://github.com/NixOS/nixpkgs/blob/a80982b4c9e5dc9a34973638d62b0c748bef4eec/pkgs/stdenv/generic/setup.sh#L515