How to build patched AVR-GCC with patched AVR-LIBC

Hi Folks,

I’m trying to build a patched version of AVR GCC with a patched version of AVR-LIBC. This is the best derivation I’ve been able to come up with so far.

It build avr-libc with the correct patches, and it builds avr-gcc with the correct patches - it just doesn’t feed the patched avr-libc into the gcc build. Can anyone give me some pointers as to how to feed the patched libc into the gcc build?

Also any other feedback would be welcome.

{ pkgs ? import <nixpkgs> { } }:
let
  base = pkgs.pkgsCross.avr.buildPackages.gcc12;

  libc = base.bintools.libc.overrideAttrs (final: previous: {
    src = pkgs.fetchFromGitHub {
      owner = "avrdudes";
      repo = "avr-libc";
      rev = "55e8cac69935657bcd3e4d938750960c757844c3";
      sha256 = "hw93vMdUSS1MXOBvuZptbUexqUuCmMw8TIQcr1HL7wM=";
    };
    nativeBuildInputs = [
      pkgs.autoreconfHook
      pkgs.python
    ];
    autoreconfPhase = "./bootstrap";
  });

  cc = base.cc.overrideAttrs (final: previous: {
    patches = [
      (pkgs.fetchpatch {
        url = "https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff_plain;h=919822adc923b00e995e8a148bdb8115a794b47b";
        sha256 = "/hwABnFuxOROIY6YBkmFlR0htZJxuwPIiU6CBKz/PmU=";
      })
    ];
  });

  avrgcc-mod = pkgs.wrapCCWith {
    inherit cc;
    bintools = pkgs.wrapBintoolsWith {
      bintools = base.bintools;
      inherit libc;
    };
  };

in
pkgs.stdenv.mkDerivation {
  name = "env";
  buildInputs = [
    pkgs.bashInteractive
    avrgcc-mod
  ];

  CFLAGS = [
    "-isystem ${avrgcc-mod.libc}/avr/include"
    "-L${avrgcc-mod.libc}/avr/lib/avr5"
  ];
}

Thanks!

Did you get closer to having the AVR toolchain “NixOS-native”?

I can now only use it from platformio in an FHS shell, but that comes with disadvantages too.
Though probably it’s easier if one wants to use platformio to just allow it to do its own dep management, meaning it might be “wasted energy” to do this the nix way?

EDIT: I just saw https://github.com/NixOS/nixpkgs/pull/48286, so that means AVR should be (partly) natively supported via pkgsCross. I haven’t tried this, since compiling for ATMega on platformio in an FHS shell works for now well enough, at least with Refactor platformio fix ide by ppenguin · Pull Request #237313 · NixOS/nixpkgs · GitHub in an overlay…