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 ( something simple like '1234' )
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-19 21:55:25 +01:00
# Target host details
2024-05-22 17:23:17 +01:00
#cd ~
#read -p "Enter hostname: " hostname
#read -p "Enter username: " username
#read -p "Enter ip address: " ip
#read -p "Enter nixosSystem to build, e.g. 'bootstrap': " config
hostname = "sparky"
username = "media"
ip = "192.168.122.192"
config = "sparky"
2024-05-19 23:33:42 +01:00
2024-05-21 19:11:23 +01:00
echo "
Details entered:
Hostname: $hostname
Username: $username
IP address: $ip
System to build: $config
"
read -p "Is this correct? (yes|no): " check_details
[ " $check_details " != "yes" ] && echo "Try again. Exiting" && exit 0
2024-05-19 23:33:42 +01:00
# Generate key name and dir
2024-05-21 19:11:23 +01:00
HOST_KEY_DIR = " $HOME /keys/hosts/ $hostname "
USER_KEY_DIR = " $HOME /keys/users/ $username "
mkdir -p " $HOST_KEY_DIR " " $USER_KEY_DIR "
2024-05-14 18:26:45 +01:00
2024-05-21 19:11:23 +01:00
# Create ssh keys if not exists
echo " Creating ' $hostname ' ssh keys "
bash " / $HOME /nixos/scripts/generate_ssh_keys.sh " --type "host" --username "root" --hostname " $hostname "
echo " Creating ' $username ' ssh keys "
bash " / $HOME /nixos/scripts/generate_ssh_keys.sh " --type "user" --username " $username " --hostname " $hostname "
2024-05-14 18:26:45 +01:00
2024-05-21 19:11:23 +01:00
# Delete key in known hosts if exists
sed -i " / $ip /d " ~/.ssh/known_hosts
2024-05-19 21:55:25 +01:00
# Authorise source public key
echo "Copying pubkey to target host"
2024-05-21 19:11:23 +01:00
ssh-copy-id -i " $( readlink -f " $HOME /.ssh/id_ed25519.pub " ) " " root@ $ip "
# Generate age key from target host and user public ssh key
echo "Generating age key from target host and user ssh key"
nix-shell -p ssh-to-age --run " cat $HOST_KEY_DIR /ssh_host_ed25519_key.pub | ssh-to-age > $HOST_KEY_DIR /age_host_key "
nix-shell -p ssh-to-age --run " cat $USER_KEY_DIR /id_ed25519.pub | ssh-to-age > $USER_KEY_DIR /age_user_key "
HOST_AGE_KEY = $( cat " $HOST_KEY_DIR /age_host_key " )
USER_AGE_KEY = $( cat " $USER_KEY_DIR /age_user_key " )
echo -e " Host age key:\n $HOST_AGE_KEY \n "
echo -e " User age key:\n $USER_AGE_KEY \n "
echo "These keys needs to be inserted into .sops.yaml file. This will be prompted again later."
2024-05-19 21:55:25 +01:00
2024-05-22 17:23:17 +01:00
# Create temp directory for ssh keys to be copied to host:
temp = $( mktemp -d)
# Function to cleanup temporary directory on exit
cleanup( ) {
rm -rf " $temp "
}
trap cleanup EXIT
# Create the directory where sshd expects to find the host keys
install -d -m755 " $temp /persist/etc/ssh "
cat " $HOST_KEY_DIR /ssh_host_ed25519_key " > " $temp /persist/etc/ssh/ssh_host_ed25519_key "
cat " $HOST_KEY_DIR /ssh_host_ed25519_key.pub " > " $temp /persist/etc/ssh/ssh_host_ed25519_key.pub "
chmod 600 " $temp /persist/etc/ssh/ssh_host_ed25519_key "
chmod 644 " $temp /persist/etc/ssh/ssh_host_ed25519_key.pub "
2024-05-19 21:55:25 +01:00
# Install Nixos to target
2024-05-14 18:26:45 +01:00
cd " $HOME /nixos "
2024-05-22 17:23:17 +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
2024-05-19 21:55:25 +01:00
## 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-19 21:55:25 +01:00
# Check host OS has booted (and not booted back into live cd)
2024-05-15 19:44:13 +01:00
while true;
do
2024-05-19 21:55:25 +01:00
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! "
2024-05-22 17:23:17 +01:00
## Copy host ssh keys to target
#scp -i "$(readlink -f "$HOME/.ssh/id_ed25519")" "$HOST_KEY_DIR/ssh_host_ed25519_key" "root@$ip:/etc/ssh/ssh_host_ed25519_key"
#scp -i "$(readlink -f "$HOME/.ssh/id_ed25519")" "$HOST_KEY_DIR/ssh_host_ed25519_key.pub" "root@$ip:/etc/ssh/ssh_host_ed25519_key.pub"
#
2024-05-19 21:55:25 +01:00
# Authorise source public key
echo "Copying pubkey to target host"
2024-05-21 19:11:23 +01:00
ssh-copy-id -i " $( readlink -f " $HOME /.ssh/id_ed25519.pub " ) " " $username @ $ip "
2024-05-19 21:55:25 +01:00
2024-05-21 19:11:23 +01:00
# Copy user ssh keys to target
2024-05-22 17:23:17 +01:00
scp -i " $( readlink -f " $HOME /.ssh/id_ed25519 " ) " " $USER_KEY_DIR /id_ed25519 " " $username @ $ip :~/.ssh/id_ed25519 "
scp -i " $( readlink -f " $HOME /.ssh/id_ed25519 " ) " " $USER_KEY_DIR /id_ed25519.pub " " $username @ $ip :~/.ssh/id_ed25519.pub "
2024-05-21 19:11:23 +01:00
# Copy deploy_key to target for personal repo authorisation
2024-05-22 17:23:17 +01:00
scp -i " $( readlink -f " $HOME /.ssh/id_ed25519 " ) " " $( readlink -f " $HOME /.ssh/deploy_key-ssh-ed25519 " ) " " $username @ $ip :~/.ssh/deploy_key-ssh-ed25519 "
scp -i " $( readlink -f " $HOME /.ssh/id_ed25519 " ) " " $( readlink -f " $HOME /.ssh/deploy_key-ssh-ed25519.pub " ) " " $username @ $ip :~/.ssh/deploy_key-ssh-ed25519.pub "
2024-05-20 00:33:06 +01:00
2024-05-19 21:55:25 +01:00
echo "Configuring target host ssh connection to enable connection to gitea repos:"
2024-05-15 15:39:30 +01:00
read -r -d '' config << EOM
Host git.bitlab21.com
IdentitiesOnly yes
2024-05-15 19:44:13 +01:00
StrictHostKeyChecking no
2024-05-17 20:17:53 +01:00
IdentityFile ~/.ssh/deploy_key-ssh-ed25519
2024-05-15 15:39:30 +01:00
EOM
2024-05-15 19:44:13 +01:00
echo " $config " | ssh " $username @ $ip " 'mkdir -p ~/.ssh/ && cat > ~/.ssh/config'
2024-05-15 15:39:30 +01:00
2024-05-14 18:26:45 +01:00
echo -e "
Complete!
2024-05-19 21:55:25 +01:00
Now add the new target host age key to .sops.yaml. This is needed to enable the
new host to decrypt the secrets.yaml file from the ssh key we generated
previously.
Enter the details as following:
2024-05-14 18:26:45 +01:00
keys:
2024-05-21 19:11:23 +01:00
- & users:
- & $username $USER_AGE_KEY
2024-05-14 18:26:45 +01:00
- & hosts:
2024-05-21 19:11:23 +01:00
- & $hostname $HOST_AGE_KEY
2024-05-14 18:26:45 +01:00
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
2024-05-21 19:11:23 +01:00
- *$username
2024-05-14 18:26:45 +01:00
- *$hostname
2024-05-19 21:55:25 +01:00
Then update ( i.e. re-encrypt) the secrets.yaml file with the new keys, run:
'sops --config .sops.yaml updatekeys secrets.yaml'
or with just:
'just update-sops-secrets'
Then commit and push these changes to remote so they can be accessed on the new
host.
2024-05-14 18:26:45 +01:00
"
2024-05-15 15:39:30 +01:00
while true;
do
2024-05-15 19:44:13 +01:00
read -p "Confirm keys have been added to .sops.yaml using the above steps, and the changes (if any) have been commited and pushed...(yes|no): " confirm
2024-05-15 15:39:30 +01:00
[ " $confirm " = "yes" ] && break
done
2024-05-22 17:23:17 +01:00
ssh -i " $( readlink -f " $HOME /.ssh/id_ed25519 " ) " " $username @ $ip " "nix-shell -p git --run 'git clone git@git.bitlab21.com:sam/nixos.git ~/keep/'"
ssh -i " $( readlink -f " $HOME /.ssh/id_ed25519 " ) " " $username @ $ip " "nix-shell -p git --run 'git clone git@git.bitlab21.com:sam/nix-secrets.git ~/keep/'"
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###"
#echo "Please remote into the new host and run 'sudo nixos-generate-config && cp /etc/nixos/hardware-configuration.nix /home/$username/nixos/hosts/$hostname/'"
2024-05-19 23:33:42 +01:00
2024-05-22 17:23:17 +01:00
exit 0