# SPDX-License-Identifier: MIT # SPDX-FileCopyrightText: 2026 Colby Russell import ../../lib/call-package.nix ( { src, spectrum-build-tools, kernel ? null , lib, stdenvNoCC, makeModulesClosure, runCommand, writeClosure, pkgsStatic , busybox, cpio }: pkgsStatic.callPackage ({ execline, kmod, mdevd, cryptsetup, util-linuxMinimal }: let inherit (lib) concatMapStringsSep filter foldl isString last split tail; emptyFirmware = runCommand "empty-firmware" {} '' mkdir -p $out/lib/firmware ''; modules = if kernel == null then null else makeModulesClosure { firmware = emptyFirmware; inherit kernel; rootModules = [ "dm-mod" "dm-verity" ]; }; packages = [ execline kmod mdevd (cryptsetup.override { programs = { cryptsetup = false; cryptsetup-reencrypt = false; integritysetup = false; }; }) (busybox.override { enableStatic = true; extraConfig = '' CONFIG_DEPMOD n CONFIG_FINDFS n CONFIG_INSMOD n CONFIG_LSMOD n CONFIG_MODINFO n CONFIG_MODPROBE n CONFIG_RMMOD n ''; }) ]; packagesSysroot = runCommand "vm-initramfs-packages-sysroot" {} '' mkdir -p $out/bin ln -s ${concatMapStringsSep " " (p: "${p}/bin/*") packages} $out/bin ln -s /bin $out/sbin ${lib.optionalString (modules != null) "cp -R ${modules}/lib $out"} # TODO: this is a hack and we should just build the util-linux # programs we want. # https://lore.kernel.org/util-linux/87zgrl6ufb.fsf@alyssa.is/ cp ${util-linuxMinimal}/bin/{findfs,lsblk} $out/bin ''; storeComponents = tail (filter isString (split "/" builtins.storeDir)); packagesCpio = runCommand "vm-initramfs-packages.cpio" { nativeBuildInputs = [ cpio ]; storePaths = writeClosure [ packagesSysroot ]; storePrefixes = foldl (acc: elem: acc ++ [ "${if acc == [] then "" else last acc}/${elem}" ]) [] storeComponents; __structuredAttrs = true; unsafeDiscardReferences = { out = true; }; dontFixup = true; } '' cd ${packagesSysroot} (printf "%s\n" "''${storePrefixes[@]}" && find . $(< $storePaths)) | cpio -o -H newc -R +0:+0 --reproducible > $out ''; in stdenvNoCC.mkDerivation { name = "spectrum-vm-initramfs"; src = lib.fileset.toSource { root = ../..; fileset = lib.fileset.intersection src (lib.fileset.unions [ ./. ../../lib/common.mk ]); }; sourceRoot = "source/vm/initramfs"; env = { PACKAGES_CPIO = packagesCpio; }; nativeBuildInputs = [ cpio spectrum-build-tools ]; makeFlags = [ "dest=$(out)" ]; dontInstall = true; enableParallelBuilding = true; __structuredAttrs = true; unsafeDiscardReferences = { out = true; }; dontFixup = true; passthru = { inherit packagesSysroot; }; } ) {}) (_: {})