cd /news/cloud-computing/nixos-on-hetzner-cloud · home topics cloud-computing article
[ARTICLE · art-10194] src=gist.github.com ↗ pub= topic=cloud-computing verified=true sentiment=· neutral

NixOS on Hetzner Cloud

This article provides a step-by-step guide for setting up a NixOS server on a Hetzner Cloud instance, including partitioning the disk, installing NixOS, and configuring an admin user with SSH access. It also explains how to manage the system configuration via a Git repository by creating a symbolic link from `/etc/nixos/` to a user-controlled repo. The process concludes with instructions for rebuilding the system and verifying the setup.

read3 min views22 publishedFeb 6, 2022

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.

  1. Create a Hetzner Cloud instance and click to enter it.
  2. Stop the instance (top right corner icon).
  3. Go to ISO Images.
  4. Search for "nixos" and click mount. ISO Images tab of a Hetzner Cloud instance with the NixOS image already mounted
  5. Start the instance again (top right corner icon).
  6. Open the console (top right corner icon).
  7. 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
  1. 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
  1. Log on from your computer on via ssh.
ssh nixos@<IPv4 address>
  1. Partition, format and mountthe disk.
sudo -i
parted /dev/sda -- mklabel msdos
parted /dev/sda -- mkpart primary 1MiB 100%
partprobe
mkfs.ext4 -L nixos /dev/sda1
mount /dev/disk/by-label/nixos /mnt
  1. Generate the initial configuration and install.
nixos-generate-config --root /mnt
sed -e 's/^.*boot\.\.grub\.device.*$/  boot..grub.device = "\/dev\/sda";/g' -i /mnt/etc/nixos/configuration.nix
mkdir /mnt/etc/nixos/configuration/
sed -e 's/\.\/hardware-configuration\.nix/.\/hardware-configuration.nix\n      .\/configuration/g' -i /mnt/etc/nixos/configuration.nix
  1. Open /mnt/etc/nixos/configuration/default.nix with an editor like vim or nano and paste the following.
{ config, pkgs, ... }:

{
  imports =
    [
      ./user.nix
    ];
  environment.systemPackages = with pkgs; [
    vim
    wget
    git
    htop bottom
  ];
  services.openssh.enable = true;
}
  1. 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...." ];
      };
    };
  };
}
  1. Perform installation.
nixos-install --no-root-passwd
  1. Enter the mounted installation and set a password for the admin account.
nixos-enter --root /mnt
passwd admin
exit
  1. Shut down the machine.
poweroff
  1. Unmount the ISO in the Hetzner Cloud interface and start the machine again.
  2. Log on via SSH.
ssh-keygen -R <IPv4 address>
ssh admin@<IPv4 address>
  1. Turn custom config into repo of admin for convenience
mkdir repos
cp -R /etc/nixos/configuration/ repos/
cd repos/configuration/
git init
git config user.name admin
git config user.email admin@localhost
git add .
git commit -m "initial commit"
sudo rm -r /etc/nixos/configuration/
sudo ln -s /home/admin/repos/configuration/ /etc/nixos/
sudo nixos-rebuild dry-run
  1. Have fun and happy hacking!
── more in #cloud-computing 4 stories · sorted by recency
── more on @nixos 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/nixos-on-hetzner-clo…] indexed:0 read:3min 2022-02-06 ·