Using gcc 14 with nix

I’m probably doing something wrong but when I try running a shell with GCC 14 I get

nix-shell -p gcc14 which --pure
these 46 paths will be fetched (120.87 MiB download, 553.39 MiB unpacked):
  /nix/store/x9d0zrgk4pb8jwb8y441ass46yvqg8rj-acl-2.3.2
  /nix/store/pq16kam01xzd6a46gavx9z36f652pyjl-attr-2.5.2
  /nix/store/4vdlf7sg4cdv6xlfmhyi3ziir3s8yxsg-bash-interactive-5.2p26
  /nix/store/wwfrj9kvfi14xclc38qfwm71ah6aawdh-binutils-2.41
  /nix/store/ylmcy0liyq86s2qpbjirqf8hxl1agqcp-binutils-2.41-lib
  /nix/store/9a1vr0ms2qkdm09bhsw1gfs2d2m6h7ay-binutils-wrapper-2.41
  /nix/store/l46fjkzva0bhvy9p2r7p4vi68kr7a1db-binutils-wrapper-2.41
  /nix/store/cyc3v8qfkhn4r38a8s5d7f2c33q624mz-bzip2-1.0.8-bin
  /nix/store/i7qhgc0bs725qw3wdanznfkdna4z2ns2-coreutils-9.5
  /nix/store/j4gkc44c1pwl5ccgxm83s4r746bsdcw9-diffutils-3.10
  /nix/store/6mx7gxk1w5j7573p1pdb0dcmpxw2r2il-ed-1.20.2
  /nix/store/sca7v6jwxf7zjk6z9nrvc3xzr5nnkf93-expand-response-params
  /nix/store/yjll8dlg38qdh9gw6bk8rsyq80jpfw3c-expand-response-params
  /nix/store/qcqmiq1mb3pkk2bxbj6d6gb2fk9knk8l-file-5.45
  /nix/store/rr1yixvn0z63mgq9s04ig9j9qlz23s2g-findutils-4.9.0
  /nix/store/hkx0wcm23i9ihqlysri8n41kl232kawb-gawk-5.2.2
  /nix/store/zc0nsv23pakbafngjy32kvhfzb16as43-gcc-13.3.0
  /nix/store/inmbhf61kvg8fkz8aak9vvp7r8cz2y12-gcc-14.1.0
  /nix/store/p8cxwb4ll9rm3wxlw2vzkygfbkmrrra9-gcc-14.1.0-lib
  /nix/store/v4snvqw3xrfzh67b8in4c58djpasrnl2-gcc-14.1.0-libgcc
  /nix/store/mpm3i0sbqc9svfch6a17179fs64dz2kv-gcc-wrapper-13.3.0
  /nix/store/piz0jc0js7xnnka355n2yw07zj7p2hgq-gcc-wrapper-14.1.0
  /nix/store/082x03cmpnsqkfp4ljrhsadz68rh3q1h-glibc-2.39-52-bin
  /nix/store/4vgk1rlzdqjnpjicb5qcxjcd4spi7wyw-glibc-2.39-52-dev
  /nix/store/b8r00mlcw1zd8cx13466mcm6p9vrwn2v-gmp-6.3.0
  /nix/store/l8n33v5ilhxakmp328pykqrawbmksgb2-gmp-with-cxx-6.3.0
  /nix/store/vgzmgmppvpb6gqlc56jhcn007cjn7xg9-gmp-with-cxx-6.3.0
  /nix/store/mzg9fi1jl69kvf979axsbfsi1wzay53c-gnu-config-2024-01-01
  /nix/store/md9apn3290h7kv0x198ihaaa3k6icg4b-gnugrep-3.11
  /nix/store/18z454gyz0wpb641rw6gpqk0vi4wbxy6-gnumake-4.4.1
  /nix/store/ks6c62g0m3gqrs5i7m0cv6d6aqhdvirn-gnused-4.9
  /nix/store/95ljdxg4drk1iq8jkjfq2c0z5vbwv8vm-gnutar-1.35
  /nix/store/nc9lq1lra01932rfyclq3gsh82cxbmii-gzip-1.13
  /nix/store/kf0ymg90210qml1f0m6bxsxx3ld4mw9i-isl-0.20
  /nix/store/qq2bnp6misi0m6vw3r8xb0vhri9rlhj9-isl-0.20
  /nix/store/cb62dki23j0sss0di7f2ka0mvlwkzs0j-libmpc-1.3.1
  /nix/store/cz1mgn5ircgw3x9709mwnbpli32b0f18-libmpc-1.3.1
  /nix/store/q9jim72sac8rdnhyxrf4z0qj9vc6446w-linux-headers-6.9
  /nix/store/42kwj5gyk7akihbpn73q69r7pxn00avg-mpfr-4.2.1
  /nix/store/cyzjdgdvk169sn93i3a42k98s520jh63-mpfr-4.2.1
  /nix/store/r05c0lpbnjc8dg3rrr3ck7s07pjy86j3-patch-2.7.6
  /nix/store/pdqndw2kgjv8l3kd5ii0c833jqmxdanq-patchelf-0.15.0
  /nix/store/lqscz8qiafcc6vha719rq7ka8yv2v4rb-pcre2-10.43
  /nix/store/d3dzfy4amjl826fb8j00qp1d9887h7hm-stdenv-linux
  /nix/store/zls41s2455b6pw5d35sslf6v3gvz9349-update-autotools-gnu-config-scripts-hook
  /nix/store/qqhrymypl970jc6npvi9a6sikhr84mdf-xz-5.4.6-bin
copying path '/nix/store/4vdlf7sg4cdv6xlfmhyi3ziir3s8yxsg-bash-interactive-5.2p26' from 'https://cache.nixos.org'...
copying path '/nix/store/sca7v6jwxf7zjk6z9nrvc3xzr5nnkf93-expand-response-params' from 'https://cache.nixos.org'...
copying path '/nix/store/hkx0wcm23i9ihqlysri8n41kl232kawb-gawk-5.2.2' from 'https://cache.nixos.org'...
copying path '/nix/store/v4snvqw3xrfzh67b8in4c58djpasrnl2-gcc-14.1.0-libgcc' from 'https://cache.nixos.org'...
copying path '/nix/store/mzg9fi1jl69kvf979axsbfsi1wzay53c-gnu-config-2024-01-01' from 'https://cache.nixos.org'...
copying path '/nix/store/q9jim72sac8rdnhyxrf4z0qj9vc6446w-linux-headers-6.9' from 'https://cache.nixos.org'...
copying path '/nix/store/pq16kam01xzd6a46gavx9z36f652pyjl-attr-2.5.2' from 'https://cache.nixos.org'...
copying path '/nix/store/cyc3v8qfkhn4r38a8s5d7f2c33q624mz-bzip2-1.0.8-bin' from 'https://cache.nixos.org'...
copying path '/nix/store/6mx7gxk1w5j7573p1pdb0dcmpxw2r2il-ed-1.20.2' from 'https://cache.nixos.org'...
copying path '/nix/store/yjll8dlg38qdh9gw6bk8rsyq80jpfw3c-expand-response-params' from 'https://cache.nixos.org'...
copying path '/nix/store/qcqmiq1mb3pkk2bxbj6d6gb2fk9knk8l-file-5.45' from 'https://cache.nixos.org'...
copying path '/nix/store/082x03cmpnsqkfp4ljrhsadz68rh3q1h-glibc-2.39-52-bin' from 'https://cache.nixos.org'...
copying path '/nix/store/18z454gyz0wpb641rw6gpqk0vi4wbxy6-gnumake-4.4.1' from 'https://cache.nixos.org'...
copying path '/nix/store/ks6c62g0m3gqrs5i7m0cv6d6aqhdvirn-gnused-4.9' from 'https://cache.nixos.org'...
copying path '/nix/store/nc9lq1lra01932rfyclq3gsh82cxbmii-gzip-1.13' from 'https://cache.nixos.org'...
copying path '/nix/store/pdqndw2kgjv8l3kd5ii0c833jqmxdanq-patchelf-0.15.0' from 'https://cache.nixos.org'...
copying path '/nix/store/ylmcy0liyq86s2qpbjirqf8hxl1agqcp-binutils-2.41-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/b8r00mlcw1zd8cx13466mcm6p9vrwn2v-gmp-6.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/l8n33v5ilhxakmp328pykqrawbmksgb2-gmp-with-cxx-6.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/vgzmgmppvpb6gqlc56jhcn007cjn7xg9-gmp-with-cxx-6.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/x9d0zrgk4pb8jwb8y441ass46yvqg8rj-acl-2.3.2' from 'https://cache.nixos.org'...
copying path '/nix/store/lqscz8qiafcc6vha719rq7ka8yv2v4rb-pcre2-10.43' from 'https://cache.nixos.org'...
copying path '/nix/store/r05c0lpbnjc8dg3rrr3ck7s07pjy86j3-patch-2.7.6' from 'https://cache.nixos.org'...
copying path '/nix/store/zls41s2455b6pw5d35sslf6v3gvz9349-update-autotools-gnu-config-scripts-hook' from 'https://cache.nixos.org'...
copying path '/nix/store/p8cxwb4ll9rm3wxlw2vzkygfbkmrrra9-gcc-14.1.0-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/qqhrymypl970jc6npvi9a6sikhr84mdf-xz-5.4.6-bin' from 'https://cache.nixos.org'...
copying path '/nix/store/95ljdxg4drk1iq8jkjfq2c0z5vbwv8vm-gnutar-1.35' from 'https://cache.nixos.org'...
copying path '/nix/store/kf0ymg90210qml1f0m6bxsxx3ld4mw9i-isl-0.20' from 'https://cache.nixos.org'...
copying path '/nix/store/cyzjdgdvk169sn93i3a42k98s520jh63-mpfr-4.2.1' from 'https://cache.nixos.org'...
copying path '/nix/store/i7qhgc0bs725qw3wdanznfkdna4z2ns2-coreutils-9.5' from 'https://cache.nixos.org'...
copying path '/nix/store/qq2bnp6misi0m6vw3r8xb0vhri9rlhj9-isl-0.20' from 'https://cache.nixos.org'...
copying path '/nix/store/42kwj5gyk7akihbpn73q69r7pxn00avg-mpfr-4.2.1' from 'https://cache.nixos.org'...
copying path '/nix/store/wwfrj9kvfi14xclc38qfwm71ah6aawdh-binutils-2.41' from 'https://cache.nixos.org'...
copying path '/nix/store/md9apn3290h7kv0x198ihaaa3k6icg4b-gnugrep-3.11' from 'https://cache.nixos.org'...
copying path '/nix/store/cb62dki23j0sss0di7f2ka0mvlwkzs0j-libmpc-1.3.1' from 'https://cache.nixos.org'...
copying path '/nix/store/cz1mgn5ircgw3x9709mwnbpli32b0f18-libmpc-1.3.1' from 'https://cache.nixos.org'...
copying path '/nix/store/j4gkc44c1pwl5ccgxm83s4r746bsdcw9-diffutils-3.10' from 'https://cache.nixos.org'...
copying path '/nix/store/rr1yixvn0z63mgq9s04ig9j9qlz23s2g-findutils-4.9.0' from 'https://cache.nixos.org'...
copying path '/nix/store/4vgk1rlzdqjnpjicb5qcxjcd4spi7wyw-glibc-2.39-52-dev' from 'https://cache.nixos.org'...
copying path '/nix/store/zc0nsv23pakbafngjy32kvhfzb16as43-gcc-13.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/inmbhf61kvg8fkz8aak9vvp7r8cz2y12-gcc-14.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/l46fjkzva0bhvy9p2r7p4vi68kr7a1db-binutils-wrapper-2.41' from 'https://cache.nixos.org'...
copying path '/nix/store/9a1vr0ms2qkdm09bhsw1gfs2d2m6h7ay-binutils-wrapper-2.41' from 'https://cache.nixos.org'...
copying path '/nix/store/mpm3i0sbqc9svfch6a17179fs64dz2kv-gcc-wrapper-13.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/d3dzfy4amjl826fb8j00qp1d9887h7hm-stdenv-linux' from 'https://cache.nixos.org'...
copying path '/nix/store/piz0jc0js7xnnka355n2yw07zj7p2hgq-gcc-wrapper-14.1.0' from 'https://cache.nixos.org'...

showing GCC 13 as well as 14 were installed; however the gcc in the resulting environment is GCC 13, although coming from nix:

[nix-shell:~]$ gcc --version
gcc (GCC) 13.3.0
Copyright (C) 2023 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:~]$ which gcc
/nix/store/mpm3i0sbqc9svfch6a17179fs64dz2kv-gcc-wrapper-13.3.0/bin/gcc

What am I doing wrong/misunderstanding?

Normally nix-shell provides the dependencies to build a package, rather than the package itself.

https://nix.dev/manual/nix/2.24/command-ref/nix-shell.html

   The  command nix-shell will build the dependencies of the specified derivation, but not the derivation itself. It will
   then start an interactive shell in which all environment variables defined by the derivation path  have  been  set  to
   their corresponding values, and the script $stdenv/setup has been sourced. This is useful for reproducing the environ‐
   ment of a derivation for development.

But when you use the -p (or --packages) option, AFAIK it creates a new derivation/package with your package(s) as dependency. I think it does something like this:

nix-shell --expr 'let pkgs = (import <nixpkgs> {}); in (pkgs.stdenv.mkDerivation { name = "nix-shell"; unpackSource = false; buildInputs = [pkgs.gcc14]; })'

EDIT found what it does exactly, pretty close: nix/src/nix-build/nix-build.cc at 48477d4a3e7130c89b2ded4496c00ef74601091f · NixOS/nix · GitHub

nix-shell --expr 'let pkgs = (import <nixpkgs> {}); in (pkgs.runCommandCC "shell" {buildInputs = [pkgs.gcc14]; })'

And pkgs.stdenv already depends on gcc (13), which somehow gets first in PATH variable. If you check the PATH variable, you’ll see both gcc 13 and 14.

To get gcc14 you could do something like this, avoiding stdenv which has gcc:

nix-shell --expr 'let pkgs = (import <nixpkgs> {}); in (pkgs.mkShell { nativeBuildInputs = [pkgs.gcc14]; })'

Or use a shell.nix file: Declarative shell environments with shell.nix — nix.dev documentation

This also works, so probably nativeBuildInputs gets put first in the PATH variable:

nix-shell --expr 'let pkgs = (import <nixpkgs> {}); in (pkgs.stdenv.mkDerivation { name = "nix-shell"; unpackSource = false; nativeBuildInputs = [pkgs.gcc14]; })'

EDIT 2:

There’s already a bug open for this: gcc always present in nix-shell and overrides clang · Issue #5117 · NixOS/nix · GitHub

1 Like

Well you should override the stdenv if you want to change the C compiler, and just use packages if you need some inputs.

nix-shell --expr 'let pkgs = (import <nixpkgs> {}); in (pkgs.mkShell.override { stdenv = pkgs.gcc14Stdenv; } { packages = [ ... ]; })

(replace … with whatever packages you want to use)

This looks a little tortured, so yeah a shell.nix would be easier to read.

let
  pkgs = import <nixpkgs> { };
in
pkgs.mkShell.override { stdenv = pkgs.gcc14Stdenv; } {
  packages = [
    # add stuff here
  ];
}
1 Like