From 4aa5f378d2e3f6cb570ec85daa17115097a8d371 Mon Sep 17 00:00:00 2001 From: ene Date: Mon, 20 Feb 2023 08:48:00 +0100 Subject: Feat(bootstrap): Add a script for quick activation --- bootstrap/activate/activate.sh | 95 +++++++++++++++++++++++++++++++ bootstrap/activate/default.nix | 12 ++++ bootstrap/default.nix | 12 ---- bootstrap/install.sh | 126 ----------------------------------------- bootstrap/install/default.nix | 12 ++++ bootstrap/install/install.sh | 126 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+), 138 deletions(-) create mode 100755 bootstrap/activate/activate.sh create mode 100644 bootstrap/activate/default.nix delete mode 100644 bootstrap/default.nix delete mode 100755 bootstrap/install.sh create mode 100644 bootstrap/install/default.nix create mode 100755 bootstrap/install/install.sh (limited to 'bootstrap') diff --git a/bootstrap/activate/activate.sh b/bootstrap/activate/activate.sh new file mode 100755 index 00000000..c3724807 --- /dev/null +++ b/bootstrap/activate/activate.sh @@ -0,0 +1,95 @@ +#! /usr/bin/env dash +# shellcheck disable=SC2086 +# shellcheck source=/dev/null +. %SHELL_LIBRARY_PATH +export LIB_TEMP_DIR_FOR_SCRIPT="$(mktemp -d)" +mne() { + mount --mkdir --options compress-force=zstd:15,subvol="$1" $DISK_ROOT "$2" || dien "Mounting of $1"; +} + + +available_disks="$(mktmp)"; +lsblk -J | jq '.[] | map(.name) | [foreach .[] as $item ({item: null, index: -1}; {$item, index: (.index + 1)})]' > $available_disks; + + +[ "$(jq '.[]' "$available_disks" | wc -l)" -eq 0 ] && die "No disks found" +msg "Select where your system is installed:\n" + +i=9999; # nobody will have so many disks attached + +while ! jq -e --argjson i "$i" '.[$i]' "$available_disks" > /dev/null 2>&1; do + for disk in $(jq -c '.[]' "$available_disks" ); do + printf "%4s) %s \n" "$(echo "$disk" | jq '.index')" "$(echo "$disk" | jq '.item' | tr -d "\"")"; + done + printf "%4s) Exit\n" "q" + readp "Enter a option: " disk + + if [ $disk = "q" ];then + exit 1 + else + i="$disk" + fi + if jq -e --argjson i "$i" 'nth($i)' "$available_disks" > /dev/null 2>&1; then + disk=$(mktmp); + jq -e --argjson i "$i" 'nth($i)' "$available_disks" > "$disk"; + else + warning "No disk selected. Select a disk to continue.\n" + fi +done + +case "$disk" in + "nvme"*) + export DISK_EFI="/dev/${disk}"p1 + export DISK_ROOT="/dev/${disk}"p2 + ;; + "sd"* |"vd"*) + export DISK_EFI="/dev/${disk}"1 + export DISK_ROOT="/dev/${disk}"2 + ;; + *) + die "The disk type: ${disk} is not yet supported!" + ;; +esac + +msg "Mounting..." +# TODO umount -R /mnt +mount -t tmpfs none /mnt +mount --mkdir "$DISK_EFI" /mnt/boot + +mne nix-store /mnt/nix +mne persistent-storage /mnt/srv + +mount --mkdir --options bind /mnt/srv/nixos-config /mnt/etc/nixos + +msg "Finished mounting" +readp "Do you want to continue with nixos-install? [N/y]: " result +case $result in + [Yy]) + msg "Great, select a host-config:" + hosts=$(mktmp); + host=""; + awk -F "." '/nixosConfiguration/{print $2}' "$(tmp 'curl https://git.sils.li/ene/nixos-config/raw/branch/prime/flake.nix 2> /dev/null')" | awk '{print $1}' > $hosts; + + while ! grep "$host" "$hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; do + i=1; + while read -r ho ; do + printf "%4s) %s \n" "$i" "$ho"; + i=$((i+1)); + done < "$hosts" + printf "%4s) Exit\n" "q" + readp "Enter a option: " host + + [ "$host" = "q" ] && exit 1 + host=$(awk -v i="$host" '{if (NR==i) {print $0}}' "$hosts"); + if ! grep "$host" "$hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; then + warning "No host selected. Select a host to continue.\n" + fi + done + nixos-install --flake git+https://git.sils.li/ene/nixos-config#${host} --no-root-passwd + ;; + *) + msg "Sure, do it yourself" + exit 1 + ;; +esac +if [ -d "$LIB_TEMP_DIR_FOR_SCRIPT" ];then rm -r "$LIB_TEMP_DIR_FOR_SCRIPT"; fi diff --git a/bootstrap/activate/default.nix b/bootstrap/activate/default.nix new file mode 100644 index 00000000..2c410f09 --- /dev/null +++ b/bootstrap/activate/default.nix @@ -0,0 +1,12 @@ +# vim: ts=2 +{ + pkgs, + shell-library, + ... +}: let + dependencies = with pkgs; [jq dash]; + name = "activate"; + script = ./activate.sh; + lib = import ../../lib {inherit pkgs shell-library;}; +in + lib.makeShellScriptWithLibrary {inherit dependencies name script;} diff --git a/bootstrap/default.nix b/bootstrap/default.nix deleted file mode 100644 index 6fab9a2d..00000000 --- a/bootstrap/default.nix +++ /dev/null @@ -1,12 +0,0 @@ -# vim: ts=2 -{ - pkgs, - shell-library, - ... -}: let - dependencies = with pkgs; [jq dash]; - name = "install"; - script = ./install.sh; - lib = import ../lib {inherit pkgs shell-library;}; -in - lib.makeShellScriptWithLibrary {inherit dependencies name script;} diff --git a/bootstrap/install.sh b/bootstrap/install.sh deleted file mode 100755 index 37a5e758..00000000 --- a/bootstrap/install.sh +++ /dev/null @@ -1,126 +0,0 @@ -#! /usr/bin/env dash -# shellcheck disable=SC2086 -# shellcheck source=/dev/null -. %SHELL_LIBRARY_PATH -export LIB_TEMP_DIR_FOR_SCRIPT="$(mktemp -d)" -bsc() { - msg2 "$(btrfs subvolume create "$1" || dien "Creating of subvol $1")"; -} - -mne() { - mount --mkdir --options compress-force=zstd:15,subvol="$1" $DISK_ROOT "$2" || dien "Mounting of $1"; -} - - -available_disks="$(mktmp)"; -lsblk -J | jq '.[] | map(.name) | [foreach .[] as $item ({item: null, index: -1}; {$item, index: (.index + 1)})]' > $available_disks; - - -[ "$(jq '.[]' "$available_disks" | wc -l)" -eq 0 ] && die "No disks found" -msg "Select a disk to format:\n" - -i=9999; # nobody will have so many disks attached - -while ! jq -e --argjson i "$i" '.[$i]' "$available_disks" > /dev/null 2>&1; do - for disk in $(jq -c '.[]' "$available_disks" ); do - printf "%4s) %s \n" "$(echo "$disk" | jq '.index')" "$(echo "$disk" | jq '.item' | tr -d "\"")"; - done - printf "%4s) Exit\n" "q" - readp "Enter a option: " disk - - if [ $disk = "q" ];then - exit 1 - else - i="$disk" - fi - if jq -e --argjson i "$i" 'nth($i)' "$available_disks" > /dev/null 2>&1; then - disk=$(mktmp); - jq -e --argjson i "$i" 'nth($i)' "$available_disks" > "$disk"; - else - warning "No disk selected. Select a disk to continue.\n" - fi -done - -readp "Do you really want to delete all data on disk $(jq '.item' "$disk")? [N/y]: " result -case $result in - [Yy]) - msg "Great, deleting everything..." - disk="$(jq '.item' "$disk" | tr -d "\"")" - ;; - *) - msg "Sure, keep your data" - exit 1 - ;; -esac - -sgdisk -Z "/dev/${disk}" > /dev/null|| dien "Zapping" - -sgdisk -n 1:0:+550M -n 2:0:"$ENDSECTOR" -t 1:ef00 -t 2:8300 "/dev/${disk}" > /dev/null|| dien "Partitioning" - -case "$disk" in - "nvme"*) - export DISK_EFI="/dev/${disk}"p1 - export DISK_ROOT="/dev/${disk}"p2 - ;; - "sd"* |"vd"*) - export DISK_EFI="/dev/${disk}"1 - export DISK_ROOT="/dev/${disk}"2 - ;; - *) - die "The disk type: ${disk} is not yet supported!" - ;; -esac - -msg "Started Formatting..." -mkfs.fat -F32 "$DISK_EFI" > /dev/null || dien "Formatting(fat32)" -mkfs.btrfs -f "$DISK_ROOT" > /dev/null || dien "Formatting(btrfs)" - - -msg "Mounting..." -mount -t btrfs $DISK_ROOT /mnt -cd /mnt || die "(Bug): no /mnt" -bsc nix-store -bsc persistent-storage -bsc persistent-storage/nixos-config -cd / -umount -R /mnt -mount -t tmpfs none /mnt -mount --mkdir "$DISK_EFI" /mnt/boot - -mne nix-store /mnt/nix -mne persistent-storage /mnt/srv - -mount --mkdir --options bind /mnt/srv/nixos-config /mnt/etc/nixos - -msg "Finished mounting and generating btrfs subvolumes" -readp "Do you want to continue with nixos-install? [N/y]: " result -case $result in - [Yy]) - msg "Great, select a host-config:" - hosts=$(mktmp); - host=""; - awk -F "." '/nixosConfiguration/{print $2}' "$(tmp 'curl https://git.sils.li/ene/nixos-config/raw/branch/prime/flake.nix 2> /dev/null')" | awk '{print $1}' > $hosts; - - while ! grep "$host" "$hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; do - i=1; - while read -r ho ; do - printf "%4s) %s \n" "$i" "$ho"; - i=$((i+1)); - done < "$hosts" - printf "%4s) Exit\n" "q" - readp "Enter a option: " host - - [ "$host" = "q" ] && exit 1 - host=$(awk -v i="$host" '{if (NR==i) {print $0}}' "$hosts"); - if ! grep "$host" "$hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; then - warning "No host selected. Select a host to continue.\n" - fi - done - nixos-install --flake git+https://git.sils.li/ene/nixos-config#${host} --no-root-passwd - ;; - *) - msg "Sure, do it yourself" - exit 1 - ;; -esac -if [ -d "$LIB_TEMP_DIR_FOR_SCRIPT" ];then rm -r "$LIB_TEMP_DIR_FOR_SCRIPT"; fi diff --git a/bootstrap/install/default.nix b/bootstrap/install/default.nix new file mode 100644 index 00000000..68580d41 --- /dev/null +++ b/bootstrap/install/default.nix @@ -0,0 +1,12 @@ +# vim: ts=2 +{ + pkgs, + shell-library, + ... +}: let + dependencies = with pkgs; [jq dash curl grep]; + name = "install"; + script = ./install.sh; + lib = import ../../lib {inherit pkgs shell-library;}; +in + lib.makeShellScriptWithLibrary {inherit dependencies name script;} diff --git a/bootstrap/install/install.sh b/bootstrap/install/install.sh new file mode 100755 index 00000000..37a5e758 --- /dev/null +++ b/bootstrap/install/install.sh @@ -0,0 +1,126 @@ +#! /usr/bin/env dash +# shellcheck disable=SC2086 +# shellcheck source=/dev/null +. %SHELL_LIBRARY_PATH +export LIB_TEMP_DIR_FOR_SCRIPT="$(mktemp -d)" +bsc() { + msg2 "$(btrfs subvolume create "$1" || dien "Creating of subvol $1")"; +} + +mne() { + mount --mkdir --options compress-force=zstd:15,subvol="$1" $DISK_ROOT "$2" || dien "Mounting of $1"; +} + + +available_disks="$(mktmp)"; +lsblk -J | jq '.[] | map(.name) | [foreach .[] as $item ({item: null, index: -1}; {$item, index: (.index + 1)})]' > $available_disks; + + +[ "$(jq '.[]' "$available_disks" | wc -l)" -eq 0 ] && die "No disks found" +msg "Select a disk to format:\n" + +i=9999; # nobody will have so many disks attached + +while ! jq -e --argjson i "$i" '.[$i]' "$available_disks" > /dev/null 2>&1; do + for disk in $(jq -c '.[]' "$available_disks" ); do + printf "%4s) %s \n" "$(echo "$disk" | jq '.index')" "$(echo "$disk" | jq '.item' | tr -d "\"")"; + done + printf "%4s) Exit\n" "q" + readp "Enter a option: " disk + + if [ $disk = "q" ];then + exit 1 + else + i="$disk" + fi + if jq -e --argjson i "$i" 'nth($i)' "$available_disks" > /dev/null 2>&1; then + disk=$(mktmp); + jq -e --argjson i "$i" 'nth($i)' "$available_disks" > "$disk"; + else + warning "No disk selected. Select a disk to continue.\n" + fi +done + +readp "Do you really want to delete all data on disk $(jq '.item' "$disk")? [N/y]: " result +case $result in + [Yy]) + msg "Great, deleting everything..." + disk="$(jq '.item' "$disk" | tr -d "\"")" + ;; + *) + msg "Sure, keep your data" + exit 1 + ;; +esac + +sgdisk -Z "/dev/${disk}" > /dev/null|| dien "Zapping" + +sgdisk -n 1:0:+550M -n 2:0:"$ENDSECTOR" -t 1:ef00 -t 2:8300 "/dev/${disk}" > /dev/null|| dien "Partitioning" + +case "$disk" in + "nvme"*) + export DISK_EFI="/dev/${disk}"p1 + export DISK_ROOT="/dev/${disk}"p2 + ;; + "sd"* |"vd"*) + export DISK_EFI="/dev/${disk}"1 + export DISK_ROOT="/dev/${disk}"2 + ;; + *) + die "The disk type: ${disk} is not yet supported!" + ;; +esac + +msg "Started Formatting..." +mkfs.fat -F32 "$DISK_EFI" > /dev/null || dien "Formatting(fat32)" +mkfs.btrfs -f "$DISK_ROOT" > /dev/null || dien "Formatting(btrfs)" + + +msg "Mounting..." +mount -t btrfs $DISK_ROOT /mnt +cd /mnt || die "(Bug): no /mnt" +bsc nix-store +bsc persistent-storage +bsc persistent-storage/nixos-config +cd / +umount -R /mnt +mount -t tmpfs none /mnt +mount --mkdir "$DISK_EFI" /mnt/boot + +mne nix-store /mnt/nix +mne persistent-storage /mnt/srv + +mount --mkdir --options bind /mnt/srv/nixos-config /mnt/etc/nixos + +msg "Finished mounting and generating btrfs subvolumes" +readp "Do you want to continue with nixos-install? [N/y]: " result +case $result in + [Yy]) + msg "Great, select a host-config:" + hosts=$(mktmp); + host=""; + awk -F "." '/nixosConfiguration/{print $2}' "$(tmp 'curl https://git.sils.li/ene/nixos-config/raw/branch/prime/flake.nix 2> /dev/null')" | awk '{print $1}' > $hosts; + + while ! grep "$host" "$hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; do + i=1; + while read -r ho ; do + printf "%4s) %s \n" "$i" "$ho"; + i=$((i+1)); + done < "$hosts" + printf "%4s) Exit\n" "q" + readp "Enter a option: " host + + [ "$host" = "q" ] && exit 1 + host=$(awk -v i="$host" '{if (NR==i) {print $0}}' "$hosts"); + if ! grep "$host" "$hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; then + warning "No host selected. Select a host to continue.\n" + fi + done + nixos-install --flake git+https://git.sils.li/ene/nixos-config#${host} --no-root-passwd + ;; + *) + msg "Sure, do it yourself" + exit 1 + ;; +esac +if [ -d "$LIB_TEMP_DIR_FOR_SCRIPT" ];then rm -r "$LIB_TEMP_DIR_FOR_SCRIPT"; fi -- cgit 1.4.1