> For AI agents: the complete documentation index is available at /en/llms.txt, the full documentation bundle is available at /en/llms-full.txt.

# NixOS

There is no NixOS template in the portal, but you can install NixOS yourself in a few
minutes. You order the server with Debian 13 and then run
[nixos-anywhere](https://github.com/nix-community/nixos-anywhere) from your own
computer. The tool replaces Debian with NixOS over SSH, and the server restarts into
NixOS with your configuration.

:::warning
Everything on the server's disk is deleted. Do this on a new server, or copy anything you
want to keep first.
:::

## What you need \{#det-har-behover-du}

- **A VPS with Debian 13 and at least 2 GB RAM**: VPS Medium, VPS Bygg själv or a
  server in VPS Cloud. The installation runs in the server's memory, and on VPS Small
  with 1 GB it stops with `Kexec failed`. Debian is then left untouched.
- **Debian 13 as the OS template.** The Ubuntu templates have Secure Boot turned on,
  and NixOS does not boot with it.
- **Nix on your computer**, running Linux, macOS or WSL2 on Windows. On Debian 13 Nix
  is available as a package:

  ```bash
  sudo apt install nix-setup-systemd
  sudo usermod -aG nix-users $USER
  ```

  Log out and in again afterwards. On other systems, install Nix following
  [nixos.org/download](https://nixos.org/download/).

Turn on flakes, which the installation uses:

```bash
mkdir -p ~/.config/nix
echo 'experimental-features = nix-command flakes' >> ~/.config/nix/nix.conf
```

## Step 1: Get the server's network settings \{#steg-1-hamta-natinstallningar}

The NixOS server gets static addresses, so you need to know what Debian was given. Log
in to the server with the username from the portal, see
[Getting started](/en/vps/kom-igang.md), and run:

```bash
ip -br link
ip -br addr
ip route
ip -6 route
```

Write down:

| What                          | Where                                                                       | Example                  |
| ----------------------------- | --------------------------------------------------------------------------- | ------------------------ |
| The MAC address of `eth0`     | `ip -br link`                                                               | `bc:24:11:20:04:7d`      |
| The IPv4 address with netmask | `ip -br addr`, the `eth0` line                                              | `193.180.173.29/24`      |
| The IPv6 address with netmask | `ip -br addr`, the `eth0` line, the address that does not start with `fe80` | `2a12:5bc5:0:500::29/64` |
| The IPv4 gateway              | `ip route`, the `default via` line                                          | `193.180.173.1`          |
| The IPv6 gateway              | `ip -6 route`, the `default via` line                                       | `2a12:5bc5:0:500::1`     |

If the server has a [private network](/en/vps/privat-natverk.md), also write down the MAC
address of `eth1`.

NixOS names the network cards `ens18` and `ens19` instead of `eth0` and `eth1`. The
configuration below picks the cards by MAC address, so the names do not matter.

## Step 2: Write the configuration \{#steg-2-skriv-konfigurationen}

Create a directory on your computer, for example `nixos-vps`, with three files.

**`flake.nix`** says which NixOS version you want:

```nix
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
    disko.url = "github:nix-community/disko";
    disko.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = { nixpkgs, disko, ... }: {
    nixosConfigurations.vps = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        disko.nixosModules.disko
        ./configuration.nix
      ];
    };
  };
}
```

**`disk-config.nix`** partitions the disk: an EFI partition and a root partition on the
rest.

```nix
{
  disko.devices.disk.main = {
    device = "/dev/vda";
    type = "disk";
    content = {
      type = "gpt";
      partitions = {
        ESP = {
          size = "512M";
          type = "EF00";
          content = {
            type = "filesystem";
            format = "vfat";
            mountpoint = "/boot";
            mountOptions = [ "umask=0077" ];
          };
        };
        root = {
          size = "100%";
          content = {
            type = "filesystem";
            format = "ext4";
            mountpoint = "/";
          };
        };
      };
    };
  };
}
```

**`configuration.nix`** is the server itself. Replace the hostname, MAC address,
addresses, gateways, username and SSH key with your own:

```nix
{ modulesPath, ... }:
{
  imports = [
    (modulesPath + "/profiles/qemu-guest.nix")
    ./disk-config.nix
  ];

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  # The console in the portal
  boot.kernelParams = [ "console=tty0" "console=ttyS0,115200" ];

  # The root grows when you make the disk bigger
  boot.growPartition = true;
  fileSystems."/".autoResize = true;

  # The guest agent, so that backups are consistent
  services.qemuGuest.enable = true;

  time.timeZone = "Europe/Stockholm";

  networking.hostName = "server1";
  networking.useNetworkd = true;
  networking.useDHCP = false;
  networking.nameservers = [ "94.142.251.250" "2a12:5bc1::250" ];

  systemd.network.networks."10-publik" = {
    matchConfig.MACAddress = "bc:24:11:20:04:7d";
    address = [ "193.180.173.29/24" "2a12:5bc5:0:500::29/64" ];
    routes = [
      { Gateway = "193.180.173.1"; }
      { Gateway = "2a12:5bc5:0:500::1"; }
    ];
  };

  services.openssh = {
    enable = true;
    settings.PasswordAuthentication = false;
    settings.KbdInteractiveAuthentication = false;
    settings.PermitRootLogin = "no";
  };

  users.users.itrhndbk = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    openssh.authorizedKeys.keys = [
      "ssh-ed25519 AAAA... du@dator"
    ];
  };
  security.sudo.wheelNeedsPassword = false;

  nix.settings.experimental-features = [ "nix-command" "flakes" ];

  system.stateVersion = "26.05";
}
```

The firewall is on from the start and only lets SSH in. Open more ports with, for
example, `networking.firewall.allowedTCPPorts = [ 80 443 ];`.

:::tip Private network
If the server has a private network, add a block for the second card with the MAC
address of `eth1` and the private address you have chosen. No gateway:

```nix
  systemd.network.networks."20-privat" = {
    matchConfig.MACAddress = "bc:24:11:b7:f9:03";
    address = [ "10.10.0.11/24" ];
  };
```

:::

## Step 3: Install \{#steg-3-installera}

Lock the versions and start the installation, with the username and address from the
portal:

```bash
cd nixos-vps
nix flake lock
nix run github:nix-community/nixos-anywhere -- --flake .#vps \
  --target-host itrhndbk@193.180.173.29
```

nixos-anywhere starts a NixOS installer in the server's memory, partitions the disk,
installs and restarts the server. It takes about five minutes. When it says
`### Done! ###` the server is running NixOS.

`flake.lock` records exactly which versions were installed. Keep it together with the
other files, preferably in git.

## Step 4: Log in \{#steg-4-logga-in}

The server has new host keys. Remove the old entry and log in:

```bash
ssh-keygen -R 193.180.173.29
ssh itrhndbk@193.180.173.29
```

Set a password on your account, so that you can log in via
[Console](/en/vps/hantera-servern.md#konsol) in the portal if the network should act up:

```bash
sudo passwd itrhndbk
```

## Change and update the server \{#andra-och-uppdatera}

The simplest way is to keep the configuration on the server. Copy the directory there
once:

```bash
scp -r nixos-vps itrhndbk@193.180.173.29:
```

When you have changed something in the files, build and switch to the new configuration
on the server:

```bash
sudo nixos-rebuild switch --flake ~/nixos-vps#vps
```

You get security updates by updating the lock file and rebuilding:

```bash
cd ~/nixos-vps
nix flake update
sudo nixos-rebuild switch --flake ~/nixos-vps#vps
```

NixOS releases a new version in May and November, and each version gets security updates
for about seven months. Move to a new version by changing `nixos-26.05` in `flake.nix`
and running the commands above. Do not change `system.stateVersion`; it should stay at
the version you installed.

:::info Is the directory in git?
Then Nix only sees files that are added to git. Run `git add` on new files before you
build.
:::

## What works differently \{#annorlunda}

The NixOS server does not have cloud-init, which the portal uses to set up the other
operating systems. Therefore:

- **SSH keys** are changed in `configuration.nix`, not with **Reset SSH Keys** in the
  portal.
- **The console password** is the one you set with `passwd`, not the one shown in the
  portal.
- **[Extra IPv4 addresses](/en/vps/extra-ipv4.md)** are added to the `address` list in
  `10-publik`, with the netmask `/24` and no new gateway.
- **Rebuild** in the portal installs the OS template you choose. If you want NixOS
  again, run step 3 once more.

This works as usual:

- **The console** in the portal shows the boot menu and the login prompt.
- **Bigger disk:** the root grows by itself at the next restart.
- **Backups and snapshots:** the guest agent freezes the file system while they are
  taken, see [Backups and snapshots](/en/vps/hantera-servern.md#sakerhetskopior).
