This commit is contained in:
2026-01-27 15:41:24 +00:00
parent 78889cf07a
commit 3d2fc01ccb

View File

@@ -1,166 +1,64 @@
{ {
lib,
pkgs, pkgs,
stdenv,
fetchurl,
}: }:
let stdenv.mkDerivation rec {
# Minimal locale package with only en_US.UTF-8 pname = "mrescue";
minimalLocales = pkgs.glibcLocales.override { version = "3.23.2";
allLocales = false;
locales = [ "en_US.UTF-8/UTF-8" ]; src = fetchurl {
url = "https://dl-cdn.alpinelinux.org/alpine/v3.23/releases/x86_64/alpine-netboot-${version}-x86_64.tar.gz";
hash = "sha256-nFfzrPH1KI2R3OXBOluV7wB/hY63ImxWp/tyzBahpK0=";
}; };
# Simple halt script nativeBuildInputs = with pkgs; [
haltScript = pkgs.writeScriptBin "halt" '' gzip
#!${pkgs.bash}/bin/bash cpio
sync squashfsTools
echo o > /proc/sysrq-trigger
'';
# Simple reboot script
rebootScript = pkgs.writeScriptBin "reboot" ''
#!${pkgs.bash}/bin/bash
sync
echo b > /proc/sysrq-trigger
'';
# Simple init script
init = pkgs.writeScript "init" ''
#!${pkgs.bash}/bin/bash
set -e
# Set up PATH first
export PATH=/bin
export HOME=/root
export TERM=linux
# Set up UTF-8 locale
export LOCALE_ARCHIVE=/lib/locale/locale-archive
export LC_ALL=en_US.utf8
export LANG=en_US.utf8
export LC_CTYPE=en_US.utf8
# Create mount points
mkdir -p /proc /sys /dev /run /tmp
# Mount essential filesystems
mount -t proc proc /proc
mount -t sysfs sys /sys
mount -t devtmpfs dev /dev
# Drop to rescue shell
exec /bin/bash
'';
# Packages to include (all binaries from each package will be included)
packages = with pkgs; [
vim
bash
less
kmod
tmux
dhcpcd
parted
procps
gnugrep
iproute2
findutils findutils
e2fsprogs zstd
dosfstools
btrfs-progs
util-linux
uutils-coreutils-noprefix
]; ];
# Generate binary entries for makeInitrdNG by auto-discovering all binaries sourceRoot = ".";
binaryEntries =
let
# Collect all entries with package info
allEntriesWithPkg = lib.flatten (
map (
pkg:
let
binDir = "${pkg}/bin";
# Get all files in the bin directory
binFiles = if builtins.pathExists binDir then builtins.attrNames (builtins.readDir binDir) else [ ];
pkgName = pkg.name or (builtins.baseNameOf (builtins.toString pkg));
in
map (bin: {
source = "${binDir}/${bin}";
target = "/bin/${bin}";
package = pkgName;
binary = bin;
}) binFiles
) packages
);
# Build map of binary -> list of packages providing it unpackPhase = ''
binaryMap = lib.foldl' ( runHook preUnpack
acc: entry: mkdir alpine-boot
let tar -xzf $src -C alpine-boot --strip-components=1
existing = acc.${entry.binary} or [ ]; runHook postUnpack
in '';
acc // { ${entry.binary} = existing ++ [ entry.package ]; }
) { } allEntriesWithPkg;
# Deduplicate by target path, keeping first occurrence and warning about duplicates buildPhase = ''
deduped = lib.foldl' ( runHook preBuild
acc: entry:
let
alreadyExists = builtins.any (e: e.target == entry.target) acc;
providers = binaryMap.${entry.binary};
hasDuplicates = builtins.length providers > 1;
in
if alreadyExists then
acc
else if hasDuplicates then
builtins.trace
"Warning: binary '${entry.binary}' provided by multiple packages: ${builtins.concatStringsSep ", " providers}. Chose: ${entry.package}"
(acc ++ [ entry ])
else
acc ++ [ entry ]
) [ ] allEntriesWithPkg;
in
deduped;
initrd = pkgs.makeInitrdNG { mkdir -p work/initramfs-extracted
name = "mrescue-initrd"; cd work
compressor = "zstd";
compressorArgs = [
#"-19"
"-12"
"-T0"
];
contents = [ gzip -dc < ../alpine-boot/initramfs-virt | \
{ cpio -idm --quiet -D initramfs-extracted
source = init;
target = "/init";
}
{
source = "${pkgs.linuxPackages_latest.kernel.modules}/lib/modules";
target = "/lib/modules";
}
{
source = "${minimalLocales}/lib/locale/locale-archive";
target = "/lib/locale/locale-archive";
}
{
source = "${haltScript}/bin/halt";
target = "/bin/halt";
}
{
source = "${rebootScript}/bin/reboot";
target = "/bin/reboot";
}
]
++ binaryEntries; # makeInitrdNG will auto-resolve dependencies for these
};
in unsquashfs -f -d modloop-extracted ../alpine-boot/modloop-virt >/dev/null
# Package both kernel and initrd together
pkgs.runCommand "mrescue" { } '' mkdir -p initramfs-extracted/lib/modules
mkdir -p $out cp -r modloop-extracted/modules/* initramfs-extracted/lib/modules/
ln -s ${pkgs.linuxPackages_latest.kernel}/bzImage $out/bzImage
ln -s ${initrd}/initrd $out/initrd cd initramfs-extracted
'' find * .[^.*] -print0 | sort -z | \
cpio --quiet -o -H newc -R +0:+0 --reproducible --null | \
zstd -15 -T0 > ../initramfs-combined.zst
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
install -Dm644 ../../alpine-boot/vmlinuz-virt $out/kernel
install -Dm644 ../initramfs-combined.zst $out/initramfs
runHook postInstall
'';
}