Given a raspberry pi 4 with
an ethernet connection
a micro-sd card
… 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
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
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.
Download the image
(for me it was
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
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.
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:
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.
mv the minimal config to the pi:
scp configuration.nix email@example.com: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
kill remote session (C-d) and log in again, but with your username and password set in the config file.
We should now be able to log into the pi, change the config and rebuild the OS.