#!/bin/env fish function runSetup set -l dir (status dirname) source "$dir/../../lib/action.fish" set -l projectRoot (realpath "$dir/../../..") set -l projectName (basename "$projectRoot") set -l PROJECT_CLONE_ROOT "/opt/$(basename "$projectName")" function setupAction -V dir -V projectRoot -V PROJECT_CLONE_ROOT source "$dir/../../lib/hooks.fish" source "$dir/../../lib/nix.fish" source "$dir/../../lib/restoration.fish" source "$dir/../../lib/settings.fish" set -l mountDir (getOSConfig partition.rootDir) if not type -q runChroot function runChroot -S command chroot $argv end end function getCloneFile -S -a path set -l relativeDir (realpath --relative-to "$projectRoot" "$dir") set -l relativePath (realpath --relative-to "$dir" "$path") echo "$PROJECT_CLONE_ROOT/$relativeDir/$relativePath" end function runInOS -S set -l script /root/run_once wrapScript $argv | chroot "$mountDir" tee "$script" >/dev/null and runChroot "$mountDir" chmod +x "$script" and runChroot "$mountDir" "$script" and runChroot "$mountDir" rm "$script" end function wrapScript -S printf %s\n \ "cd $PROJECT_CLONE_ROOT" \ "$argv" end initBackupConfig --action restore echo "Partitioning drives..." begin set -l script (mktemp) chmod +x "$script" and getOSConfig partition.script >"$script" and "$script" and rm "$script" end if [ -n "$VALHALLA_BACKUP_SERVER_KEY" ] set -l knownHosts /root/.ssh/known_hosts mkdir -p (dirname "$mountDir$VALHALLA_BACKUP_SERVER_KEY") mkdir -p (dirname "$mountDir$knownHosts") cp "$VALHALLA_BACKUP_SERVER_KEY" "$mountDir$VALHALLA_BACKUP_SERVER_KEY" cp $knownHosts "$mountDir$knownHosts" end and echo "Cloning project..." and source "$dir/../../lib/copy-repo.fish" "$mountDir$PROJECT_CLONE_ROOT" and begin set -l path "$mountDir/$nixPkgsDir" and mkdir -p (dirname "$path") and cp -r "$nixPkgsDir" "$path" end and runHook initOS "Please set up a function `initOS` for initializing the mounted OS and installing valhalla dependencies" and runChroot "$mountDir" git config --system --add safe.directory "$PROJECT_CLONE_ROOT" and runHook --force bootstrapSetup "Please set up a function `bootstrapSetup` for installing `fish` into the " and runHook setupOS and echo "Preparing auto-login..." and runHook --force autologin "Please set up a function `autologin` for setting up autologin for the `root` user" and begin set -l script (string escape (getCloneFile (getInstallerScript))) wrapScript ( string join " " \ (begin for var in \ CONFIG_NAME \ VALHALLA_BACKUP_DISABLED \ VALHALLA_BACKUP_DIR \ VALHALLA_BACKUP_SERVER \ VALHALLA_BACKUP_SERVER_PORT \ VALHALLA_BACKUP_SERVER_USER \ VALHALLA_BACKUP_SERVER_KEY echo "$var=$(string escape "$$var")" end end) \ (string escape $script)) end | runChroot "$mountDir" tee /root/.bash_profile >/dev/null end function actionPostRun echo "Setup finished!" and echo "This machine will reboot in 5 seconds..." and echo "Press CTRL-C to abort..." and sleep 5 and systemctl reboot end runAction setupAction end