Cross compile with different version of gcc

I want to use gcc with different version other than the default gcc10 when crossing compile

With the following config:

let
  pkgs = import <nixpkgs> { };
  pkgsCross = import <nixpkgs> {
    crossSystem = { config = "x86_64-unknown-linux-gnu"; };
  };
  localSystem = pkgs.lib.systems.elaborate builtins.currentSystem;
  crossSystem =
    pkgs.lib.systems.elaborate { config = "aarch64-unknown-linux-gnu"; };
  crGcc10 = pkgs.wrapCCWith {
    cc = pkgs.gcc10.cc.override {
      libcCross = pkgsCross.libcCross;
      stdenv = pkgs.stdenv.override {
        hostPlatform = localSystem;
        buildPlatform = localSystem;
        targetPlatform = crossSystem;
      };
    };
  };
in crGcc10

but with no luck, yielding error:

configure: error: cannot execute: /nix/store/fh1a6sp991q60d93nb78h75rq429cl41-binutils-wrapper-2.35.2/bin/aarch64-unknown-linux-gnu-ld: check --with-ld or env. var. DEFAULT_LINKER

There is only ld under that folder, no aarch64-unknown-linux-gnu-ld

I also tried with:

with import <nixpkgs> {
  crossSystem = { config = "x86_64-unknown-linux-gnu"; };
};
let
  localSystem = lib.systems.elaborate builtins.currentSystem;
  crossSystem = lib.systems.elaborate { config = "aarch64-unknown-linux-gnu"; };
  crGcc10 = wrapCCWith {
    cc = gcc10.cc.override {
      stdenv = stdenv.override {
        buildPlatform = localSystem;
        hostPlatform = localSystem;
        targetPlatform = crossSystem;
      };
    };
  };
in crGcc10

According to env-vars, CC is set to x86_64-unknown-linux-gnu-gcc which is unwrapped in PATH. Yielding that config cannot compile anything

Give this a shot:

  pkgsCross = import <nixpkgs> {
    crossSystem = { config = "x86_64-unknown-linux-gnu"; };
    config.replaceStdenv = { pkgs }: pkgs.wrapCCWith {
    cc = pkgs.gcc10.cc.override {
      libcCross = pkgsCross.libcCross;
      stdenv = pkgs.stdenv.override {
        hostPlatform = localSystem;
        buildPlatform = localSystem;
        targetPlatform = crossSystem;
      };
    };
  };

I replaced pkgsCross like that

let
  pkgs = import <nixpkgs> { };
  pkgsCross = import <nixpkgs> {
    crossSystem = { config = "x86_64-unknown-linux-gnu"; };
    config.replaceStdenv = { pkgs }:
      pkgs.wrapCCWith {
        cc = pkgs.gcc10.cc.override {
          libcCross = pkgsCross.libcCross;
          stdenv = pkgs.stdenv.override {
            hostPlatform = localSystem;
            buildPlatform = localSystem;
            targetPlatform = crossSystem;
          };
        };
      };
  };
  localSystem = pkgs.lib.systems.elaborate builtins.currentSystem;
  crossSystem =
    pkgs.lib.systems.elaborate { config = "aarch64-unknown-linux-gnu"; };
  crGcc10 = pkgs.wrapCCWith {
    cc = pkgs.gcc10.cc.override {
      libcCross = pkgsCross.libcCross;
      stdenv = pkgs.stdenv.override {
        hostPlatform = localSystem;
        buildPlatform = localSystem;
        targetPlatform = crossSystem;
      };
    };
  };
in crGcc10

But it is still broken with

configure: error: cannot execute: /nix/store/w327j7z9wlv7hym4spjzagax7c5hqvrf-binutils-wrapper-2.35.2/bin/aarch64-unknown-linux-gnu-ld: check --with-ld or env. var. DEFAULT_LINKER
...
error: builder for '/nix/store/cg18iqxp7w6s4kx481mm9dl374myyz0h-aarch64-unknown-linux-gnu-stage-final-gcc-debug-10.3.0.drv' failed with exit code 2

Same as before

Easiest is to swap out the gcc attribute with an overlay:

import <nixpkgs> { crossSystem = "aarch64-linux"; overlays = [ (self: super: { gcc = self.gcc10; }) ]; })