From fedbb048276598ca7c10fa49c219a77e7aa55ecd Mon Sep 17 00:00:00 2001 From: Swarsel Date: Thu, 19 Dec 2024 17:40:25 +0100 Subject: [PATCH] feat: provide installation from local ISO --- SwarselSystems.org | 125 ++++++++++++++++++++++++++++++- flake.nix | 5 ++ pkgs/swarsel-rebuild/default.nix | 7 ++ scripts/swarsel-install.sh | 20 ++++- scripts/swarsel-rebuild.sh | 81 ++++++++++++++++++++ 5 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 pkgs/swarsel-rebuild/default.nix create mode 100644 scripts/swarsel-rebuild.sh diff --git a/SwarselSystems.org b/SwarselSystems.org index f3caceb..5a444a6 100644 --- a/SwarselSystems.org +++ b/SwarselSystems.org @@ -1062,6 +1062,11 @@ In this section I am creating some attributes that define general concepts of my type = "app"; program = "${self.packages.${system}.swarsel-install}/bin/swarsel-install"; }; + + rebuild = { + type = "app"; + program = "${self.packages.${system}.swarsel-rebuild}/bin/swarsel-rebuild"; + }; }); devShells = forAllSystems ( system: @@ -2984,11 +2989,11 @@ This program sets up a new NixOS host. } #+end_src -**** swarsel-install +**** swarsel-rebuild This program sets up a new NixOS host. -#+begin_src shell :tangle scripts/swarsel-install.sh +#+begin_src shell :tangle scripts/swarsel-rebuild.sh set -eo pipefail target_flake="chaostheatre" @@ -3074,6 +3079,122 @@ This program sets up a new NixOS host. +#+begin_src nix :tangle pkgs/swarsel-rebuild/default.nix + { writeShellApplication, git }: + + writeShellApplication { + name = "swarsel-rebuild"; + runtimeInputs = [ git ]; + text = builtins.readFile ../../scripts/swarsel-rebuild.sh; + } +#+end_src + +**** swarsel-install + +This program sets up a new NixOS host. + +#+begin_src shell :tangle scripts/swarsel-install.sh + set -eo pipefail + + target_flake="chaostheatre" + target_user="swarsel" + fs_type="ext4" + disk="/dev/vda" + + function help_and_exit() { + echo + echo "Remotely installs NixOS on a target machine using this nix-config." + echo + echo "USAGE: $0 [OPTIONS]" + echo + echo "ARGS:" + echo " -f specify flake to deploy the nixos config of." + echo " Default: chaostheatre" + echo " -u specify user to deploy for." + echo " Default: swarsel" + echo " -t specify file system type to deploy for." + echo " Default: ext4" + echo " -d specify disk to install on." + echo " Default: /dev/vda" + echo " -h | --help Print this help." + exit 0 + } + + function green() { + echo -e "\x1B[32m[+] $1 \x1B[0m" + if [ -n "${2-}" ]; then + echo -e "\x1B[32m[+] $($2) \x1B[0m" + fi + } + function yellow() { + echo -e "\x1B[33m[*] $1 \x1B[0m" + if [ -n "${2-}" ]; then + echo -e "\x1B[33m[*] $($2) \x1B[0m" + fi + } + + while [[ $# -gt 0 ]]; do + case "$1" in + -f) + shift + target_flake=$1 + ;; + -u) + shift + target_user=$1 + ;; + -t) + shift + fs_type=$1 + ;; + -d) + shift + disk=$1 + ;; + -h | --help) help_and_exit ;; + ,*) + echo "Invalid option detected." + help_and_exit + ;; + esac + shift + done + + cd /home/"$target_user" + + if [ ! -d /home/"$target_user"/.dotfiles ]; then + green "Cloning repository from GitHub" + git clone https://github.com/Swarsel/.dotfiles.git + 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" + + cd .dotfiles + if [[ $local_keys != *"${pub_arr[1]}"* ]]; then + yellow "The ssh key for this configuration is not available." + green "Adjusting flake.nix so that the configuration is buildable" + sed -i '/nix-secrets = {/,/^[[:space:]]*};/d' flake.nix + git add flake.nix + fi + sudo mkfs."$fs_type" "$disk" + sudo mount "$disk" /mnt + sudo nixos-generate-config --root /mnt --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/ + git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix + sudo mkdir -p /root/.local/share/nix/ + printf '{\"extra-substituters\":{\"https://nix-community.cachix.org\":true,\"https://nix-community.cachix.org https://cache.ngi0.nixos.org/\":true},\"extra-trusted-public-keys\":{\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=\":true,\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= cache.ngi0.nixos.org-1:KqH5CBLNSyX184S9BKZJo1LxrxJ9ltnY2uAs5c/f1MA=\":true}}' > /root/.local/share/nix/trusted-settings.json + green "Installing flake $target_flake" + sudo nixos-install --flake .#"$target_flake" + yellow "Please keep in mind that this is only a demo of the configuration. Things might break unexpectedly." + git restore --staged /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix + git restore /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix + git restore --staged /home/"$target_user"/.dotfiles/flake.nix + git restore /home/"$target_user"/.dotfiles/flake.nix +#+end_src + + + #+begin_src nix :tangle pkgs/swarsel-install/default.nix { writeShellApplication, git }: diff --git a/flake.nix b/flake.nix index b703e54..925b8ce 100644 --- a/flake.nix +++ b/flake.nix @@ -211,6 +211,11 @@ type = "app"; program = "${self.packages.${system}.swarsel-install}/bin/swarsel-install"; }; + + rebuild = { + type = "app"; + program = "${self.packages.${system}.swarsel-rebuild}/bin/swarsel-rebuild"; + }; }); devShells = forAllSystems ( system: diff --git a/pkgs/swarsel-rebuild/default.nix b/pkgs/swarsel-rebuild/default.nix new file mode 100644 index 0000000..cb80eda --- /dev/null +++ b/pkgs/swarsel-rebuild/default.nix @@ -0,0 +1,7 @@ +{ writeShellApplication, git }: + +writeShellApplication { + name = "swarsel-rebuild"; + runtimeInputs = [ git ]; + text = builtins.readFile ../../scripts/swarsel-rebuild.sh; +} diff --git a/scripts/swarsel-install.sh b/scripts/swarsel-install.sh index b01ac70..c50b067 100644 --- a/scripts/swarsel-install.sh +++ b/scripts/swarsel-install.sh @@ -2,6 +2,8 @@ set -eo pipefail target_flake="chaostheatre" target_user="swarsel" +fs_type="ext4" +disk="/dev/vda" function help_and_exit() { echo @@ -14,6 +16,10 @@ function help_and_exit() { echo " Default: chaostheatre" echo " -u specify user to deploy for." echo " Default: swarsel" + echo " -t specify file system type to deploy for." + echo " Default: ext4" + echo " -d specify disk to install on." + echo " Default: /dev/vda" echo " -h | --help Print this help." exit 0 } @@ -41,6 +47,14 @@ while [[ $# -gt 0 ]]; do shift target_user=$1 ;; + -t) + shift + fs_type=$1 + ;; + -d) + shift + disk=$1 + ;; -h | --help) help_and_exit ;; *) echo "Invalid option detected." @@ -68,12 +82,14 @@ if [[ $local_keys != *"${pub_arr[1]}"* ]]; then sed -i '/nix-secrets = {/,/^[[:space:]]*};/d' flake.nix git add flake.nix fi -sudo nixos-generate-config --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/ +sudo mkfs."$fs_type" "$disk" +sudo mount "$disk" /mnt +sudo nixos-generate-config --root /mnt --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/ git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix sudo mkdir -p /root/.local/share/nix/ printf '{\"extra-substituters\":{\"https://nix-community.cachix.org\":true,\"https://nix-community.cachix.org https://cache.ngi0.nixos.org/\":true},\"extra-trusted-public-keys\":{\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=\":true,\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= cache.ngi0.nixos.org-1:KqH5CBLNSyX184S9BKZJo1LxrxJ9ltnY2uAs5c/f1MA=\":true}}' > /root/.local/share/nix/trusted-settings.json green "Installing flake $target_flake" -sudo nixos-rebuild --show-trace --flake .#"$target_flake" boot +sudo nixos-install --flake .#"$target_flake" yellow "Please keep in mind that this is only a demo of the configuration. Things might break unexpectedly." git restore --staged /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix git restore /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix diff --git a/scripts/swarsel-rebuild.sh b/scripts/swarsel-rebuild.sh new file mode 100644 index 0000000..b01ac70 --- /dev/null +++ b/scripts/swarsel-rebuild.sh @@ -0,0 +1,81 @@ +set -eo pipefail + +target_flake="chaostheatre" +target_user="swarsel" + +function help_and_exit() { + echo + echo "Remotely installs NixOS on a target machine using this nix-config." + echo + echo "USAGE: $0 [OPTIONS]" + echo + echo "ARGS:" + echo " -f specify flake to deploy the nixos config of." + echo " Default: chaostheatre" + echo " -u specify user to deploy for." + echo " Default: swarsel" + echo " -h | --help Print this help." + exit 0 +} + +function green() { + echo -e "\x1B[32m[+] $1 \x1B[0m" + if [ -n "${2-}" ]; then + echo -e "\x1B[32m[+] $($2) \x1B[0m" + fi +} +function yellow() { + echo -e "\x1B[33m[*] $1 \x1B[0m" + if [ -n "${2-}" ]; then + echo -e "\x1B[33m[*] $($2) \x1B[0m" + fi +} + +while [[ $# -gt 0 ]]; do + case "$1" in + -f) + shift + target_flake=$1 + ;; + -u) + shift + target_user=$1 + ;; + -h | --help) help_and_exit ;; + *) + echo "Invalid option detected." + help_and_exit + ;; + esac + shift +done + +cd /home/"$target_user" + +if [ ! -d /home/"$target_user"/.dotfiles ]; then + green "Cloning repository from GitHub" + git clone https://github.com/Swarsel/.dotfiles.git +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" + +cd .dotfiles +if [[ $local_keys != *"${pub_arr[1]}"* ]]; then + yellow "The ssh key for this configuration is not available." + green "Adjusting flake.nix so that the configuration is buildable" + sed -i '/nix-secrets = {/,/^[[:space:]]*};/d' flake.nix + git add flake.nix +fi +sudo nixos-generate-config --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/ +git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix +sudo mkdir -p /root/.local/share/nix/ +printf '{\"extra-substituters\":{\"https://nix-community.cachix.org\":true,\"https://nix-community.cachix.org https://cache.ngi0.nixos.org/\":true},\"extra-trusted-public-keys\":{\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=\":true,\"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= cache.ngi0.nixos.org-1:KqH5CBLNSyX184S9BKZJo1LxrxJ9ltnY2uAs5c/f1MA=\":true}}' > /root/.local/share/nix/trusted-settings.json +green "Installing flake $target_flake" +sudo nixos-rebuild --show-trace --flake .#"$target_flake" boot +yellow "Please keep in mind that this is only a demo of the configuration. Things might break unexpectedly." +git restore --staged /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix +git restore /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix +git restore --staged /home/"$target_user"/.dotfiles/flake.nix +git restore /home/"$target_user"/.dotfiles/flake.nix