yxsunq (text, doesn't expire)
{ pkgs ? import <nixpkgs> {}
, platformArch ? "AARCH64"
, platformFile ? "Platform/RaspberryPi/RPi3/RPi3.dsc"
, ... }:

let
  platform = if platformArch == "AARCH64" then "aarch64-multiplatform"
    else if platformArch == "ARM" then "armv7l-hf-multiplatform"
    else if platformArch == "IA32" then "gnu32"
    else if platformArch == "X64" then "gnu64"
    else if platformArch == "RISCV64" then "riscv64"
    else builtins.throw "Unknown platform architecture";

  platformPkgs = pkgs.pkgsCross.${platform};
  src = {
    edk2 = pkgs.fetchgit {
      url = "https://github.com/tianocore/edk2.git";
      rev = "dd5c7e3c5282b084daa5bbf0ec229cec699b2c17";
      sha256 = "1m49m8z1448lmds3ypzipv2yayhayj3w7f0fashry8563zc85cpb";
      fetchSubmodules = true;
    };
    edk2-platforms = pkgs.fetchgit {
      url = "https://github.com/tianocore/edk2-platforms.git";
      rev = "4efd9ab2cfabdcbd6ca410f870bc889e76f18d85";
      sha256 = "1mz8885scaksbkshab2sqwjxmblcy733l0qf1a8ixxjawa3rx7y9";
      fetchSubmodules = true;
    };
    edk2-non-osi = pkgs.fetchgit {
      url = "https://github.com/tianocore/edk2-non-osi.git";
      rev = "ff6750947db338c5a8be116c0d3dfef8ec3064ec";
      sha256 = "1844jsm2dafy4f70srgb3k1jpfx7xc70snjp30qzi74bcyka05l6";
      fetchSubmodules = true;
    };
  };
in

platformPkgs.stdenv.mkDerivation rec {
  pname = "edk2-platform-${with builtins; head (split "\\." (baseNameOf platformFile))}";
  version = "4efd9ab";

  srcs = builtins.attrValues src;

  depsBuildBuild = with pkgs; [ gcc utillinux python iasl openssl ];

  unpackPhase = ''
    mkdir -p $out
    cp -r ${src.edk2} $out/edk2
    cp -r ${src.edk2-platforms} $out/edk2-platforms
    cp -r ${src.edk2-non-osi} $out/edk2-non-osi
    chmod -R u+w $out
    cd $out
  '';

  NIX_CFLAGS_COMPILE = "-Wno-format -Wno-format-security";

  patches = [ ./rnglib.patch ];
  patchFlags = [ "--strip=1" "--directory=edk2-platforms" ];
  postPatch = "patchShebangs .";

  configurePhase = ''
    export WORKSPACE=$PWD
    export PACKAGES_PATH=$PWD/edk2:$PWD/edk2-platforms:$PWD/edk2-non-osi
    export GCC5_${platformArch}_PREFIX="${pkgs.lib.systems.examples.${platform}.config}-"
    . edk2/edksetup.sh
  '';

  buildPhase = ''
    make -C edk2/BaseTools
    build -n $NIX_BUILD_CORES -b RELEASE -a ${platformArch} -t GCC5 -p ${platformFile}
  '';

  dontInstall = true;
  dontFixup = true;
}