Can't build GCC for the riscv32 target

Hello, I’m trying to build binutils and gcc (deliberately locally from source) for baremetal riscv32. However, at some step beyond my control, gcc default.nix is “run” with targetPlatform set to x64, apparently as caused by libgcc build files. I’m on unstable and my nixpkgs git checkout is fresh.

{ pkgs ? (import ./nixpkgs { }), stdenv ? pkgs.stdenv, lib ? pkgs.lib
, callPackage ? pkgs.callPackage }:
let
  targetstdenv = stdenv.override {targetPlatform = lib.systems.elaborate "riscv32-unknown-none-elf";};
  bintools = callPackage ./nixpkgs/pkgs/development/tools/misc/binutils {
    noSysDirs = false;
    stdenv = targetstdenv;
  };
in {
  inherit bintools;
  inherit targetstdenv;
  gcc = callPackage ./nixpkgs/pkgs/development/compilers/gcc {
    binutils = bintools;
    majorMinorVersion = "13";
    noSysDirs = false;
    withoutTargetLibc = true;
    stdenv = targetstdenv;
  };
  foo = "foo";
}
error:
       … from call site

         at /home/emil/work/riscv-nix/buildme.nix:12:9:

           11|   inherit targetstdenv;
           12|   gcc = callPackage ./nixpkgs/pkgs/development/compilers/gcc {
             |         ^
           13|     binutils = bintools;

       … while calling 'callPackageWith'

         at /home/emil/work/riscv-nix/nixpkgs/lib/customisation.nix:122:35:

          121|   */
          122|   callPackageWith = autoArgs: fn: args:
             |                                   ^
          123|     let

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/lib/customisation.nix:173:34:

          172|
          173|     in if missingArgs == [] then makeOverridable f allArgs else abort error;
             |                                  ^
          174|

       … while calling 'makeOverridable'

         at /home/emil/work/riscv-nix/nixpkgs/lib/customisation.nix:72:24:

           71|   */
           72|   makeOverridable = f: origArgs:
             |                        ^
           73|     let

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/lib/customisation.nix:74:16:

           73|     let
           74|       result = f origArgs;
             |                ^
           75|

       … while calling anonymous lambda

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/default.nix:1:1:

            1| { lib, stdenv, targetPackages, fetchurl, fetchpatch, noSysDirs
             | ^
            2| , langC ? true, langCC ? true, langFortran ? false

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/default.nix:236:1:

          235|
          236| lib.pipe ((callFile ./common/builder.nix {}) ({
             | ^
          237|   pname = "${crossNameAddon}${name}";

       … while calling 'pipe'

         at /home/emil/work/riscv-nix/nixpkgs/lib/trivial.nix:61:15:

           60|   */
           61|   pipe = val: functions:
             |               ^
           62|     let reverseApply = x: f: f x;

       … while calling 'reverseApply'

         at /home/emil/work/riscv-nix/nixpkgs/lib/trivial.nix:62:27:

           61|   pipe = val: functions:
           62|     let reverseApply = x: f: f x;
             |                           ^
           63|     in builtins.foldl' reverseApply val functions;

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/lib/trivial.nix:430:7:

          429|     { # TODO: Should we add call-time "type" checking like built in?
          430|       __functor = self: f;
             |       ^
          431|       __functionArgs = args;

       … while calling anonymous lambda

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/common/libgcc.nix:16:1:

           15|
           16| drv: lib.pipe drv
             | ^
           17|

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/common/libgcc.nix:16:6:

           15|
           16| drv: lib.pipe drv
             |      ^
           17|

       … while calling 'pipe'

         at /home/emil/work/riscv-nix/nixpkgs/lib/trivial.nix:61:15:

           60|   */
           61|   pipe = val: functions:
             |               ^
           62|     let reverseApply = x: f: f x;

       … while calling 'reverseApply'

         at /home/emil/work/riscv-nix/nixpkgs/lib/trivial.nix:62:27:

           61|   pipe = val: functions:
           62|     let reverseApply = x: f: f x;
             |                           ^
           63|     in builtins.foldl' reverseApply val functions;

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/lib/trivial.nix:62:30:

           61|   pipe = val: functions:
           62|     let reverseApply = x: f: f x;
             |                              ^
           63|     in builtins.foldl' reverseApply val functions;

       … while calling anonymous lambda

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/common/libgcc.nix:69:2:

           68|
           69| (pkg: pkg.overrideAttrs (previousAttrs: lib.optionalAttrs useLibgccFromTargetLibc {
             |  ^
           70|   passthru = (previousAttrs.passthru or {}) // {

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/common/libgcc.nix:69:7:

           68|
           69| (pkg: pkg.overrideAttrs (previousAttrs: lib.optionalAttrs useLibgccFromTargetLibc {
             |       ^
           70|   passthru = (previousAttrs.passthru or {}) // {

       … while calling 'overrideAttrs'

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:24:23:

           23|
           24|       overrideAttrs = f0:
             |                       ^
           25|         let

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:43:11:

           42|         in
           43|           makeDerivationExtensible
             |           ^
           44|             (self: let super = rattrs self; in super // (if builtins.isFunction f0 || f0?__functor then f self super else f0));

       … while calling 'makeDerivationExtensible'

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:14:30:

           13|   # Based off lib.makeExtensible, with modifications:
           14|   makeDerivationExtensible = rattrs:
             |                              ^
           15|     let

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:21:14:

           20|       # An infinite recursion here can be caused by having the attribute names of expression `e` in `.overrideAttrs(finalAttrs: previousAttrs: e)` depend on `finalAttrs`. Only the attribute values of `e` can depend on `finalAttrs`.
           21|       args = rattrs (args // { inherit finalPackage overrideAttrs; });
             |              ^
           22|       #              ^^^^

       … while calling anonymous lambda

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:44:14:

           43|           makeDerivationExtensible
           44|             (self: let super = rattrs self; in super // (if builtins.isFunction f0 || f0?__functor then f self super else f0));
             |              ^
           45|

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:44:105:

           43|           makeDerivationExtensible
           44|             (self: let super = rattrs self; in super // (if builtins.isFunction f0 || f0?__functor then f self super else f0));
             |                                                                                                         ^
           45|

       … while calling 'f'

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:26:21:

           25|         let
           26|           f = self: super:
             |                     ^
           27|             # Convert f0 to an overlay. Legacy is:

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:34:21:

           33|             # arise from flipping an overlay's parameters in some cases.
           34|             let x = f0 super;
             |                     ^
           35|             in

       … while calling anonymous lambda

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/common/libgcc.nix:69:26:

           68|
           69| (pkg: pkg.overrideAttrs (previousAttrs: lib.optionalAttrs useLibgccFromTargetLibc {
             |                          ^
           70|   passthru = (previousAttrs.passthru or {}) // {

       … from call site

         at /home/emil/work/riscv-nix/nixpkgs/pkgs/development/compilers/gcc/common/libgcc.nix:69:41:

           68|
           69| (pkg: pkg.overrideAttrs (previousAttrs: lib.optionalAttrs useLibgccFromTargetLibc {
             |                                         ^
           70|   passthru = (previousAttrs.passthru or {}) // {

       … while calling 'optionalAttrs'

         at /home/emil/work/riscv-nix/nixpkgs/lib/attrsets.nix:679:5:

          678|     # The attribute set to return if `cond` is `true`.
          679|     as:
             |     ^
          680|     if cond then as else {};

       error: assertion '((stdenv).targetPlatform != (stdenv).buildPlatform)' failed

       at /home/emil/work/riscv-nix/nixpkgs/pkgs/top-level/all-packages.nix:21798:15:

        21797|
        21798|   libcCross = assert stdenv.targetPlatform != stdenv.buildPlatform; libcCrossChooser stdenv.targetPlatform.libc;
             |               ^
        21799|

I have found out I could almost use this oneliner:
NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 NIX_PATH=./ nix build -f "<nixpkgs>" pkgsCross.riscv32-embedded.gcc
Unfortunately the build fails:

In file included from shell.h:42:
externs.h:295:12: error: conflicting types for 'gethostname'; have 'int(char *, int)'
  295 | extern int gethostname PARAMS((char *, int));
      |            ^~~~~~~~~~~
In file included from /nix/store/pg5j9wz7y6q6ya817d0998sj2z2n5qm0-riscv32-none-elf-stage-final-gcc-12.3.0/riscv32-none-elf/sys-include/unistd.h:4,
                 from copy_cmd.c:28:
/nix/store/pg5j9wz7y6q6ya817d0998sj2z2n5qm0-riscv32-none-elf-stage-final-gcc-12.3.0/riscv32-none-elf/sys-include/sys/unistd.h:286:9: note: previous declaration of 'gethostname' with type 'int(char *, size_t)' {ak>
  286 |  int    gethostname (char *__name, size_t __len);
      |         ^~~~~~~~~~~
make: *** [Makefile:106: copy_cmd.o] Error 1

I probably have to understand what I have to do to get rid of nix patches first. https://trofi.github.io/posts/289-building-vanilla-gcc-on-nixos.html

No, the problem is that for some reason, when I want to build GCC to cross compile for RISC-V, it builds bash for RISC-V, which fails, probably since it’s a freestanding target