October 30, 2020

Install NIXOS on raspberry pi 4

Given a raspberry pi 4 with

  • an ethernet connection

  • a micro-sd card

  • a usb-keyboard

… this is how to set up the RP with nixos so that you can ssh into it. We won't need to attach a screen to it. All we need beyond above is a computer with an internet connection that can write to the SD card (using dd).

This is not a tutorial, this is a howto (and cheat sheet for my future self), so I won't explain too much here. Also the commands might still use some paths from my computer (like /home/johannes/).

All of this is necessary as of the time of writing the RP4 is not officially supported by NixOS. But I really fell in love with the declarative configuration of NixOS, so I had to install it anyway. Fortunatelly there is some documentation from people who did it before me, but I didn't find a complete howto.

Install NixOS

Download the image

(for me it was nixos-sd-image-21.03pre249162.1dc37370c48-aarch64-linux.img) from here: https://hydra.nixos.org/job/nixos/release-20.03/nixos.sd_image.aarch64-linux

Make sure you check the hashes.

Prepare the SD card

Put SD card into computer and write image on SD card. CARFULL HERE, better check (with lsblk) the of is really the SD card.

sudo dd if=~/Downloads/nixos-sd-image-aarch64-linux.img of=/dev/mmcblk0 status=progress

Boot into NixOS

Plug SD-card out and into the raspberry pi. Connect it to power - it should boot into NixOS now.

Set password for ssh login

We have to set password (mypassword) for default user nixos to log in via ssh, so plug in a usb keyboard and type:

passwd
ENTER
mypassword
ENTER
mypassword
ENTER

This is much easier with a display attached. But if you don't have an adapter for micro-hdmi (or a display), just do it blindly.

We won't need the keyboard from now on.

Log in via ssh

In my setup the address of the pi is nixos.fritz.box. So we should be able to ssh in with the password set above.

ssh nixos@nixos.fritz.box

I will call this session the "remote" from here on.

Build NixOS from a config

Create the nixos config

First create a config, especially the hardware-configuration.nix. On remote:

sudo nixos-generate-config

I wrote a minimal config to save some time. It is a slimmed down version of the config that is generated with nixos-generate-config. I added the boot settings from the NixOS Wiki and removed everything that I understood well enough to be sure it is not necessary for working on nixos via ssh.

Now mv the minimal config to the pi:

scp configuration.nix nixos@nixos.fritz.box:configration.nix

On the pi, move it to the right place (replacing the generated one).

sudo mv /home/nixos/configration.nix /etc/nixos/configuration.nix

Rebuild the OS from the config.

sudo nixos-rebuild switch

Profit

kill remote session (C-d) and log in again, but with your username and password set in the config file.

ssh johannes@nixos.fritz.box

Result

We should now be able to log into the pi, change the config and rebuild the OS.