CASCADE/overlays/cascade-installer.nix

64 lines
2.2 KiB
Nix

final: prev: {
cascade-installer = prev.writeShellApplication {
name="cascade-installer";
runtimeInputs = with final; [ bash git parted util-linux jq ];
text=''
usage() {
1>&2 echo "usage: cascade-installer HOSTNAME BLOCKDEVTONUKE PROFILE"
}
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ];then
usage
exit 1
fi
HOSTNAME="$1"
BLOCKDEVTONUKE="$2"
PROFILE="$3"
if ! [ -d /usr/src/cascade/profiles/"$PROFILE" ];then
1>&2 echo "profile $PROFILE does not exist. possible options:"
(cd /usr/src/cascade/profiles/ && 1>&2 printf " %s\n" [a-zA-Z0-9]*)
exit 1
fi
mkdir -p /mnt
# if the block device has children...
if lsblk -J "$BLOCKDEVTONUKE" | jq -e '.blockdevices[]|.children' > /dev/null ; then
1>&2 echo "$BLOCKDEVTONUKE still has partitions. please clear the block device before installing"
exit 3
fi
parted "$BLOCKDEVTONUKE" -- mklabel gpt
parted "$BLOCKDEVTONUKE" -- mkpart primary 512MiB -2GiB
parted "$BLOCKDEVTONUKE" -- mkpart primary linux-swap -2GiB 100%
parted "$BLOCKDEVTONUKE" -- mkpart ESP fat32 1MiB 512MiB
parted "$BLOCKDEVTONUKE" -- set 3 esp on
parted "$BLOCKDEVTONUKE" -- name 1 nixos
parted "$BLOCKDEVTONUKE" -- name 2 nixos-swap
parted "$BLOCKDEVTONUKE" -- name 3 ESP
sync
sleep 3
BLK_NIXOS="$(lsblk -OJ "$BLOCKDEVTONUKE" | jq -r '.blockdevices[].children[]|select(.partlabel == "nixos")|.name')"
BLK_SWAP="$(lsblk -OJ "$BLOCKDEVTONUKE" | jq -r '.blockdevices[].children[]|select(.partlabel == "nixos-swap")|.name')"
BLK_ESP="$(lsblk -OJ "$BLOCKDEVTONUKE" | jq -r '.blockdevices[].children[]|select(.partlabel == "ESP")|.name')"
mkfs.xfs -L nixos "$BLK_NIXOS"
mkswap -L nixos-swap "$BLK_SWAP"
mkfs.fat -F 32 -n BOOT "$BLK_ESP"
mount "$BLK_NIXOS" /mnt
mkdir -p /mnt/boot /mnt/etc/nixos
sed -e '
s#[(] *toString ../profiles/.*[)]#(toString /usr/src/cascade/profiles/'"$PROFILE"')#
s#networking.hostName = "nixos";#networking.hostName = "'"$HOSTNAME"'";#
' < /usr/src/cascade/hosts/_basic.nix > /etc/nixos/configuration.nix
swapon "$BLK_SWAP"
nixos-install
'';
};
}