Packaging a custom GCC

I would like to build a custom GCC package from the existing nixpkgs definitions but using a git repository with my own patches as a source.

Is there a reasonably straightforward way to do this?

I know how to do something similar for LLVM, for example:

{
  nixpkgs.overlays = [
    (final: prev: {
      custom-toolchain = {
        llvmPackages = prev.llvmPackages_git.override {
          gitRelease = null;
          officialRelease = {
            version = prev.llvmPackages_git.release_version;
            rev-version = "19.0.0-custom-toolchain";
          };
          monorepoSrc = prev.fetchFromGitHub {
            ...
          };
        };
      };
    })
  ];
}

I’ve tried to look through the definitions for the gcc packages but I don’t see any place where I can override the source in a similar way.

Turns out you can do it if you modify pkgs.gccFun to allow specifying a version and src and overriding the version mapping and hash selection logic, then you end up with this:

{
  nixpkgs.overlays = [
    (_final: prev: {
      custom-toolchain = {
        gcc = prev.wrapCC (prev.gccFun {
          noSysDirs = false;
          majorMinorVersion = "14.0.0";
          exactVersion = "14.0.0";
          reproducibleBuild = true;
          profiledCompiler = false;
          libcCross = null;
          threadsCross = { };
          isl = prev.isl_0_20;
          cloog = prev.cloog_0_18_0;
          inherit (prev) stdenv;
          src = prev.fetchFromGitHub {
            ...
          };
        });
      };
    })
  ];
}

Although this seems to get close to what I am looking for, there are some build errors I’m trying to track down (that also seem to happen with the release gcc 13.2.0 from git).

If anyone has ideas about how to handle this better I would be interested to hear about it.