Nix-shell without nix-shell

I’d like to do roughly Reddit - Dive into anything

I.e. I need pkg-config at runtime instead of build time, and afaict the only way to do that right now is nix-shell… or is it?

I want to do it for similar reasons - faster startup, but I might not have internet access while running a script that needs the dev environment so having nix build it at runtime could pose issues, and invoking nix-shell could complicate things in other ways (sharing config/variables with my system, various other environment things).

Would sourcing setup.sh and calling _activatePackage() manually on all my systemPackages work (somehow, maybe I could copy and paste bits of it…)? Or maybe there’s a way to precompile a nix shell and use it from a script? I’m not sure what the output of mkShell has in it.

Package it using one of the trivial builders (or mkDerivation itself if you prefer).

https://nixos.org/manual/nixpkgs/stable/#chap-trivial-builders

Thanks for the response! Are you saying to do e.g. writeShellApplication with derivationArgs.nativeBuildInputs = [ pkgs.pkg-config ] or something like that? IIUC that will run the hooks while preparing the script (build time), but not when the prepared script executes (runtime).

I tried writeShellApplication with runtimeInputs = [ pkgs.pkg-config ] and it doesn’t run the setup hooks, just sets PATH I think.