OverrideCC Will Lose CMake Flags and Cannot Link Properly for Cross Compiling

Hi. I’ve been trying to replace the bundled glibc (2.34) from the wrapped gcc 10.3.0 with a newer one (2.35). After days of investigations I’m getting to a point where derivations shown in nix repl seem to be OK (Gist link below):

    stdenv = with pkgs; overrideCC buildPackages.stdenv (wrapCCWith {
      cc = buildPackages.gcc10.cc;
      libc = glibcPkgs.buildPackages.gcc10.libc;
      bintools = glibcPkgs.binutils.override {
        libc = glibcPkgs.buildPackages.gcc10.bintools.libc;
      };
    });

The cross compiler:
stdenv.cc.cc = aarch64-unknown-linux-gnu-stage-final-gcc-debug-10.3.0
where I can find the executable aarch64-unknown-linux-gnu-gcc.

And the libc:
stdenv.cc.libc = glibc-aarch64-unknown-linux-gnu-2.35-163.drv

However, this setup will confuse CMake setup and result in wrong flags (buildlog_badcc.txt):

-DCMAKE_C_COMPILER=cc
-DCMAKE_CXX_COMPILER=c++

If I add a cmakeFlags attribute to tell this is a cross-compiling project, it is then having trouble linking the standard C library (buildlog_cmakeFlags.txt).

I have also tried using both gcc and glibc from pkgsCross:

    stdenv = with pkgs; overrideCC pkgsCross.aarch64-multiplatform.stdenv (wrapCCWith {
      cc = pkgsCross.aarch64-multiplatform.gcc10.cc;
      libc = glibcPkgs.pkgsCross.aarch64-multiplatform.gcc10.libc;
      bintools = glibcPkgs.pkgsCross.aarch64-multiplatform.binutils.override {
        libc = glibcPkgs.pkgsCross.aarch64-multiplatform.buildPackages.gcc10.bintools.libc;
      };
    });

Unfortunately, gcc10 cannot build in this configuration (buildlog_pkgsCross.txt). What’s the difference between the two different derivations of gcc?

Here is the full (GitHub Gist) including the stripped flake.nix, default.nix and build logs. Has anyone run into similar use cases as I do?

Thanks everyone.