nixos-anywhere-example/template/install-helper.sh

112 lines
2.7 KiB
Bash
Raw Normal View History

2023-10-12 13:43:30 +02:00
#!/usr/bin/env sh
set -e
#
# This script is based on the work of Solomon <ssbothwell@gmail.com>.
# https://github.com/solomon-b/nixos-config/blob/ca047bdbb95859ee902e4750a3b0e018f2396bfe/installer/install-server.sh
#
cleanup() {
2023-10-16 18:32:46 +02:00
printf '%s\n' 'Cleanup on exit.'
if [ -d "${temp}" ]; then
printf '%s\n' 'Deleting local copy of SSH ed25519 key ...'
rm -rf "${temp}"
fi
if [ -d "${pwd_temp}" ]; then
printf '%s\n' 'Deleting local copy of disk encryption password ...'
rm -rf "${pwd_temp}"
fi
2023-10-12 13:43:30 +02:00
}
2023-11-03 13:42:11 +01:00
temp_dir(){
# Cleanup temporary directories on exit.
trap cleanup EXIT
2023-10-12 13:43:30 +02:00
# Create a temporary directory.
temp="$(mktemp -d)"
2023-11-03 13:42:11 +01:00
}
2023-10-12 13:43:30 +02:00
2023-11-03 13:42:11 +01:00
gen_ssh_key() {
# Create parent directories.
2023-10-12 13:43:30 +02:00
install -d -m755 "${temp}/etc/ssh"
2023-11-03 13:42:11 +01:00
# Generate SSH host key.
2023-10-12 13:43:30 +02:00
ssh-keygen -t ed25519 -f "${temp}/etc/ssh/ssh_host_ed25519_key" -q -N ""
}
2023-10-16 18:32:46 +02:00
ssh_fingerprint() {
2023-11-03 13:42:11 +01:00
printf '%s\n' 'host SSH ed25519 fingerprint:'
2023-10-16 18:32:46 +02:00
ssh-keygen -lf "${temp}/etc/ssh/ssh_host_ed25519_key"
}
2023-11-03 13:42:11 +01:00
gen_initrd_ssh_key() {
# Create parent directories.
install -d -m755 "${temp}/etc/secrets/initrd"
# Generate initrd SSH key.
ssh-keygen -t ed25519 -f "${temp}/etc/secrets/initrd/ssh_host_ed25519_key" -q -N ""
}
initrd_ssh_fingerprint() {
printf '%s\n' 'initrd SSH ed25519 fingerprint:'
ssh-keygen -lf "${temp}/etc/secrets/initrd/ssh_host_ed25519_key"
}
2023-10-16 18:32:46 +02:00
save_pwd() {
# Create a temporary directory.
pwd_temp="$(mktemp -d)"
# Get password from user without echoing.
# https://stackoverflow.com/a/3980713
stty -echo
printf "Disk encryption password: "
read -r password
stty echo
printf "\n"
stty -echo
printf "Retype disk encryption password: "
read -r password2
stty echo
printf "\n"
if [ "${password}" != "${password2}" ]; then
printf '%s\n' 'Passwords don'\''t match!'
return 1
fi
# Create password-file.
install -m600 /dev/stdin "${pwd_temp}/pwd.key" << EOF
${password}
EOF
}
2023-10-12 13:43:30 +02:00
main(){
num_args=2
if [ "$#" -ne "${num_args}" ]; then
printf '%s%s%s\n' 'ERROR: ' "${num_args}" ' arguments required'
return 1
fi
for i in "$@"; do
if [ -z "${i}" ]; then
printf '%s\n' 'ERROR: All given args must not be empty'
return 1
fi
done
ssh_port="${1}"
ssh_target="${2}"
2023-11-03 13:42:11 +01:00
temp_dir
2023-10-12 13:43:30 +02:00
gen_ssh_key
2023-11-03 13:42:11 +01:00
gen_initrd_ssh_key
2023-10-16 18:32:46 +02:00
save_pwd
# echo "$temp"
# echo "$pwd_temp"
# echo "Press enter start the installation:"
# read -r _foo
2023-10-12 13:43:30 +02:00
# Install NixOS to the target machine with our secrets.
nix --extra-experimental-features nix-command --extra-experimental-features flakes \
2023-10-16 18:32:46 +02:00
run github:numtide/nixos-anywhere -- --extra-files "${temp}" \
--disk-encryption-keys /tmp/secret.key "${pwd_temp}/pwd.key" --flake '.#mysystem' \
-p "${ssh_port}" "${ssh_target}"
ssh_fingerprint
2023-11-03 13:42:11 +01:00
initrd_ssh_fingerprint
2023-10-12 13:43:30 +02:00
}
main "$@"