I’m attempting to set a specific version of gcc (gcc 10.x) from with in a flake, but not having much luck.
I’ve tried a number of different things, and I can see the package get loaded ‘pkgs.gcc8’ – loads gcc8 package, ‘pkgs.gcc10’ – loads gcc10 package and so on, however, executing ‘gcc --version’ at the command-line always produces (11.3.0):
gcc (GCC) 11.3.0
Copyright (C) 2021 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.
setup is the basic build script that most nixpkgs packages use to build. Even it won’t be available in $PATH in your shell because it’s not in the bin/ subdirectory. I’m not sure if a nested list will actually work with buildInputs, either. I find it surprising this evaluates.
Any particular reason you’re not using pkgs.gcc10? If you literally want every package that is available in the normal nix stdenv, you could do something like this:
# Note, I can't help myself, I cleaned this up a bit
devShell = pkgs.mkShell {
# Typically you want `packages` for `mkShell`, unless I guess
# you want to do cross compilation by hand? Does that even
# work with `mkShell`?
packages = with pkgs;[
cmake
cmakeWithGui
libv4l
ocl-icd
freeglut
libgeotiff
libusb1
libdc1394
gtk3
cudaPackages_11_2.cudatoolkit
]
++ gcc10Stdenv.defaultBuildInputs
++ gcc10Stdenv.defaultNativeBuildInputs;
};
But there’s basically only patchelf, gcc10 and a lot of very specific
scripts in there:
I did try ‘pkgs.gcc10’ — and I can see that it gets installed — however , when you check the version on the command-line ‘gcc -version’; it always shows ‘11.3’.
I was digging around more after my cry for help — and I came across this on the NixOS C Wiki (https://nixos.wiki/wiki/C) — but not sure how to apply it within a flake.
Use a different compiler version
Adding a different c compiler to buildInputs in a nix expression will not change the default compiler available in $PATH. Instead, nixpkgs provides a several alternative stdenv which you can search with nix search stdenv so for example:
Available gcc based stdenv variants: gcc{49,6-12}Stdenv, gccMultiStdenv (32bit/64bit mixed support)
Available clang based stdenv variants: llvmPackages_[5-13].{stdenv,libcxxStdenv}, clangMultiStdenv (32bit/64bit mixed support)
Those stdenv instances can be also constructed using the overrideCC function: Here we are creating a shell environment that will always have the latest available gcc:
(overrideCC stdenv gcc_latest).mkDerivation {
name = "env";
}
Note that this will only affect compiler and not the used linker. To overwrite the linker and maybe also the used libc Check out the wrapCCWith example in the next section.
tlater ~/Scratch $ cat flake.nix
{
description = "Scratch dir for various testing";
outputs = {nixpkgs, ...}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [
gcc10
];
};
};
}
tlater ~/Scratch $ nix develop
[tlater@ct-lt-02052:~/Scratch]$ gcc --version
gcc (GCC) 10.3.0
Copyright (C) 2020 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.
[tlater@ct-lt-02052:~/Scratch]$
exit
tlater ~/Scratch $ gcc --version
gcc (GCC) 11.3.0
Copyright (C) 2021 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.
What nix version are you using? Any shell extensions? Are you using bash and it’s loading some kind of custom .bashrc? What if you use --ignore-environment?
I applied your example as-is and it worked like a champ. Not entirely sure what I was doing differently as “thought” I did something similar, but at least I got it working.