sigil/nixos-modules/lib/make-bootable-image.nix

51 lines
1.2 KiB
Nix
Raw Normal View History

# Builds a compressed EFI System Partition image
2021-04-06 11:10:15 +02:00
{ config, lib, pkgs }:
2022-04-24 08:35:10 +02:00
let cfg = config.block.partitions;
in pkgs.stdenv.mkDerivation {
name = "boot.qcow2";
nativeBuildInputs = with pkgs.buildPackages.buildPackages; [
qemu_test
utillinux
zstd
];
2021-04-06 11:10:15 +02:00
disklabel = lib.uuidFrom config.system.nixos.label;
2021-03-30 22:39:22 +02:00
buildCommand = ''
img=./temp.raw
# Pad the front of the image
2021-04-06 11:10:15 +02:00
truncate --size=1M $img
# Concatentenate the ESP
2021-04-06 11:10:15 +02:00
espByteOffset=$(stat --printf='%s' $img)
2022-04-24 08:35:10 +02:00
zstdcat ${cfg.esp.image} >> $img
2021-04-06 11:10:15 +02:00
truncate --size=%1M $img
2021-04-06 11:10:15 +02:00
# Concatenate the store
storeByteOffset=$(stat --printf='%s' $img)
2022-04-24 08:35:10 +02:00
zstdcat ${cfg.store.image} >> $img
2021-04-06 11:10:15 +02:00
truncate --size=%1M $img
# Pad the end of the image
2021-04-06 11:10:15 +02:00
truncate --size=+1M $img
imgBytes=$(stat --format=%s $img)
# Create the partition table
2021-04-06 11:10:15 +02:00
sectorSize=512
sfdisk $img <<EOF
label: gpt
2021-04-06 11:10:15 +02:00
label-id: $disklabel
2022-04-24 08:35:10 +02:00
start=$(( $storeByteOffset / $sectorSize )), uuid=${cfg.store.guid}, type=${cfg.store.gptType}
start=$(( $espByteOffset / $sectorSize )), uuid=${cfg.esp.guid}, type=${cfg.esp.gptType}
EOF
2021-04-06 11:10:15 +02:00
sfdisk --reorder $img
2020-12-30 21:07:30 +01:00
qemu-img convert -f raw -O qcow2 $img $out
'';
}