Difference in package installed via `nix-shell` and `configuration.nix`

I need to install the gmp library to install some OCaml packages that depend on it. When I add gmp to my environment.systemPackages in /etc/nixos/configuration.nix, I see no change and my ocaml packages still do not build. I’m not even able to compile a C file against the gmp headers.

However, when I run nix-shell -p gmp, these problems are resolved. I can compile OCaml and C code that depends on gmp without a problem. This seems very counterintuitive, I’d like the behavior provided by nix-shell to be replicated in my configuration.nix file. It seems very silly to have to open a shell every time I want to write OCaml, considering that I write it very often.

How can I properly set up my environment to include the gmp headers without any additional shells?

I installed a library but my compiler is not finding it. Why?

nix-shell also sets required environment variables so that the C compiler can find gmp in the Nix store. environment.systemPackages does not do that.

How can I properly set up my environment to include the gmp headers without any additional shells?

Find out what nix-shell sets in the environment and reproduce that using environment.variables or similar. Not that I would recommend it, because usually project dependencies should go in project-specific shell.nix files.

That’s expected. If it worked, that would be fundamentally violating the entire point of nix. We don’t have a global folder of libraries with nix or NixOS.

If you want your packages to work in your system config, package them correctly - see Packaging existing software with Nix — nix.dev documentation and the nixpkgs manual. If you’re just doing development, continue to create shells.

That should also not work, nix builds that depended on the environment would be horribly impure and broken.

Assuming we’re talking about builds outside of the Nix sandbox, i.e. not nix-build, but manually building from a normal terminal environment, why wouldn’t it work, given one has reproduced everything that nix-shell -p libsomething does in their system configuration instead?