HowTo: Build GCC in NixOs and nix flake

I spent today debugging my Nix flake configuration (that is reported at the end of the file) because I am using NixOS on my build machine where I run heavy processes (e.g: running integration tests and so on).

However, I am not able to compile the GCC compiler due to the following error

# @multilib_dir@ is not really necessary, but sometimes it has
# more uses than just a directory name.
/nix/store/xdqlrixlspkks50m9b0mpvag65m3pf2w-bash-5.2-p15/bin/bash ../../../../gittea/gcc/libgcc/../mkinstalldirs .
/home/vincenzopalazzo/build-dir/gcc/./gcc/xgcc -B/home/vincenzopalazzo/build-dir/gcc/./gcc/ -B/home/vincenzopalazzo/build-dir/gcc/build/x86_64-pc-linux-gnu/bin/ -B/home/vincenzopalazzo/build-dir/gcc/build/x86_64-pc-linux-gnu/lib/ -isystem /home/vincenzopalazzo/build-dir/gcc/build/x86_64-pc-linux-gnu/include -isystem /home/vincenzopalazzo/build-dir/gcc/build/x86_64-pc-linux-gnu/sys-include    -O2  -g -O2 -DIN_GCC    -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition  -isystem ./include  -fpic -mlong-double-80 -DUSE_ELF_SYMVER -fcf-protection -mshstk -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector  -shared -nodefaultlibs -Wl,--soname=libgcc_s.so.1 -Wl,--version-script=libgcc.map  -o ./libgcc_s.so.1.tmp -g -O2 -B./ _muldi3_s.o _negdi2_s.o _lshrdi3_s.o _ashldi3_s.o _ashrdi3_s.o _cmpdi2_s.o _ucmpdi2_s.o _clear_cache_s.o _trampoline_s.o __main_s.o _absvsi2_s.o _absvdi2_s.o _addvsi3_s.o _addvdi3_s.o _subvsi3_s.o _subvdi3_s.o _mulvsi3_s.o _mulvdi3_s.o _negvsi2_s.o _negvdi2_s.o _ctors_s.o _ffssi2_s.o _ffsdi2_s.o _clz_s.o _clzsi2_s.o _clzdi2_s.o _ctzsi2_s.o _ctzdi2_s.o _popcount_tab_s.o _popcountsi2_s.o _popcountdi2_s.o _paritysi2_s.o _paritydi2_s.o _powisf2_s.o _powidf2_s.o _powixf2_s.o _mulsc3_s.o _muldc3_s.o _mulxc3_s.o _divsc3_s.o _divdc3_s.o _divxc3_s.o _bswapsi2_s.o _bswapdi2_s.o _clrsbsi2_s.o _clrsbdi2_s.o _fixunssfsi_s.o _fixunsdfsi_s.o _fixunsxfsi_s.o _fixsfdi_s.o _fixdfdi_s.o _fixxfdi_s.o _fixunssfdi_s.o _fixunsdfdi_s.o _fixunsxfdi_s.o _floatdisf_s.o _floatdidf_s.o _floatdixf_s.o _floatundisf_s.o _floatundidf_s.o _floatundixf_s.o _divdi3_s.o _moddi3_s.o _divmoddi4_s.o _udivdi3_s.o _umoddi3_s.o _udivmoddi4_s.o _udiv_w_sdiv_s.o cpuinfo_s.o sfp-exceptions_s.o _divhc3_s.o _mulhc3_s.o addtf3_s.o divtf3_s.o multf3_s.o negtf2_s.o subtf3_s.o unordtf2_s.o fixtfsi_s.o fixunstfsi_s.o floatsitf_s.o floatunsitf_s.o fixtfdi_s.o fixunstfdi_s.o floatditf_s.o floatunditf_s.o fixtfti_s.o fixunstfti_s.o floattitf_s.o floatuntitf_s.o extendhfsf2_s.o extendhfdf2_s.o extendhftf2_s.o extendhfxf2_s.o extendsfdf2_s.o extendsftf2_s.o extenddftf2_s.o extendxftf2_s.o extendbfsf2_s.o trunctfhf2_s.o truncxfhf2_s.o truncdfhf2_s.o truncsfhf2_s.o trunctfsf2_s.o truncdfsf2_s.o trunctfdf2_s.o trunctfxf2_s.o trunctfbf2_s.o truncxfbf2_s.o truncdfbf2_s.o truncsfbf2_s.o trunchfbf2_s.o fixhfti_s.o fixunshfti_s.o floattihf_s.o floatuntihf_s.o floattibf_s.o floatuntibf_s.o eqhf2_s.o getf2_s.o letf2_s.o eqtf2_s.o _divtc3_s.o _multc3_s.o _powitf2_s.o enable-execute-stack_s.o unwind-dw2_s.o unwind-dw2-fde-dip_s.o unwind-sjlj_s.o unwind-c_s.o emutls_s.o libgcc.a -lc && rm -f ./libgcc_s.so && if [ -f ./libgcc_s.so.1 ]; then mv -f ./libgcc_s.so.1 ./libgcc_s.so.1.backup; else true; fi && mv ./libgcc_s.so.1.tmp ./libgcc_s.so.1 && (echo "/* GNU ld script"; echo "   Use the shared library, but some functions are only in"; echo "   the static library.  */"; echo "GROUP ( libgcc_s.so.1 -lgcc )" ) > ./libgcc_s.so
/nix/store/74y3751gsixaz9797ib0hp7c658sp1y5-binutils-2.40/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:998: libgcc_s.so] Error 1
make[2]: Leaving directory '/home/vincenzopalazzo/build-dir/gcc/x86_64-pc-linux-gnu/libgcc'
make[1]: *** [Makefile:14113: all-target-libgcc] Error 2
make[1]: Leaving directory '/home/vincenzopalazzo/build-dir/gcc'
make: *** [Makefile:1025: all] Error 2

I ran the compilation as mentioned InstallingGCC - GCC Wiki in a separate repository, but I had to do an --with-native-system-header-dir=SBUILD_SYSTEM_HEADER_DIR hack (I do not know if I am missing something)

../../gittea/gcc/configure --prefix=/home/vincenzopalazzo/build-dir/gcc/build --disable-bootstrap --disable-multilib --enable-languages=rust --with-native-system-header-dir=$BUILD_SYSTEM_HEADER_DIR && make -j$(nproc)

Do you have any thoughts on what I am missing or doing wrong?


{
  description = "gcc compiler";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = nixpkgs.legacyPackages.${system};
      in {
        packages = {
          default = pkgs.gnumake;
        };
        formatter = pkgs.nixpkgs-fmt;

        devShell = pkgs.mkShell {
          hardeningDisable = [ "format" ]; # Disable format-security hardening flags

          buildInputs = with pkgs; [
            gnumake
            gcc13.cc

            # library depends
            gmp
            gmp.dev
            isl
            libffi
            libffi.dev
            libmpc
            libxcrypt
            mpfr
            mpfr.dev
            xz
            xz.dev
            zlib
            zlib.dev
            m4
            bison
            flex
            texinfo
            binutils

            pkg-config
            autoconf-archive
            autoconf
            automake

            valgrind
            valgrind.dev
          ];

          LDFLAGS = "-L${pkgs.gcc13.libc}/lib";
          shellHook = ''
            BUILD_SYSTEM_HEADER_DIR="${pkgs.gcc13.libc.dev}/include"
          '';
        };
      }
    );
}

The solution is reported in this post Development on GCC on nixos - #4 by trofi

I think just importing the buildInputs into the flake will work