#!/usr/bin/env bash echo -e " Before using this tool, ensure that the host has been setup correctly. Boot the latest Nixos-minimal install ISO on the host and access the tty. Use 'ip a' to get the ip address, then 'sudo su' to change to root. Finally Run 'passwd' and set a temporary password for the root user. Also, ensure secrets for the new host and users have been set in secrets.yaml " read -p "Confirm host had been setup using the above steps...(yes|no): " confirm [ "$confirm" != "yes" ] && echo "Exiting" && exit 0 read -p "Enter hostname of target: " hostname read -p "Enter IP of target: " ip read -p "Enter config to install on target: " config read -p "Enter username (if none, use 'root'): " username read -p "Using impermanence? (yes|no): " impermanence [ "$impermanence" = "yes" ] && persist="/persist" # Delete key in known hosts if exists sed -i "/$ip/d" ~/.ssh/known_hosts # Authorise source public key echo "Copying pubkey to target host" ssh-copy-id -i "$(readlink -f "$HOME/.ssh/id_ed25519.pub")" "root@$ip" # Create temp directory for ssh and luks keys to be copied to host: temp=$(mktemp -d) touch /tmp/luks_secret.key # Function to cleanup temporary directory on exit cleanup() { rm -rf "$temp" /tmp/luks_secret.key } trap cleanup EXIT # Create the directory for target host keys install -d -m755 "$temp$persist/etc/ssh" # Create ssh keys echo "Creating '$hostname' ssh keys" ssh-keygen -t ed25519 -f "$temp$persist/etc/ssh/ssh_host_ed25519_key" -C root@"$hostname" -N "" # Extract luks key from secrets luks_secret=$(nix-shell -p sops --run "SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt sops -d --extract '[""\"luks_passphrase""\"][""\"$hostname""\"]' ~/.local/share/src/nix-secrets/secrets.yaml") echo "$luks_secret" > /tmp/luks_secret.key # Generate age key from target host and user public ssh key echo "Generating age key from target host and user ssh key" HOST_AGE_KEY=$(nix-shell -p ssh-to-age --run "cat $temp$persist/etc/ssh/ssh_host_ed25519_key.pub | ssh-to-age") echo -e "Host age key:\n$HOST_AGE_KEY\n" # Update .sops.yaml with new age key: SOPS_FILE="$HOME/.local/share/src/nix-secrets/.sops.yaml" sed -i "{ # Remove any * and & entries for this host /[*&]$hostname/ d; # Inject a new age: entry # n matches the first line following age: and p prints it, then we transform it while reusing the spacing /age:/{n; p; s/\(.*- \*\).*/\1$hostname/}; # Inject a new hosts: entry /&hosts:/{n; p; s/\(.*- &\).*/\1$hostname $HOST_AGE_KEY/} }" "$SOPS_FILE" # Commit and push changes to sops file just update-sops-secrets && just update-flake-secrets && just update-flake # Copy current nix config over to target echo "copying current nix config to host" cp -pr . "$temp$persist/etc/nixos" # Install Nixos to target SHELL=/bin/sh nix run github:nix-community/nixos-anywhere/1.3.0 -- --extra-files "$temp" --disk-encryption-keys /tmp/luks_secret.key /tmp/luks_secret.key --flake .#"$config" root@"$ip" -i "$HOME/.ssh/id_ed25519" [ $? != 0 ] && echo "Error installing Nixos" && exit 1 ## Delete keys from local known_hosts echo "Deleting host from known_hosts" sed -i "/$ip/d" ~/.ssh/known_hosts echo -e "###\nSuccessfully installed Nixos on the target host!\n###" exit 0