nixos/scripts/bootstrap.sh

86 lines
3.1 KiB
Bash
Raw Permalink Normal View History

2024-05-14 18:26:45 +01:00
#!/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
2024-05-14 18:26:45 +01:00
"
read -p "Confirm host had been setup using the above steps...(yes|no): " confirm
[ "$confirm" != "yes" ] && echo "Exiting" && exit 0
2024-05-27 16:19:00 +01:00
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
2024-06-28 20:21:27 +01:00
read -p "Using impermanence? (yes|no): " impermanence
[ "$impermanence" = "yes" ] && persist="/persist"
# Delete key in known hosts if exists
2024-05-26 13:25:20 +01:00
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"
2024-05-26 14:18:14 +01:00
# Create temp directory for ssh and luks keys to be copied to host:
temp=$(mktemp -d)
2024-05-26 14:18:14 +01:00
touch /tmp/luks_secret.key
2024-05-22 17:23:17 +01:00
# Function to cleanup temporary directory on exit
cleanup() {
rm -rf "$temp" /tmp/luks_secret.key
2024-05-22 17:23:17 +01:00
}
trap cleanup EXIT
# Create the directory for target host keys
2024-06-28 20:21:27 +01:00
install -d -m755 "$temp$persist/etc/ssh"
2024-05-22 17:23:17 +01:00
# Create ssh keys
2024-05-23 12:42:57 +01:00
echo "Creating '$hostname' ssh keys"
2024-06-28 20:21:27 +01:00
ssh-keygen -t ed25519 -f "$temp$persist/etc/ssh/ssh_host_ed25519_key" -C root@"$hostname" -N ""
2024-05-22 17:23:17 +01:00
# 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
2024-05-22 17:23:17 +01:00
2024-05-23 12:42:57 +01:00
# Generate age key from target host and user public ssh key
echo "Generating age key from target host and user ssh key"
2024-06-28 20:21:27 +01:00
HOST_AGE_KEY=$(nix-shell -p ssh-to-age --run "cat $temp$persist/etc/ssh/ssh_host_ed25519_key.pub | ssh-to-age")
2024-05-23 12:42:57 +01:00
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"
2024-05-23 12:42:57 +01:00
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"
2024-05-23 12:42:57 +01:00
# 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"
2024-05-26 14:07:26 +01:00
# 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"
2024-05-14 18:26:45 +01:00
[ $? != 0 ] && echo "Error installing Nixos" && exit 1
## Delete keys from local known_hosts
2024-05-14 18:26:45 +01:00
echo "Deleting host from known_hosts"
sed -i "/$ip/d" ~/.ssh/known_hosts
2024-05-22 17:23:17 +01:00
echo -e "###\nSuccessfully installed Nixos on the target host!\n###"
2024-05-19 23:33:42 +01:00
2024-05-22 17:23:17 +01:00
exit 0