Need help understand why nix-shell is giving me the wrong GCC version

I’m using NixOS on the unstable channel and don’t have gcc installed by default. No matter what GCC package I try and use, I end up with gcc12. See the following shell commands and output:

[stusmall@desktop:~]$ which gcc
which: no gcc in (/run/wrappers/bin:/home/stusmall/.nix-profile/bin:/nix/profile/bin:/home/stusmall/.local/state/nix/profile/bin:/etc/profiles/per-user/stusmall/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin)

[stusmall@desktop:~]$ nix-shell -p gcc12

[nix-shell:~]$ which gcc
/nix/store/sfgnb6rr428bssyrs54d6d0vv2avi95c-gcc-wrapper-12.3.0/bin/gcc

[nix-shell:~]$ gcc --version
gcc (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[nix-shell:~]$ exit
exit

[stusmall@desktop:~]$ nix-shell -p gcc11

[nix-shell:~]$ which gcc
/nix/store/sfgnb6rr428bssyrs54d6d0vv2avi95c-gcc-wrapper-12.3.0/bin/gcc

[nix-shell:~]$ gcc --version
gcc (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Any idea what’s up with this? Is it a bug? Am I misunderstanding the purpose of the gcc11 package?

Check your path. Inside a nix-shell -p gcc11 I see

But why would gcc12 be in my path at all if I’m using the gcc11 package?

I imagine you’re trying something that is a bit of an edge case for nix-shell.

It’s meant to provide a close approximation of the standard build environment, which in this case will already come with a gcc.

So as bme illustrates you have more than one and will need to discriminate.

@abathur already answered this but as an addendum: nix-shell doesn’t care what packages you add with -p. You are ascribing to it a level of functionality it doesn’t possess. It’s a standard env + whatever else you add. But this is easily solved. Just make a shell.nix with a shellHook and stick your gcc11 at the front of the path. Or set $CC to gcc 11 for your make file, or whatever it is you want to do.

I can’t try it right now to confirm it’s current, but How can I override the stdenv to be gcc11Stdenv with optimisations might describe what you are looking for.

1 Like