Skip to content

Instantly share code, notes, and snippets.

@FlorianFranzen
Created March 26, 2021 12:58
Show Gist options
  • Save FlorianFranzen/a36ad6c6818b6f23204dd0a1500dc0a4 to your computer and use it in GitHub Desktop.
Save FlorianFranzen/a36ad6c6818b6f23204dd0a1500dc0a4 to your computer and use it in GitHub Desktop.
Bash script to install basic hetzner cloud instance
#! /usr/bin/env bash
# Script to install NixOS from the Hetzner Cloud NixOS bootable ISO image.
# (tested with Hetzner's `NixOS 20.03 (amd64/minimal)` ISO image).
#
# This script wipes the disk of the server!
#
# Instructions:
#
# 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:
#
# # 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
#
# This will install NixOS and power off the server.
# 4. Unmount the ISO image from the Hetzner Cloud GUI.
# 5. Turn the server back on from the Hetzner Cloud GUI.
#
# To run it from the Hetzner Cloud web terminal without typing it down,
# you can either select it and then middle-click onto the web terminal, (that pastes
# to it), or use `xdotool` (you have e.g. 3 seconds to focus the window):
#
# sleep 3 && xdotool type --delay 50 'curl YOUR_URL_HERE | sudo bash'
#
# (In the xdotool invocation you may have to replace chars so that
# the right chars appear on the US-English keyboard.)
#
# 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.
set -e
# Hetzner Cloud OS images grow the root partition to the size of the local
# disk on first boot. In case the NixOS live ISO is booted immediately on
# 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" ];
boot.kernelPackages = pkgs.linuxPackages_hardened;
security.hideProcessInformation = true;
# Enable flake support
nix = {
package = pkgs.nixFlakes;
extraOptions = ''
experimental-features = nix-command flakes ca-references
'';
};
# Set your time zone.
time.timeZone = "Europe/Amsterdam";
# Enable SSH login via key
services.openssh = {
enable = true;
passwordAuthentication = false;
permitRootLogin = "no";
};
security.pam.enableSSHAgentAuth = true;
# Enable fail2ban
services.fail2ban.enable = true;
# Enable time synchronization
services.ntp.enable = true;
# Add default users
users.users.admin = {
isNormalUser = true;
uid = 1000;
description = "Admin User";
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCXZ+XdLV1D5NqKCqY/PU2ntCm1v7zwtBXEK6px12E04d4lS6fjnbJ+bEaw1GtHI/KMhz+rmFRVVGd/gjONtGJ6BCxHelHcuw0/sCvuuwYk8WkdSRD7nZ5CEJt+xiyqcUC4uzCd9woR/+i7vfNTabPvx1+fnlCS/+HmpViv7UiaOXxzsqHPYCOXWl3I+EiI0UGOGJoZFAnfb+7yNX0SnzCZsLZJ5s2jr5V1NqEql70d3JXQDY4uQSsbMjdsc0jopc1ICcDqK9td/WdV4votSggY0Mz6oLw3vcdp+kyIreoGiGF+1yMIc03xIRPDzrA/Y2rascImLpBBV3zZQ6rSOYPhufWs74aueVZTiv4MMIxb4R+yZs93BHEtd76wQTE+VnxXTkvA8Lb2doh3WrcdX+2tiDWUXfjNg0Ko0QNH8liY7KuoeP4hgx2iSWpHoGOhYP1006es/TgFtWo21ZHmGiuPC/mWwUjLFddbP+ERqIaGtuE+62dJZTK3inD3xQqtSivTW1ZOcViZWLhM5mFYj6Hlklboi6HbDydLi9nTat+yfF+5KOiH69HwPjtTIrfTUIndQqTZSfRi/MLPZRfxLo3UeqSPWnbJe7A56Mc3kvlQbjrNkcJTwMIeIiVbDTwtoXiMwk8Z/r/R7+9Z1r2/6uzBLPaiNNdxZ/E6EKl7ThaAHQ== cardno:000604936884"
];
};
}
' >> /mnt/etc/nixos/configuration.nix
nixos-install --no-root-passwd
poweroff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment