auto: bootstrapping sparky

This commit is contained in:
Sam 2024-05-26 13:52:51 +01:00
parent b7d7f40171
commit dbcadf5315
3 changed files with 27 additions and 22 deletions

View File

@ -228,11 +228,11 @@
"nix-secrets": {
"flake": false,
"locked": {
"lastModified": 1716726314,
"narHash": "sha256-51OoLeW7G0MgtY6veCsbPxN+SMg6RjMzNAj9jb0QWNk=",
"lastModified": 1716727965,
"narHash": "sha256-NTsv/rWrB2coS62aKKD9GDR2mhzL1MMU+5VYDhh1y6w=",
"ref": "refs/heads/master",
"rev": "1260e25cdc7057ca61f947ec67b3aaf4de013852",
"revCount": 40,
"rev": "4973f9b8652f9a829510593f8b17448783054b5e",
"revCount": 41,
"type": "git",
"url": "ssh://git@git.bitlab21.com/sam/nix-secrets.git"
},

View File

@ -1,6 +1,4 @@
{device ? throw "Must define a device, e.g. /dev/sda", keyFile ? throw "LUKS password file not specified"}:
let
in
{device ? throw "Must define a device, e.g. /dev/sda"}:
{
disko.devices = {
disk = {
@ -28,11 +26,11 @@ in
type = "luks";
name = "crypted";
# disable settings.keyFile if you want to use interactive password entry
#passwordFile = "/tmp/secret.key"; # Interactive
settings = {
allowDiscards = true;
inherit keyFile;
};
passwordFile = "/tmp/luks_secret.key"; # Interactive
# settings = {
# allowDiscards = true;
# keyFile = "${sopsHashedPasswordFile}";
# };
content = {
type = "btrfs";
extraArgs = ["-f"];

View File

@ -23,28 +23,35 @@ sed -i "/$ip/d" ~/.ssh/known_hosts
echo "Copying pubkey to target host"
ssh-copy-id -i "$(readlink -f "$HOME/.ssh/id_ed25519.pub")" "root@$ip"
# Create temp directory for ssh keys to be copied to host:
temp=$(mktemp -d)
# Create temp directory for ssh and luks to be copied to host:
temp_ssh=$(mktemp -d)
temp_luks=$(mktemp -d)
# Function to cleanup temporary directory on exit
cleanup() {
rm -rf "$temp"
rm -rf "$temp_ssh" "$temp_luks"
}
trap cleanup EXIT
# Create the directory where sshd expects to find the host keys
install -d -m755 "$temp/persist/etc/ssh"
# Create the directory where services are to find the host keys
install -d -m755 "$temp_ssh/persist/etc/ssh"
install -d -m755 "$temp_luks/tmp"
# 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")
echo "$luks_key" > "$temp_luks/luks_secrets.key"
# Create ssh keys if not exists
echo "Creating '$hostname' ssh keys"
ssh-keygen -t ed25519 -f "$temp/persist/etc/ssh/ssh_host_ed25519_key" -C root@"$hostname" -N ""
ssh-keygen -t ed25519 -f "$temp_ssh/persist/etc/ssh/ssh_host_ed25519_key" -C root@"$hostname" -N ""
chmod 600 "$temp/persist/etc/ssh/ssh_host_ed25519_key"
chmod 644 "$temp/persist/etc/ssh/ssh_host_ed25519_key.pub"
# Change permissions
chmod 600 "$temp_ssh/persist/etc/ssh/ssh_host_ed25519_key"
chmod 644 "$temp_ssh/persist/etc/ssh/ssh_host_ed25519_key.pub"
# 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")
HOST_AGE_KEY=$(nix-shell -p ssh-to-age --run "cat $temp_ssh/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:
@ -65,7 +72,7 @@ just update-sops-secrets && just update-flake-secrets && just update-flake
cd "$HOME/nixos"
git add . && git commit -m "auto: bootstrapping $hostname" && git push
SHELL=/bin/sh nix run github:nix-community/nixos-anywhere -- --extra-files "$temp" --flake .#"$config" root@"$ip" -i "$HOME/.ssh/id_ed25519"
SHELL=/bin/sh nix run github:nix-community/nixos-anywhere -- --extra-files "$temp_ssh" "$temp_luks" --flake .#"$config" root@"$ip" -i "$HOME/.ssh/id_ed25519"
[ $? != 0 ] && echo "Error installing Nixos" && exit 1
## Delete keys from local known_hosts