third_party/nix: introduce toolchain bundle

This introduces a nix derivation that builds a musl amd64/aarch64
toolchain sysroot.

Change-Id: Iba082edb8fd1f2ab580020bb1c7339a76487f3c8
Reviewed-on: https://review.monogon.dev/c/monogon/+/4006
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
diff --git a/build/toolchain/toolchain-bundle/default.nix b/build/toolchain/toolchain-bundle/default.nix
new file mode 100644
index 0000000..f59a6a8
--- /dev/null
+++ b/build/toolchain/toolchain-bundle/default.nix
@@ -0,0 +1,52 @@
+{ pkgs ? import ../../../third_party/nix/default.nix { } }: with pkgs;
+symlinkJoin {
+  name = "toolchain";
+  paths =
+    let
+      platforms = with pkgsCross; [
+        aarch64-multiplatform-musl
+        musl64
+      ];
+    in
+    map
+      (platform: (
+        let
+          merged = buildEnv {
+            name = "toolchain-env";
+            paths = with platform.pkgsStatic; [
+              gnumake
+              flex
+              bison
+              lz4
+              busybox
+              findutils
+              bc
+              util-linux-minimal # custom pkg
+              perl
+              nasm
+              acpica-tools
+              patch
+              diffutils
+              qemu-minimal # custom pkg
+              m4
+              strace
+            ];
+          };
+        in
+        stdenv.mkDerivation rec {
+          name = "toolchain-bundle";
+          buildInputs = [ gnutar zstd ];
+
+          phases = [ "buildPhase" "installPhase" ];
+          buildPhase = ''
+            tar --zstd --sort=name --hard-dereference -hcf bundle.tar.zst -C ${merged} .
+          '';
+
+          installPhase = ''
+            mkdir $out
+            mv bundle.tar.zst $out/${name}-${platform.hostPlatform.config}-${lib.version}.tar.zst
+          '';
+        }
+      ))
+      platforms;
+}