Install NixOS on Hetzner Cloud: Difference between revisions

L0b0 (talk | contribs)
m Undo invalid fix
A-h (talk | contribs)
Line 9: Line 9:
There are several ways to install NixOS, such as the "traditional" ISO installation, [[nixos-infect]] or [[nixos-anywhere]].
There are several ways to install NixOS, such as the "traditional" ISO installation, [[nixos-infect]] or [[nixos-anywhere]].


=== Tradition ISO installation ===
=== Traditional ISO installation ===
TODO
 
In the Hetzner console, mount the NixOS minimal ISO into your server, and use the console to install NixOS.
 
==== x86_64 ====
 
At time of writing, Hetnzer's x86_64 servers use legacy boot.
 
First, create a new MBR partition table.
 
  parted /dev/sda --script mklabel msdos
 
Then create a 512MB boot partition with ext4
 
  parted /dev/sda --script mkpart primary ext4 1MiB 513MiB
  parted /dev/sda --script set 1 boot on
  mkfs.ext4 -L boot /dev/sda1
 
Create a swap partition. This example uses 8GB, you may want to research the correct amount for your server size. Note the end of the swap partition in this command is 8577MiB, this is the value used in the next command.
 
  parted /dev/sda --script mkpart primary linux-swap 513MiB 8577MiB
  mkswap -L swap /dev/sda2
  swapon /dev/sda2
 
Create a root partition using the rest of the disk with ext4.
 
  parted /dev/sda --script mkpart primary ext4 8577MiB 100%
  mkfs.ext4 -L nixos /dev/sda3
 
If you don't mount the partitions you've just created, the NixOS installer will produce an error in the form `Failed to get blkid info (returned 512) for  on  tmpfs at <path>/<prefix>-install-grub.pl`.
 
  # Mount the partitions to /mnt and /mnt/boot.
  mount /dev/disk/by-label/nixos /mnt
  mkdir /mnt/boot
  mount /dev/disk/by-label/boot /mnt/boot
 
Finally, install. Install from a remote flake:
 
  sudo nixos-install --flake github:<username>/<repo>#<id>
 
Once installed, unmount the ISO and reboot.
 
===== Hetnzer base configuration =====
 
In the example below, the id would be `hetzner-x86_64`.
 
The `flake.nix` file in the repo should be of the form:
 
  {
    inputs = {
      nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
    };
 
    outputs = { nixpkgs, ... }: {
      nixosConfigurations = {
        hetzner-x86_64 = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [
            ./configuration.nix
          ];
        };
      };
    };
  }
 
With the referenced `configuration.nix` in the form.
 
Note the filesystems configuration, which matches the partition scheme, and the `availableKernelModules` section which includes modules that enable ext4 at boot.
 
Also note the user `username` which is configured to be part of the `wheel` group, and can therefore use `sudo`.
 
  { pkgs, ... }:
 
  {
    nix.settings = {
      experimental-features = "nix-command flakes";
      auto-optimise-store = true;
    };
   
    environment.systemPackages = [
      pkgs.vim
      pkgs.git
      pkgs.zip
      pkgs.unzip
      pkgs.wget
    ];
   
    fileSystems."/" = {
      device = "/dev/disk/by-label/nixos";
      fsType = "ext4";
    };
    fileSystems."/boot" = {
      device = "/dev/disk/by-label/boot";
      fsType = "ext4";
    };
    swapDevices = [
      {
        device = "/dev/disk/by-label/swap";
      }
    ];
   
    documentation.nixos.enable = false;
    time.timeZone = "Europe/London";
    i18n.defaultLocale = "en_GB.UTF-8";
    console.keyMap = "us";
    nix.settings.trusted-users = [ "@wheel" ];
   
    boot.loader.grub.enable = true;
    boot.loader.grub.device = "/dev/sda";
    boot.initrd.availableKernelModules = [ "ahci" "xhci_pci" "virtio_pci" "virtio_scsi" "sd_mod" "sr_mod" "ext4" ];
   
    users.users = {
      root.hashedPassword = "!"; # Disable root login
      username = {
        isNormalUser = true;
        extraGroups = [ "wheel" ];
        openssh.authorizedKeys.keys = [
          ''ssh-rsa <your_ssh_public_key>''
        ];
      };
    };
   
    security.sudo.wheelNeedsPassword = false;
   
    services.openssh = {
      enable = true;
      settings = {
        PermitRootLogin = "no";
        PasswordAuthentication = false;
        KbdInteractiveAuthentication = false;
      };
    };
    networking.firewall.allowedTCPPorts = [ 22 ];
   
    # This value determines the NixOS release from which the default
    # settings for stateful data, like file locations and database versions
    # on your system were taken. It‘s perfectly fine and recommended to leave
    # this value at the release version of the first install of this system.
    # Before changing this value read the documentation for this option
    # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
    system.stateVersion = "24.05"; # Did you read the comment?
  }
 
 
To access the system, you will need to ensure that port 22 on the VM is available via the Hetzner firewall.


=== nixos-anywhere ===
=== nixos-anywhere ===