#!/nix/store/14lypyys4gfcl982rjddxa6jg7msqz9q-bash-5.1-p16/bin/bash
export PATH=/nix/store/mq2r4pqy9mhmx6ca4jb2zy59gkqxblwj-kexec-tools-2.0.23/bin:/nix/store/s3y5cbdpn7a34vb1kdalyqijq8h1na06-cpio-2.13/bin:$PATH
set -x
set -e
cd $(mktemp -d)
pwd
mkdir initrd
pushd initrd
if [ -e /ssh_pubkey ]; then
  cat /ssh_pubkey >> authorized_keys
fi
find -type f | cpio -o -H newc | gzip -9 > ../extra.gz
popd
cat /nix/store/iilafcf51l465jbaf93y1afajy2da7vg-image/initrd extra.gz > final.gz

kexec -l /nix/store/iilafcf51l465jbaf93y1afajy2da7vg-image/kernel --initrd=final.gz --append="init=/nix/store/zpsl94an9wlpiclnx0yf5iph4jdk0pf1-nixos-system-kexec-22.05pre378557.52dc75a4fee/init console=ttyS0,115200 panic=30 boot.panic_on_fail loglevel=4"
sync
echo "executing kernel, filesystems will be improperly umounted"
kexec -e
