GCC fails in standard environment but works in nix-shell

I can’t run gcc in my standard NixOS environment even though all libraries are installed.
Running inside nix-shell works perfectly:

nix-shell -p gcc glibc.dev glibc binutils

But outside nix-shell I get linker errors like:


ld: cannot find Scrt1.o: No such file or directory
ld: cannot find crti.o: No such file or directory
ld: cannot find -lgcc_s

How can I make GCC work in my normal environment?

What exactly do you mean by “normal environment”?

What do you mean by “all libraries are installed”?

On NixOS, a shell is the normal environment for development, as well as the intended way to “install” libraries for the sake of development.

2 Likes

I’ve installed glibc glibc.dev gcc libgccjit binutils .

What does that mean to you? Adding libraries to environment.systemPackages or home.packages or similar does nothing but wasting space.

This is not how NixOS works. Just use devshells.

2 Likes

I occasionally solve coding problems and participate in contests, so I sometimes need these compilers available alongside VS Code. My main goal is to have VS Code automatically detect these libraries. Is there a recommended way to make this work seamlessly on NixOS without having to launch the IDE from a devshell every time?

There is a way, but it isn’t recommended, as it basically defeats the purpose of nix.

The essence is to set a variety of environment variables that setup hooks would set for you in a shell and/or create symlinks at certain (default) locations.

The recommended way is to use devshells though, or a docker container/VM that works the way you want it to work.

Personally I have no problems with drive by contributions and ad-hoc shells or uncommitted shell.nixs.


PS: yes, I know, it can feel like an annoying limitation at the beginning of the journey, though once you got used to it, it really becomes a major strength of the ecosystem, in my opinion.

Thanks for the explanation! I appreciate it.

You don’t need these with nix-shell, stdenv includes these.

1 Like