bootstrap script

This commit is contained in:
Sam 2024-05-14 18:26:45 +01:00
parent 51533f7d07
commit af75086a7e
4 changed files with 82 additions and 1 deletions

View File

@ -48,7 +48,6 @@
};
home.sessionPath = [
"$HOME/.scripts/"
];
gtk.enable = true;

View File

@ -78,6 +78,7 @@
inherit(pkgs)
wget
curl
git
rsync;
};

View File

@ -2,4 +2,5 @@
imports = [
./sops.nix
];
}

80
scripts/bootstrap.sh Executable file
View File

@ -0,0 +1,80 @@
#!/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
cd ~
read -p "Enter hostname: " hostname
read -p "Enter username: " username
read -p "Enter ip address: " ip
KEY_DIR="$HOME/keys/$hostname/"
mkdir -p $KEY_DIR
echo "Creating ssh keys for new host. Will create in current directory: "$KEY_DIR
KEY_NAME="ssh_ed25519_key_$hostname"
KNOWN_HOSTS=$(grep "$ip" ~/.ssh/known_hosts)
confirm_delete=""
[ "$KNOWN_HOSTS" != "" ] && echo -e "Host found in: ~/.ssh/known_hosts\n\n$KNOWN_HOSTS\n" && read -p "Delete existing hosts from ~/.ssh/known_hosts? (yes|no) " confirm_delete
[ "$confirm_delete" = "yes" ] && sed -i "/$ip/d" ~/.ssh/known_hosts
ssh-keygen -t ed25519 -f "$KEY_DIR/$KEY_NAME" -C "$hostname@$ip" -N ""
[ $? != 0 ] && echo "Error generating ssh keys" && exit 1
echo "Copying ssh key to target host:"
# Rsync currently not available in Nixos. Use scp instead.
#rsync -av "ssh_ed25519_key_$hostname"* "root@$ip:/etc/ssh/"
scp -i "$KEY_DIR/ssh_ed25519_key_$hostname"* "root@$ip:/etc/ssh/"
[ $? != 0 ] && echo "Error copying keys to target host" && exit 1
echo "Generating age key from ssh key"
nix-shell -p ssh-to-age --run "cat $KEY_NAME.pub | ssh-to-age > $KEY_DIR/age_key_$hostname"
[ $? != 0 ] && echo "Error generating age key" && exit 1
AGE_KEY=$(cat "$KEY_DIR/age_key_$hostname")
echo -e "Age key:\n$AGE_KEY\n"
echo "This needs to be inserted into .sops.yaml file."
# Install Nixos bootstrap
cd "$HOME/nixos"
SHELL=/bin/sh nix run github:nix-community/nixos-anywhere -- --flake .#bootstrap root@"$ip" -i "$KEY_DIR/ssh_ed25519_key_$hostname"
[ $? != 0 ] && echo "Error installing Nixos" && exit 1
echo "Deleting host from known_hosts"
sed -i "/$ip/d" ~/.ssh/known_hosts
echo -e "
Complete!
Remember to add the age key to .sops.yaml, like this:
keys:
- &hosts:
- &$hostname $AGE_KEY
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
- *$hostname
Then to update the keys for the secrets.yaml file, run:
'sops --config .sops.yaml updatekeys secrets.yaml'
"
echo "Copying gitea private ssh key to host"
rsync -av "$(readlink -n "$HOME/.ssh/openssh_key" )" "$username@$ip":~/.ssh/gitea
ssh "$username@$ip" "nix-shell -p git --run \"GIT_SSH_COMMAND='ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes' git clone git@git.bitlab21.com:sam/nixos.git\""
ssh "$username@$ip" "nix-shell -p git --run \"GIT_SSH_COMMAND='ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes' git clone git@git.bitlab21.com:sam/nix-secrets.git\""