This is the gist of how to setup a NixOS server on a Hetzner Cloud instance with an admin
user, ssh access and configuration management via git.
- Create a Hetzner Cloud instance and click to enter it.
- Stop the instance (top right corner icon).
- Go to ISO Images.
- Search for "nixos" and click mount.
- Start the instance again (top right corner icon).
- Open the console (top right corner icon).
- Get the IP address by executing
ip --brief --color address
. The address can also be optained from the Hetzner Cloud web interface.
lo UNKNOWN 127.0.0.1/8 ::1/128
enp61s0 UP <IPv4 address>/24 <IPv6 address>/64
- Download your public ssh key to the machine. If you have a GitHub account you can download it from there.
mkdir ~/.ssh
curl -L https://github.com/<username>.keys | tee -a .ssh/authorized_keys
- Log on from your computer on via ssh.
ssh nixos@<IPv4 address>
- Partition, format and mountthe disk.
sudo -i
# Create a GPT partition table.
parted /dev/sda -- mklabel msdos
# Add the root partition.
parted /dev/sda -- mkpart primary 1MiB 100%
partprobe
# Format the partition.
mkfs.ext4 -L nixos /dev/sda1
# Mount root.
mount /dev/disk/by-label/nixos /mnt
- Generate the initial configuration and install.
nixos-generate-config --root /mnt
# Set boot device.
sed -e 's/^.*boot\.loader\.grub\.device.*$/ boot.loader.grub.device = "\/dev\/sda";/g' -i /mnt/etc/nixos/configuration.nix
# Create a directory to manage configurations and import it in the generated config.
mkdir /mnt/etc/nixos/configuration/
sed -e 's/\.\/hardware-configuration\.nix/.\/hardware-configuration.nix\n .\/configuration/g' -i /mnt/etc/nixos/configuration.nix
- Open
/mnt/etc/nixos/configuration/default.nix
with an editor likevim
ornano
and paste the following.
{ config, pkgs, ... }:
{
imports =
[
./user.nix
];
environment.systemPackages = with pkgs; [
vim
wget
git
htop bottom
];
services.openssh.enable = true;
}
- Open
/mnt/etc/nixos/configuration/user.nix
with an editor and paste the following to create an account with the name admin. Replace the public ssh key with yours.
{ config, pkgs, ... }:
{
imports = [
];
users = {
users = {
admin = {
isNormalUser = true;
extraGroups = [ "wheel" ];
initialHashedPassword = "";
openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAA...." ];
};
};
};
}
- Perform installation.
nixos-install --no-root-passwd
- Enter the mounted installation and set a password for the admin account.
nixos-enter --root /mnt
passwd admin
exit
- Shut down the machine.
poweroff
- Unmount the ISO in the Hetzner Cloud interface and start the machine again.
- Log on via SSH.
# Remove old host key from install system.
ssh-keygen -R <IPv4 address>
# Log in as admin
ssh admin@<IPv4 address>
- Turn custom config into repo of admin for convenience
mkdir repos
cp -R /etc/nixos/configuration/ repos/
cd repos/configuration/
# Initialize repository with placeholder configuration.
git init
git config user.name admin
git config user.email admin@localhost
git add .
git commit -m "initial commit"
# Remove replace old files with symbolic link.
sudo rm -r /etc/nixos/configuration/
sudo ln -s /home/admin/repos/configuration/ /etc/nixos/
# Test by rebuilding system
sudo nixos-rebuild dry-run
- Have fun and happy hacking!