Running NixOS on Proxmox

Compatibility: These instructions work as of Proxmox 8.x and NixOS 24.05.

One of the stumbling blocks I ran into when trying out NixOS was that I couldn’t run it under Proxmox, my preferred virtual machine server.

Through some trial and error, I figured out how to install NixOS as a Proxmox container.

Download the NixOS container image ๐Ÿ”—︎

First, download the latest NixOS x86_x64 lxdContainerImage image. For other hardware architectures, see this GitHub comment.

Warning: Hydra also features a build called proxmoxLXC.x86_64-linux. I expected it to work even better on Proxmox, but it seems to be broken. It boots NixOS, but the login does not accept any standard NixOS credential (nixos, root).

At the time of this writing, the latest NixOS container build is 275970650, but you can just click whatever is the latest build as you read this.

Screenshot of latest builds page, showing a NixOS container image build each day.

From the build result page, click the link labeled nixos-system-x86_64-linux.tar.xz to download the image:

Screenshot of metadata page for a NixOS build from 2023-09-21

Upload the image to Proxmox ๐Ÿ”—︎

Now, it’s time to upload the image to Proxmox. Scroll down to one of your Proxmox storage nodes.

Click the storage node you’d like to use. The default is called local, but you may have others.

Screenshot of local storage node menu item in the Server View of Proxmox

From the storage node, click “CT Templates,” and then click “Download from URL.”

Screenshot of settings pages for storage node, showing the CT Templates tab is selected and an arrow pointing to the Upload button

The NixOS container image download doesn’t include any version or date information. For organization, I renamed my image file to:

  • nixos-2024-10-21-lxdContainerImage.x86_64-linux.tar.xz

Renaming will help you identify which version of NixOS this is when you see it later in Proxmox, though this step is optional.

Screenshot of download settings showing the build URL at the top and the custom filename in the middle field

Click “Download” to download the template to Proxmox.

Create a NixOS container ๐Ÿ”—︎

Warning: Creating a container through the Proxmox web UI does not work with this template. You need to perform this step through the Proxmox terminal.

For the next step, SSH to your Proxmox system and switch to the root user context:

ssh root@pve

From the Proxmox SSH session, select the settings for the new NixOS container:

# Where the template file is located
TEMPLATE_STORAGE='local'
# Name of the template file downloaded from Hydra.
TEMPLATE_FILE='nixos-2024-10-21-lxdContainerImage.x86_64-linux.tar.xz'
# Name to assign to new NixOS container.
CONTAINER_HOSTNAME='nixos'
# Which storage location to place the new NixOS container.
CONTAINER_STORAGE='local'
# How much RAM to assign the new container.
CONTAINER_RAM_IN_MB='8192'
# How much disk space to assign the new container.
CONTAINER_DISK_SIZE_IN_GB='80'

With those settings in place, create the new NixOS container with pct create:

pct create "$(pvesh get /cluster/nextid)" \
  --arch amd64 \
  "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE_FILE}" \
  --ostype unmanaged \
  --description nixos \
  --hostname "${CONTAINER_HOSTNAME}" \
  --net0 name=eth0,bridge=vmbr0,ip=dhcp,firewall=1 \
  --storage "${CONTAINER_STORAGE}" \
  --memory "${CONTAINER_RAM_IN_MB}" \
  --rootfs ${CONTAINER_STORAGE}:${CONTAINER_DISK_SIZE_IN_GB} \
  --unprivileged 1 \
  --features nesting=1 \
  --cmode console \
  --onboot 1 \
  --start 1

Log in to NixOS ๐Ÿ”—︎

Your NixOS container is now configured and should be running.

Confusingly, if you visit the Console tab for your new container, you’ll see only a black screen:

Screenshot of a black screen in the Proxmox container on the Console tab

If you hit “Enter,” you should see the standard NixOS prompt. You can log in with username root and no password.

Screenshot of nixos default login prompt in a Proxmox container on the Console tab

Get SSH access to NixOS ๐Ÿ”—︎

Strangely, the configuration.nix file that ships with the NixOS container image does not work. If you try to run nixos-rebuild, you’ll see errors about a missing lxd.nix file. Even if you fix those, there are other systemd errors.

Before you fix those, it’s easier if you have SSH access. The easiest way I’ve found to do that is to download your SSH keys to the system. If you have a GitHub account with SSH keys enabled, you can do that as follows:

GITHUB_USERNAME='your-github-username' # Replace this.

mkdir -p ~/.ssh && \
  curl "https://github.com/${GITHUB_USERNAME}.keys" > ~/.ssh/authorized_keys

After this, you should be able to SSH in to your NixOS system with ssh root@nixos.

Configuring NixOS ๐Ÿ”—︎

I’ve created a basic configuration for a NixOS server system as a Proxmox container. You can download this configuration by running the following command:

curl \
  --show-error \
  --fail \
  https://mtlynch.io/notes/nixos-proxmox/configuration.nix \
  > /etc/nixos/configuration.nix

Apply the new configuration by running the following commands:

nix-channel --update && \
  nixos-rebuild switch --upgrade && \
  echo "install complete, rebooting..." && \
  poweroff --reboot

From there, you have a basic NixOS server system, which you can configure however you like.

References ๐Ÿ”—︎

Read My Book

I'm writing a book of simple techniques to help developers improve their writing.

My book will teach you how to:

Read Michael's Book