nixos/scripts/bootstrap.sh

107 lines
3.8 KiB
Bash
Raw 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
2024-05-26 13:25:20 +01:00
Run 'passwd' and set a temporary password (something simple like '1234')
2024-05-14 18:26:45 +01:00
for the root user.
"
read -p "Confirm host had been setup using the above steps...(yes|no): " confirm
[ "$confirm" != "yes" ] && echo "Exiting" && exit 0
2024-05-23 13:37:11 +01:00
hostname="sparky"
2024-05-26 13:25:20 +01:00
ip="192.168.122.193"
2024-05-22 21:10:41 +01:00
config="bootstrap"
# 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 13:52:51 +01:00
# Create temp directory for ssh and luks to be copied to host:
2024-05-26 13:55:47 +01:00
temp=$(mktemp -d)
2024-05-22 17:23:17 +01:00
# Function to cleanup temporary directory on exit
cleanup() {
2024-05-26 13:55:47 +01:00
rm -rf "$temp"
2024-05-22 17:23:17 +01:00
}
trap cleanup EXIT
2024-05-26 13:52:51 +01:00
# Create the directory where services are to find the host keys
2024-05-26 13:55:47 +01:00
install -d -m755 "$temp/persist/etc/ssh"
install -d -m755 "$temp/tmp"
2024-05-26 13:52:51 +01:00
# Extract luks key from secrets
luks_key=$(nix-shell -p sops --run "SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt sops -d --extract '[""\"passwords""\"][""\"root""\"]' ../nix-secrets/secrets.yaml")
2024-05-26 13:55:47 +01:00
echo "$luks_key" > "$temp/tmp/luks_secrets.key"
2024-05-22 17:23:17 +01:00
2024-05-23 12:42:57 +01:00
# Create ssh keys if not exists
echo "Creating '$hostname' ssh keys"
2024-05-26 13:55:47 +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
2024-05-26 13:52:51 +01:00
# Change permissions
2024-05-26 13:55:47 +01:00
chmod 600 "$temp/persist/etc/ssh/ssh_host_ed25519_key"
chmod 644 "$temp/persist/etc/ssh/ssh_host_ed25519_key.pub"
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-05-26 13:55:47 +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:
2024-05-23 12:44:26 +01:00
SOPS_FILE="../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 14:02:11 +01:00
just update-sops-secrets && just update-flake-secrets && just update-flake
2024-05-23 13:38:43 +01:00
# Install Nixos to target
2024-05-14 18:26:45 +01:00
cd "$HOME/nixos"
2024-05-22 17:39:12 +01:00
git add . && git commit -m "auto: bootstrapping $hostname" && git push
2024-05-26 13:55:47 +01:00
SHELL=/bin/sh nix run github:nix-community/nixos-anywhere -- --extra-files "$temp" --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
# Check host OS has booted (and not booted back into live cd)
2024-05-15 19:44:13 +01:00
while true;
do
read -p "Confirm live CD has been removed... (yes|no): " confirm
2024-05-15 19:44:13 +01:00
[ "$confirm" = "yes" ] && break
done
echo "Waiting for $ip to come back online and port 22 to be open..."
while ! ping -c 1 $ip &> /dev/null || ! nc -zvw3 $ip 22 &> /dev/null
do
echo "$ip is still offline or port 22 is not open. Checking again in 5 seconds..."
sleep 5
done
echo "$ip is now online and port 22 is open!"
# Authorise source public key
echo "Copying pubkey to target host"
2024-05-22 21:10:41 +01:00
ssh-copy-id -i "$(readlink -f "$HOME/.ssh/id_ed25519.pub")" "root@$ip"
# Copy deploy_key to target for personal repo authorisation
2024-05-22 21:10:41 +01:00
scp -i "$(readlink -f "$HOME/.ssh/id_ed25519")" "$(readlink -f "$HOME/.ssh/deploy_key-ssh-ed25519")" "root@$ip:/persist/etc/ssh/deploy_key-ssh-ed25519"
2024-05-15 15:39:30 +01:00
2024-05-22 21:10:41 +01:00
ssh -i "$(readlink -f "$HOME/.ssh/id_ed25519")" "root@$ip" "nix-shell -p git --run 'git clone git@git.bitlab21.com:sam/nixos.git /persist/etc/nixos/'"
2024-05-19 23:33:42 +01:00
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