~ehmry/sigil

ref: post-mortem sigil/nixos-modules/lib/make-bootable-image.nix -rw-r--r-- 1.2 KiB
2b1c9d71Emery Hemingway Link the write-up (or write-off) of the project 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Builds a compressed EFI System Partition image
{ config, lib, pkgs }:

with config.block.partitions;

pkgs.stdenv.mkDerivation {
  name = "boot.qcow2";

  nativeBuildInputs = with pkgs.buildPackages.buildPackages; [
    qemu_test
    utillinux
    zstd
  ];

  disklabel = lib.uuidFrom config.system.nixos.label;

  buildCommand = ''
    img=./temp.raw

    # Pad the front of the image
    truncate --size=1M $img

    # Concatentenate the ESP
    espByteOffset=$(stat --printf='%s' $img)
    zstdcat ${esp.image} >> $img
    truncate --size=%1M $img

    # Concatenate the store
    storeByteOffset=$(stat --printf='%s' $img)
    zstdcat ${store.image} >> $img
    truncate --size=%1M $img

    # Pad the end of the image
    truncate --size=+1M $img

    imgBytes=$(stat --format=%s $img)

    # Create the partition table
    sectorSize=512
    sfdisk $img <<EOF
      label: gpt
      label-id: $disklabel
      start=$(( $storeByteOffset / $sectorSize )), uuid=${store.guid}, type=${store.gptType}
      start=$(( $espByteOffset / $sectorSize )), uuid=${esp.guid}, type=${esp.gptType}
    EOF
    sfdisk --reorder $img

    qemu-img convert -f raw -O qcow2 $img $out
  '';
}