2020-10-11 14:21:12 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
|
|
|
|
# Script to install NixOS from the Hetzner Cloud NixOS bootable ISO image.
|
2020-10-20 15:04:50 +00:00
|
|
|
# (tested with Hetzner's `NixOS 20.03 (amd64/minimal)` ISO image).
|
2020-12-27 21:45:07 +00:00
|
|
|
#
|
2020-10-20 15:15:31 +00:00
|
|
|
# This script wipes the disk of the server!
|
2020-10-20 15:04:50 +00:00
|
|
|
#
|
2020-10-20 15:15:31 +00:00
|
|
|
# Instructions:
|
2020-10-20 15:04:50 +00:00
|
|
|
#
|
2020-10-20 15:15:31 +00:00
|
|
|
# 1. Mount the above mentioned ISO image from the Hetzner Cloud GUI
|
|
|
|
# and reboot the server into it; do not run the default system (e.g. Ubuntu).
|
|
|
|
# 2. To be able to SSH straight in (recommended), you must replace hardcoded pubkey
|
|
|
|
# further down in the section labelled "Replace this by your SSH pubkey" by you own,
|
|
|
|
# and host the modified script way under a URL of your choosing
|
|
|
|
# (e.g. gist.github.com with git.io as URL shortener service).
|
|
|
|
# 3. Run on the server:
|
2020-10-11 14:21:12 +00:00
|
|
|
#
|
2020-10-20 15:15:31 +00:00
|
|
|
# # Replace this URL by your own that has your pubkey in
|
|
|
|
# curl -L https://raw.githubusercontent.com/nix-community/nixos-install-scripts/master/hosters/hetzner-cloud/nixos-install-hetzner-cloud.sh | sudo bash
|
2020-12-30 16:13:11 +00:00
|
|
|
#
|
|
|
|
# This will install NixOS and power off the server.
|
2020-10-20 15:15:31 +00:00
|
|
|
# 4. Unmount the ISO image from the Hetzner Cloud GUI.
|
2020-12-30 16:13:11 +00:00
|
|
|
# 5. Turn the server back on from the Hetzner Cloud GUI.
|
2020-10-11 14:21:12 +00:00
|
|
|
#
|
|
|
|
# To run it from the Hetzner Cloud web terminal without typing it down,
|
2020-10-16 01:37:49 +00:00
|
|
|
# you can either select it and then middle-click onto the web terminal, (that pastes
|
2020-10-20 15:15:31 +00:00
|
|
|
# to it), or use `xdotool` (you have e.g. 3 seconds to focus the window):
|
2020-10-11 14:21:12 +00:00
|
|
|
#
|
2020-10-20 15:15:31 +00:00
|
|
|
# sleep 3 && xdotool type --delay 50 'curl YOUR_URL_HERE | sudo bash'
|
2020-10-11 14:21:12 +00:00
|
|
|
#
|
|
|
|
# (In the xdotool invocation you may have to replace chars so that
|
|
|
|
# the right chars appear on the US-English keyboard.)
|
2020-10-20 15:15:31 +00:00
|
|
|
#
|
|
|
|
# If you do not replace the pubkey, you'll be running with my pubkey, but you can
|
|
|
|
# change it afterwards by logging in via the Hetzner Cloud web terminal as `root`
|
|
|
|
# with empty password.
|
2020-10-11 14:21:12 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Hetzner Cloud OS images grow the root partition to the size of the local
|
2020-10-20 15:04:50 +00:00
|
|
|
# disk on first boot. In case the NixOS live ISO is booted immediately on
|
2020-10-11 14:21:12 +00:00
|
|
|
# first powerup, that does not happen. Thus we need to grow the partition
|
|
|
|
# by deleting and re-creating it.
|
|
|
|
sgdisk -d 1 /dev/sda
|
|
|
|
sgdisk -N 1 /dev/sda
|
|
|
|
partprobe /dev/sda
|
|
|
|
|
|
|
|
mkfs.ext4 -F /dev/sda1 # wipes all data!
|
|
|
|
|
|
|
|
mount /dev/sda1 /mnt
|
|
|
|
|
|
|
|
nixos-generate-config --root /mnt
|
|
|
|
|
|
|
|
# Delete trailing `}` from `configuration.nix` so that we can append more to it.
|
|
|
|
sed -i -E 's:^\}\s*$::g' /mnt/etc/nixos/configuration.nix
|
|
|
|
|
|
|
|
# Extend/override default `configuration.nix`:
|
|
|
|
echo '
|
|
|
|
boot.loader.grub.devices = [ "/dev/sda" ];
|
|
|
|
|
|
|
|
# Initial empty root password for easy login:
|
|
|
|
users.users.root.initialHashedPassword = "";
|
|
|
|
services.openssh.permitRootLogin = "prohibit-password";
|
|
|
|
|
|
|
|
services.openssh.enable = true;
|
|
|
|
|
|
|
|
users.users.root.openssh.authorizedKeys.keys = [
|
2020-12-27 21:45:07 +00:00
|
|
|
# Replace this by your SSH pubkey!
|
|
|
|
"ssh-rsa AAAAAAAAAAA..."
|
2020-10-11 14:21:12 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
' >> /mnt/etc/nixos/configuration.nix
|
|
|
|
|
|
|
|
nixos-install --no-root-passwd
|
|
|
|
|
2020-12-30 16:13:11 +00:00
|
|
|
poweroff
|