Can we use overrideAttrs to change the src of GCC?

Hello all,

I need to build a project using GCC9.3 which I would like to build from source (as a learning exercise).
Following is the closest I was able to get to a solution, but the build fails.

Would anyone be willing to take a look and show me the Nix way to do this?

{
  description = "";

  inputs.nixpkgs.url = github:NixOS/nixpkgs?ref=ddb3455e152edb633e171d51a5c1f12ae8d22641;

  outputs = {self, nixpkgs, ...}:
  {
    packages."x86_64-linux" = with nixpkgs.legacyPackages."x86_64-linux"; {
      default = stdenv.mkDerivation {
        pname = "foo";
        version = "0.1.0";
        src = ./src;

        nativeBuildInputs = [
          (gcc.cc.overrideAttrs (old: {
            version = "9.3.0";
            src = builtins.fetchTarball {
              url = "https://github.com/gcc-mirror/gcc/archive/refs/tags/releases/gcc-9.3.0.tar.gz";
              sha256 = "0ahcsg4ymfxar9g8i3r5akmhpnswfdxfd9cpr4bar0zdza08xdym";
            };
          }))
        ];
        buildPhase = ''
          echo $(g++ --version)
          g++ -o foo foo.cpp
        '';

        installPhase = ''
          mkdir -p $out/bin
          cp foo $out/bin
        '';
      };
    };
  };
}

Following is the error I get when building Gcc

$CPATH is `/nix/store/5604wq5idbgniqchzal2pjmjgv9ns1kb-zlib-1.2.13-dev/include'
$LIBRARY_PATH is `/nix/store/37a5krk4a1a8vhl93q2bm9nbv8hymyii-zlib-1.2.13/lib'
/nix/store/qr60k7sz61hvv2baadlfv5qjr8q7vlg6-builder.sh: line 40: /nix/store/9sz03jz5kjgsvwkws2vzb0qq23dfgjhl-binutils-patchelfed-ld-wrapper-2.40/nix-support/libc-ldflags-before: No such file or directory
/nix/store/qr60k7sz61hvv2baadlfv5qjr8q7vlg6-builder.sh: line 40: /nix/store/9sz03jz5kjgsvwkws2vzb0qq23dfgjhl-binutils-patchelfed-ld-wrapper-2.40/nix-support/libc-ldflags-before: No such file or directory
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/dsf3x5giy4z5clmw4f1h35pfg4gmpv6c-source
source root is source
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
applying patch /nix/store/9577hmdlmhki67cg8ar85cvidyg7xr7p-gcc-12-no-sys-dirs.patch
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- a/gcc/cppdefault.cc        2013-01-10 21:38:27.000000000 +0100
|+++ b/gcc/cppdefault.cc        2014-08-18 16:20:32.893944536 +0200
--------------------------
File to patch: 
Skip this patch? [y] 
Skipping patch.
1 out of 1 hunk ignored
can't find file to patch at input line 14
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- a/gcc/gcc.cc       2014-03-23 12:30:57.000000000 +0100
|+++ b/gcc/gcc.cc       2014-08-18 13:19:32.689201690 +0200
--------------------------
File to patch: 
Skip this patch? [y] 
Skipping patch.
1 out of 1 hunk ignored

The source is fairly simple cpp which just spits out a string in the terminal.
Any help on this would be really appreciated!

Cheers,
Bhavesh.

You are properly overriding the source, but there’s a bunch of patches that are being applied that of course don’t apply to gcc 9 (because they’re written for gcc 12). You could override patches = []; but frankly I think it’s going to be very difficult to get to a working build through overrides (most likely there’ll be other stuff in the derivation that are specific to gcc 12 and don’t work on gcc 9). It would probably be “easier” to write a new derivation from scratch, but that’s very very far from easy. More like one of the harder Nix projects you could get yourself into…

gcc9 is in nixpkgs, by the way… :slight_smile:

2 Likes

Thanks @delroth , you are correct, I was able to get rid of the erroneous patches which fixed that error but as you mentioned I ran into other errors.

I am working on a project which is meant to be used with gcc9.3 but I can only find 9.5 in nixpkgs. Since I am running into compilation errors, I thought it was worth trying a build with gcc9.3 before opening an issue on Github.

While typing this, I realised I have better chances of success overriding the source for the gcc9.5. I will update the thread if that worked :slight_smile:

Thanks for the help!

@bhaveshpandey You can still build gcc 9.3 from e2de23b6db6c1f36bbf52e953929bb9283c549bf which is the commit before it was bumped to 9.5.

❯ nix-build https://github.com/nixos/nixpkgs/archive/e2de23b6db6c1f36bbf52e953929bb9283c549bf.tar.gz -A gcc9
/nix/store/mc677rxb3hrbv9zmskxv566fjkc6w055-gcc-wrapper-9.3.0
2 Likes

Hey @aforemny , thanks for that…to be honest I had not really thought about this. This did work out for me!

Can you or anyone also show me how I can use the glibc (2.17 I believe) which is compatible with gcc9.3?

How would you override glibc in the mkDerivation?