summary refs log tree commit diff
path: root/default.nix
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2021-12-09 20:45:22 +0000
committerAlyssa Ross <hi@alyssa.is>2021-12-09 22:55:59 +0000
commit082636312443099e90ad6ed9f961a416a066ae12 (patch)
tree73f2a61204ce61f1fc8ac59501496f12f69b3329 /default.nix
parent4dc359a1afbfd67bcb98a66699b898d7409c23d9 (diff)
downloadspectrum-082636312443099e90ad6ed9f961a416a066ae12.tar
spectrum-082636312443099e90ad6ed9f961a416a066ae12.tar.gz
spectrum-082636312443099e90ad6ed9f961a416a066ae12.tar.bz2
spectrum-082636312443099e90ad6ed9f961a416a066ae12.tar.lz
spectrum-082636312443099e90ad6ed9f961a416a066ae12.tar.xz
spectrum-082636312443099e90ad6ed9f961a416a066ae12.tar.zst
spectrum-082636312443099e90ad6ed9f961a416a066ae12.zip
Build an installer image from the configuration
Diffstat (limited to 'default.nix')
-rw-r--r--default.nix87
1 files changed, 87 insertions, 0 deletions
diff --git a/default.nix b/default.nix
new file mode 100644
index 0000000..183a962
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: MIT
+# SPDX-FileCopyrightText: 2021 Alyssa Ross <hi@alyssa.is>
+
+{ pkgs ? import <nixpkgs> {} }: with pkgs;
+
+let
+  inherit (builtins) head match storeDir;
+  inherit (pkgs.lib) removePrefix;
+  inherit (nixos ./configuration.nix) config;
+
+  image = import ../spectrum-initramfs/live.nix { inherit pkgs; };
+
+  grub = grub2_efi;
+
+  grubCfg = substituteAll {
+    src = ./grub.cfg.in;
+    linux = removePrefix storeDir "${config.boot.kernelPackages.kernel}/${config.system.boot.loader.kernelFile}";
+    initrd = removePrefix storeDir "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}";
+    kernelParams = toString ([
+      "init=${config.system.build.toplevel}/init"
+    ] ++ config.boot.kernelParams);
+  };
+
+  installer = runCommand "installer.img" {
+    nativeBuildInputs = [ squashfs-tools-ng ];
+  } ''
+    sed 's,^${storeDir}/,,' ${writeReferencesToFile config.system.build.toplevel} |
+        tar -C ${storeDir} -c --verbatim-files-from -T - \
+            --owner 0 --group 0 | tar2sqfs $out
+  '';
+
+  storeDev = config.fileSystems."/nix/store".device;
+  installerUuid = head (match "/dev/disk/by-partuuid/(.*)" storeDev);
+
+  eosimages = vmTools.runInLinuxVM (runCommand "eosimages.img" {
+    nativeBuildInputs = [ exfatprogs kmod util-linux ];
+  } ''
+    truncate -s 4G "$out"
+    mkfs.exfat -L eosimages "$out"
+    mkdir /mnt
+    modprobe loop
+    mount "$out" /mnt
+    name=Spectrum-0.0-x86_64-generic.0.Live.img
+    cp ${image} /mnt/$name
+    sha256sum /mnt/$name > /mnt/$name.sha256
+    umount /mnt
+  '');
+in
+
+vmTools.runInLinuxVM (runCommand "spectrum-installer" {
+  nativeBuildInputs = [ dosfstools grub jq kmod util-linux systemdMinimal ];
+} ''
+  blockSize() {
+      wc -c "$1" | awk '{printf "%d\n", ($1 + 511) / 512}'
+  }
+
+  fillPartition() {
+      read start size < <(sfdisk -J "$1" | jq -r --argjson index "$2" \
+          '.partitiontable.partitions[$index] | "\(.start) \(.size)"')
+      dd if="$3" of="$1" seek="$start" count="$size" conv=notrunc
+  }
+
+  efiSize=40000
+  installerSize="$(blockSize ${installer})"
+  eosimagesSize="$(blockSize ${eosimages})"
+
+  truncate -s $(((3 * 2048 + $efiSize + $installerSize + $eosimagesSize) * 512)) $out
+  sfdisk $out <<EOF
+  label: gpt
+  size=$efiSize, type=U
+  size=$installerSize, type=L, uuid=${installerUuid}
+  size=$eosimagesSize, type=56a3bbc3-aefa-43d9-a64d-7b3fd59bbc4e
+  EOF
+
+  fillPartition $out 1 ${installer}
+  fillPartition $out 2 ${eosimages}
+
+  modprobe loop
+  loop="$(losetup -P --show -f $out)"
+  mkfs.vfat "$loop"p1
+  mkdir /mnt
+  mount "$loop"p1 /mnt
+  grub-install --target ${grub.grubTarget} --removable \
+      --boot-directory /mnt --efi-directory /mnt "$loop"
+  cp ${grubCfg} /mnt/grub/grub.cfg
+  umount /mnt
+'')