How do I select GCC version in nix-shell?

You need to base your environment on stdenvNoCC, instead of stdenv

That means, you may create a shell.nix or default.nix file in your current directory, defining the environment, and then run nix-shell.

with import <nixpkgs> {}; {
  qpidEnv = stdenvNoCC.mkDerivation {
    name = "my-gcc8-environment";
    buildInputs = [
        gcc8
        go
        ruby_2_4
        gdb
        swig
        # libev
        #...
    ];
  };
}

Now, it works as expected

$ nix-shell
[nix-shell:~]$ gcc --version
gcc (GCC) 8.1.0

Source: https://groups.google.com/forum/#!topic/nix-devel/of6P-sEtQN0


Use nix run instead. It behaves better, and also has nicer progress indicator when downloading packages.

jdanek@nixos ~ % nix run -f channel:nixos-19.03 gcc8
[jdanek@nixos:~]$ gcc --version
gcc (GCC) 8.3.0

Tags:

Nixos