From 54b3106db33f8a2fc5e31ba0b530759996da2c89 Mon Sep 17 00:00:00 2001 From: Swarsel Date: Wed, 18 Dec 2024 13:58:49 +0100 Subject: [PATCH] feat: local install utility --- SwarselSystems.org | 19 +++++----- flake.nix | 5 +++ pkgs/default.nix | 1 + pkgs/swarsel-install/default.nix | 7 ++++ scripts/swarsel-install.sh | 64 ++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 pkgs/swarsel-install/default.nix create mode 100644 scripts/swarsel-install.sh diff --git a/SwarselSystems.org b/SwarselSystems.org index a5fb0ab..80dc814 100644 --- a/SwarselSystems.org +++ b/SwarselSystems.org @@ -3033,19 +3033,18 @@ This program sets up a new NixOS host. shift done - if [[ -z ${FLAKE} ]]; then - FLAKE=/home/"$target_user"/.dotfiles - fi - if [ ! -d "$FLAKE" ]; then - cd /home/"$target_user" - yellow "Flake directory not found - cloning repository from GitHub" - git clone git@github.com:Swarsel/.dotfiles.git || (yellow "Could not clone repository via SSH - defaulting to HTTPS" && git clone https://github.com/Swarsel/.dotfiles.git) - FLAKE=/home/"$target_user"/.dotfiles + 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 - cd "$FLAKE" + cd .dotfiles + sudo nixos-generate-config --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix + git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix green "Installing flake $target_flake" - sudo nixos-rebuild --show-trace --flake .#"$target_flake" switch + sudo nixos-rebuild --show-trace --flake .#"$target_flake" --keep-going switch #+end_src diff --git a/flake.nix b/flake.nix index e0aee93..b703e54 100644 --- a/flake.nix +++ b/flake.nix @@ -206,6 +206,11 @@ type = "app"; program = "${self.packages.${system}.bootstrap}/bin/bootstrap"; }; + + install = { + type = "app"; + program = "${self.packages.${system}.swarsel-install}/bin/swarsel-install"; + }; }); devShells = forAllSystems ( system: diff --git a/pkgs/default.nix b/pkgs/default.nix index 3e91c1c..07cfa99 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -17,6 +17,7 @@ let "github-notifications" "screenshare" "bootstrap" + "swarsel-install" "t2ts" "ts2t" "vershell" diff --git a/pkgs/swarsel-install/default.nix b/pkgs/swarsel-install/default.nix new file mode 100644 index 0000000..6b3c4b2 --- /dev/null +++ b/pkgs/swarsel-install/default.nix @@ -0,0 +1,7 @@ +{ writeShellApplication, git }: + +writeShellApplication { + name = "swarsel-install"; + runtimeInputs = [ git ]; + text = builtins.readFile ../../scripts/swarsel-install.sh; +} diff --git a/scripts/swarsel-install.sh b/scripts/swarsel-install.sh new file mode 100644 index 0000000..18a01af --- /dev/null +++ b/scripts/swarsel-install.sh @@ -0,0 +1,64 @@ +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 + +cd .dotfiles +sudo nixos-generate-config --dir /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix +git add /home/"$target_user"/.dotfiles/hosts/nixos/"$target_flake"/hardware-configuration.nix +green "Installing flake $target_flake" +sudo nixos-rebuild --show-trace --flake .#"$target_flake" --keep-going switch