---
title: 'NixOS Install'
description: 'Setup fresh NixOS install on an internal or test server.'
sidebar:
  icon: server-cog
---

:::warning[Firewall]
BEFORE starting this install tutorial the server needs to have configured firewall entries to reach the internet (downloading NixOS assets).
:::

This guide is basically the same as the official [NixOS Install docs](https://nixos.org/manual/nixos/stable/#ch-installation) with small adjustments or notes.

## Server requirements

To register a new server, place a request with these details:

- Type: `VM`
- OS: `NixOS -> Minimal ISO https://nixos.org/download/`
- VM: min 2 CPU; 4GB RAM; 64GB SSD
- 24x7 high reliability/ monitoring
- placement: one of the internal DC zones in the internal network (not DMZ!)
- backup: yes, daily

## Workflow

Since we cannot register everything at once, the workflow for server setup is currently roughly as follows:

- Server registration (one at a time)
  - this provides you with a server number
- Firewall for outbound HTTPS
  - rules from FQDN `s....swr.ard` to HTTPS 443
- NixOS install
  - afterwards this server no longer announces itself as `nixos`
- repeat server registration steps
- Firewall for NATS with all servers — copy-paste values in [Firewall](/ard-eventhub/development/firewall)

## Step-by-step

Log into the server via SSH. `ssh s000...` might not work since the new NixOS install announces itself as hostname `nixos`.
For this to work your SSH key also needs to be placed in `/home/nixos/.ssh/authorized_keys` or `/root/.ssh/authorized_keys`.

```bash
ssh root@nixos
```

Identify the target disk (e.g. `sda`).

```bash
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
```

Confirm the installer booted in UEFI mode.

```bash
test -d /sys/firmware/efi && echo UEFI || echo BIOS
```

Partition and format the virtual disk.

```bash
parted /dev/sda -- mklabel gpt
parted /dev/sda -- mkpart root ext4 512MB -8GB
parted /dev/sda -- mkpart swap linux-swap -8GB 100%
parted /dev/sda -- mkpart ESP fat32 1MB 512MB
parted /dev/sda -- set 3 esp on
```

```bash
mkfs.ext4 -L nixos /dev/sda1
mkswap -L swap /dev/sda2
mkfs.fat -F 32 -n boot /dev/sda3
```

Mount partition.

```bash
mount /dev/disk/by-label/nixos /mnt
mkdir -p /mnt/boot
mount -o umask=077 /dev/disk/by-label/boot /mnt/boot
```

Generate the NixOS config.

```bash
nixos-generate-config --root /mnt
```

Check IP config to set static IP.

```bash
ip -br addr; ip route; nmcli -t -f NAME,DEVICE,TYPE con show'
```

The first block in the second line print something like `ens33` or `eth0`. This is the network interface name. The line also shows the IP address of the interface (e.g. `10.xxx.xxx.xxx/24`).

Somewhere it says `default via`, which is the default gateway.

Update the config: insert `hostName`, SSH keys and update networking config from previous step.

```bash
nano /mnt/etc/nixos/configuration.nix
```

```nix
# ...
networking.hostName = "s000...";
users.users.root.openssh.authorizedKeys.keys = [
	# ...
];
nix.settings.experimental-features = [ "nix-command" "flakes" ];
programs.zsh.enable = true;
services.openssh = {
	enable = true;
	settings.PermitRootLogin = "yes";
	settings.PasswordAuthentication = false;
};
networking.nameservers = [
  "10.127.201.221"
  "10.63.101.221"
  "10.191.201.221"
];
networking.networkmanager.unmanaged = [ "ens33" ];
networking.interfaces.ens33.ipv4.addresses = [
  {
    address = "10.xxx.xxx.xxx";
    prefixLength = 24;
  }
];
networking.defaultGateway = {
  address = "10.xxx.xxx.1";
  interface = "ens33";
};
```

Run installation.

```bash
nixos-install
```

```bash
reboot
```

## Post Install

Download the remote config with the init command (copies the whole folder)

```bash
just nix-download-init eventhub-connect-xxx
```

Merge the downloaded `hardware-configuration.nix` into the correct host folder. update `config.nix` if needed (static IP addresses).

Then upload the config to the server.

```bash
just nix-upload eventhub-connect-xxx
```

SSH into the server with the new hostname.

```bash
ssh s000...
cd /etc/nixos
nixos-rebuild switch --flake /etc/nixos#eventhub-connect-xxx
```

## Known Problems

- `Could not resolve hostname (6) Could not resolve host: cache.nixos.org;` --> Fix resolv.conf
  - otherwise: `ip route replace default via 10.xxx.xxx.1 dev ens33 src 10.xxx.xxx.xxx`
- ssh `Permission denied` --> Fix `authorized_keys`
