Skip to content

Commit

Permalink
WIP: install
Browse files Browse the repository at this point in the history
  • Loading branch information
Swarsel committed Dec 28, 2024
1 parent 665abec commit c84c5e5
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 50 deletions.
118 changes: 93 additions & 25 deletions SwarselSystems.org
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ For added convenience, the live environment displays a helpful text on login, we
Also, an initial bash history is provided to allow for a very quick local deployment:

#+begin_src shell :tangle programs/bash/.bash_history
swarsel-install -f chaostheatre -d /dev/vda
swarsel-install -n chaostheatre -d /dev/vda
#+end_src


Expand Down Expand Up @@ -3260,10 +3260,13 @@ This program sets up a new NixOS host locally.
#+begin_src shell :tangle scripts/swarsel-install.sh
set -eo pipefail

target_config="chaostheatre"
target_config=target_hostname="chaostheatre"
target_user="swarsel"
fs_type="ext4"
disk=""
# fs_type="ext4"
# disk=""

# persist_dir=""
disk_encryption=0

function help_and_exit() {
echo
Expand All @@ -3284,6 +3287,12 @@ This program sets up a new NixOS host locally.
exit 0
}

function red() {
echo -e "\x1B[31m[!] $1 \x1B[0m"
if [ -n "${2-}" ]; then
echo -e "\x1B[31m[!] $($2) \x1B[0m"
fi
}
function green() {
echo -e "\x1B[32m[+] $1 \x1B[0m"
if [ -n "${2-}" ]; then
Expand All @@ -3301,20 +3310,20 @@ This program sets up a new NixOS host locally.
case "$1" in
-n)
shift
target_config=$1
target_config=target_hostname=$1
;;
-u)
shift
target_user=$1
;;
-t)
shift
fs_type=$1
;;
-d)
shift
disk=$1
;;
# -t)
# shift
# fs_type=$1
# ;;
# -d)
# shift
# disk=$1
# ;;
-h | --help) help_and_exit ;;
,*)
echo "Invalid option detected."
Expand All @@ -3324,6 +3333,8 @@ This program sets up a new NixOS host locally.
shift
done

green "~SwarselSystems~ remote installer"

cd /home/"$target_user"

sudo rm -rf .cache/nix
Expand All @@ -3332,6 +3343,42 @@ This program sets up a new NixOS host locally.
green "Cloning repository from GitHub"
git clone https://github.com/Swarsel/.dotfiles.git

green "Reading system information for $target_config ..."
DISK="$(nix eval --raw ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.rootDisk)"
green "Root Disk: $DISK"

CRYPTED="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isCrypted)"
if [[ $CRYPTED == "true" ]]; then
green "Encryption: ✓"
disk_encryption=1
else
red "Encryption: X"
disk_encryption=0
fi

IMPERMANENCE="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isImpermanence)"
if [[ $IMPERMANENCE == "true" ]]; then
green "Impermanence: ✓"
persist_dir="/persist"
else
red "Impermanence: X"
persist_dir=""
fi

SWAP="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isSwap)"
if [[ $SWAP == "true" ]]; then
green "Swap: ✓"
else
red "Swap: X"
fi

SECUREBOOT="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isSecureBoot)"
if [[ $SECUREBOOT == "true" ]]; then
green "Secure Boot: ✓"
else
red "Secure Boot: X"
fi

local_keys=$(ssh-add -L || true)
pub_key=$(cat /home/"$target_user"/.dotfiles/secrets/keys/ssh/nbl-imba-2.pub)
read -ra pub_arr <<< "$pub_key"
Expand All @@ -3346,25 +3393,46 @@ This program sets up a new NixOS host locally.
green "Valid SSH key found! Continuing with installation"
fi

green "Creating /boot partition"
sudo parted -a optimal --script "$disk" mklabel gpt
sudo parted -a optimal --script "$disk" mkpart "boot" fat32 1MiB 1025MiB
sudo parted -a optimal --script "$disk" set 1 esp on
if [ "$disk_encryption" -eq 1 ]; then
while true; do
green "Set disk encryption passphrase:"
read -rs luks_passphrase
green "Please confirm passphrase:"
read -rs luks_passphrase_confirm
if [[ $luks_passphrase == "$luks_passphrase_confirm" ]]; then
echo "$luks_passphrase" > /tmp/disko-password
break
else
red "Passwords do not match"
fi
done
fi

green "Creating / partition"
sudo parted -a optimal --script "$disk" mkpart "root" "$fs_type" 1025MiB 100%
sudo parted -a optimal --script "$disk" type 2 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
green "Setting up disk"
sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko/latest -- --mode destroy,format,mount -- flake .#"$target_config"

green "Ensuring proper file systems"
sudo mkfs.fat -F32 "$disk"1
sudo mkfs."${fs_type}" -F "$disk"2
# green "Creating /boot partition"
# sudo parted -a optimal --script "$disk" mklabel gpt
# sudo parted -a optimal --script "$disk" mkpart "boot" fat32 1MiB 1025MiB
# sudo parted -a optimal --script "$disk" set 1 esp on

# green "Creating / partition"
# sudo parted -a optimal --script "$disk" mkpart "root" "$fs_type" 1025MiB 100%
# sudo parted -a optimal --script "$disk" type 2 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709

# green "Ensuring proper file systems"
# sudo mkfs.fat -F32 "$disk"1
# sudo mkfs."${fs_type}" -F "$disk"2

green "Generating hardware configuration"
sudo mount "$disk"2 /mnt
sudo mount "$DISK"2 /mnt
sudo mkdir -p /mnt/boot
sudo mount "$disk"1 /mnt/boot
sudo mount "$DISK"1 /mnt/boot
sudo nixos-generate-config --root /mnt --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_config"/

green "Injecting initialSetup"
sudo sed -i '/ boot.extraModulePackages /a \ swarselsystems.initialSetup = true;' /mnt/etc/nixos/hardware-configuration.nix

git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_config"/hardware-configuration.nix
# sudo rm -rf /root/.nix-defexpr/channels
# sudo rm -rf /nix/var/nix/profiles/per-user/channels
Expand Down
2 changes: 1 addition & 1 deletion programs/bash/.bash_history
Original file line number Diff line number Diff line change
@@ -1 +1 @@
swarsel-install -f chaostheatre -d /dev/vda
swarsel-install -n chaostheatre -d /dev/vda
116 changes: 92 additions & 24 deletions scripts/swarsel-install.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
set -eo pipefail

target_config="chaostheatre"
target_config=target_hostname="chaostheatre"
target_user="swarsel"
fs_type="ext4"
disk=""
# fs_type="ext4"
# disk=""

# persist_dir=""
disk_encryption=0

function help_and_exit() {
echo
Expand All @@ -24,6 +27,12 @@ function help_and_exit() {
exit 0
}

function red() {
echo -e "\x1B[31m[!] $1 \x1B[0m"
if [ -n "${2-}" ]; then
echo -e "\x1B[31m[!] $($2) \x1B[0m"
fi
}
function green() {
echo -e "\x1B[32m[+] $1 \x1B[0m"
if [ -n "${2-}" ]; then
Expand All @@ -41,20 +50,20 @@ while [[ $# -gt 0 ]]; do
case "$1" in
-n)
shift
target_config=$1
target_config=target_hostname=$1
;;
-u)
shift
target_user=$1
;;
-t)
shift
fs_type=$1
;;
-d)
shift
disk=$1
;;
# -t)
# shift
# fs_type=$1
# ;;
# -d)
# shift
# disk=$1
# ;;
-h | --help) help_and_exit ;;
*)
echo "Invalid option detected."
Expand All @@ -64,6 +73,8 @@ while [[ $# -gt 0 ]]; do
shift
done

green "~SwarselSystems~ remote installer"

cd /home/"$target_user"

sudo rm -rf .cache/nix
Expand All @@ -72,6 +83,42 @@ sudo rm -rf .dotfiles
green "Cloning repository from GitHub"
git clone https://github.com/Swarsel/.dotfiles.git

green "Reading system information for $target_config ..."
DISK="$(nix eval --raw ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.rootDisk)"
green "Root Disk: $DISK"

CRYPTED="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isCrypted)"
if [[ $CRYPTED == "true" ]]; then
green "Encryption: ✓"
disk_encryption=1
else
red "Encryption: X"
disk_encryption=0
fi

IMPERMANENCE="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isImpermanence)"
if [[ $IMPERMANENCE == "true" ]]; then
green "Impermanence: ✓"
persist_dir="/persist"
else
red "Impermanence: X"
persist_dir=""
fi

SWAP="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isSwap)"
if [[ $SWAP == "true" ]]; then
green "Swap: ✓"
else
red "Swap: X"
fi

SECUREBOOT="$(nix eval ~/.dotfiles#nixosConfigurations."$target_hostname".config.swarselsystems.isSecureBoot)"
if [[ $SECUREBOOT == "true" ]]; then
green "Secure Boot: ✓"
else
red "Secure Boot: X"
fi

local_keys=$(ssh-add -L || true)
pub_key=$(cat /home/"$target_user"/.dotfiles/secrets/keys/ssh/nbl-imba-2.pub)
read -ra pub_arr <<< "$pub_key"
Expand All @@ -86,25 +133,46 @@ else
green "Valid SSH key found! Continuing with installation"
fi

green "Creating /boot partition"
sudo parted -a optimal --script "$disk" mklabel gpt
sudo parted -a optimal --script "$disk" mkpart "boot" fat32 1MiB 1025MiB
sudo parted -a optimal --script "$disk" set 1 esp on
if [ "$disk_encryption" -eq 1 ]; then
while true; do
green "Set disk encryption passphrase:"
read -rs luks_passphrase
green "Please confirm passphrase:"
read -rs luks_passphrase_confirm
if [[ $luks_passphrase == "$luks_passphrase_confirm" ]]; then
echo "$luks_passphrase" > /tmp/disko-password
break
else
red "Passwords do not match"
fi
done
fi

green "Setting up disk"
sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko/latest -- --mode destroy,format,mount -- flake .#"$target_config"

# green "Creating /boot partition"
# sudo parted -a optimal --script "$disk" mklabel gpt
# sudo parted -a optimal --script "$disk" mkpart "boot" fat32 1MiB 1025MiB
# sudo parted -a optimal --script "$disk" set 1 esp on

green "Creating / partition"
sudo parted -a optimal --script "$disk" mkpart "root" "$fs_type" 1025MiB 100%
sudo parted -a optimal --script "$disk" type 2 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
# green "Creating / partition"
# sudo parted -a optimal --script "$disk" mkpart "root" "$fs_type" 1025MiB 100%
# sudo parted -a optimal --script "$disk" type 2 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709

green "Ensuring proper file systems"
sudo mkfs.fat -F32 "$disk"1
sudo mkfs."${fs_type}" -F "$disk"2
# green "Ensuring proper file systems"
# sudo mkfs.fat -F32 "$disk"1
# sudo mkfs."${fs_type}" -F "$disk"2

green "Generating hardware configuration"
sudo mount "$disk"2 /mnt
sudo mount "$DISK"2 /mnt
sudo mkdir -p /mnt/boot
sudo mount "$disk"1 /mnt/boot
sudo mount "$DISK"1 /mnt/boot
sudo nixos-generate-config --root /mnt --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_config"/

green "Injecting initialSetup"
sudo sed -i '/ boot.extraModulePackages /a \ swarselsystems.initialSetup = true;' /mnt/etc/nixos/hardware-configuration.nix

git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_config"/hardware-configuration.nix
# sudo rm -rf /root/.nix-defexpr/channels
# sudo rm -rf /nix/var/nix/profiles/per-user/channels
Expand Down

0 comments on commit c84c5e5

Please sign in to comment.