NixOS on ARM/Raspberry Pi 4: Difference between revisions

Pulkits (talk | contribs)
m Added instructions for audio through hdmi and fixed a typo in spi section
Tags: Mobile edit Mobile web edit
m add udev rule for non-root access through libgpiod
 
(25 intermediate revisions by 9 users not shown)
Line 47: Line 47:
Using <code>nixos-generate-config</code> will generate the required minimal configuration.
Using <code>nixos-generate-config</code> will generate the required minimal configuration.


For better GPU support and some deviceTree quirks add the nixos-hardware channel:
Raspberry Pi 4 is well-supported on modern kernels. However, if you encounter issues with GPU support or other deviceTree quirks, you may wish to add the nixos-hardware channel:


<code>
<code>
Line 75: Line 75:
     raspberrypi-eeprom
     raspberrypi-eeprom
   ];
   ];
   system.stateVersion = "23.11";
   system.stateVersion = "24.11";
}
}
</nowiki>}}
</nowiki>}}
=== <code>config.txt</code> ===
{{warning|Since 24.11, the option <code>boot.loader.raspberrypi</code> which included <code>firmwareConfig</code> is removed from <code>nixpkgs</code>, therefore changes have to be written to <code>config.txt</code> directly<ref>https://github.com/NixOS/nixpkgs/pull/241534</ref>}}
To edit options only available through <code>config.txt</code>, as of May 12, 2025, you can only do so non-declaratively:
{{commands|<nowiki>
$ sudo mount /dev/disk/by-label/FIRMWARE /mnt
$ sudo vim /mnt/config.txt # <-- make changes here
</nowiki>}}
For example, [https://www.raspberrypi-spy.co.uk/2020/11/overclocking-the-raspberry-pi-400/ overclocking] the Raspberry Pi 400 can be done by adding the following:
{{file|config.txt|text|<nowiki>
arm_freq=2000
over_voltage=6
</nowiki>}}
=== USB boot ===
=== USB boot ===


Line 135: Line 152:


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
   sound.enable = true;
{
  hardware.pulseaudio.enable = true;
  # Enable audio devices
  hardware.raspberry-pi."4".audio.enable = true;
   boot.kernelParams = [ "snd_bcm2835.enable_hdmi=1" "snd_bcm2835.enable_headphones=1" ];
}
</nowiki>}}
{{file|config.txt|txt|<nowiki>
dtparam=audio=on
</nowiki>}}
</nowiki>}}


For audio through hdmi:
If you're running headless, you can also disable HDMI audio and force use of the headphones jack by adding <code>hdmi_ignore_edid_audio=1</code> on a line below <code>dtparam=audio=on</code>.
 
=== Networking ===
 
Ethernet and wifi interfaces should work out of the box. In addition to normal network configuration, consider disabling wifi powersaving if you experience slowness or issues with the host becoming unreachable on the network shortly after boot. For NetworkManager, the following configuration is sufficient:


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
   sound.enable = true;
{
   hardware.pulseaudio.enable = true;
   # Basic networking
   boot.kernelParams = [
   networking.networkmanager.enable = true;
"snd_bcm2835.enable_hdmi=1"];
   # Prevent host becoming unreachable on wifi after some time.
  networking.networkmanager.wifi.powersave = false;
}
</nowiki>}}
</nowiki>}}


Line 158: Line 185:
The <code>extraRules</code> changes the owner of <code>gpiomem</code> and all other files needed for GPIO to work to <code>root:gpio</code> and changes the permissions to <code>0660</code>.
The <code>extraRules</code> changes the owner of <code>gpiomem</code> and all other files needed for GPIO to work to <code>root:gpio</code> and changes the permissions to <code>0660</code>.
Therefore, the root user and anyone in the gpio group can now access the GPIO pins.
Therefore, the root user and anyone in the gpio group can now access the GPIO pins.
Permissions on the <code>/dev/gpiochip*</code> devices are needed to support access through the newer '''GPIO character device''' interface (see [https://libgpiod.readthedocs.io/en/latest/ libgpiod]).


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 168: Line 196:
     SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", RUN+="${pkgs.bash}/bin/bash -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
     SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", RUN+="${pkgs.bash}/bin/bash -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
     SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add",RUN+="${pkgs.bash}/bin/bash -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
     SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add",RUN+="${pkgs.bash}/bin/bash -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
    SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", RUN+="${pkgs.bash}/bin/bash -c 'chown root:gpio /dev/gpiochip* && chmod 0660 /dev/gpiochip*'"
   '';
   '';


Line 186: Line 215:
After applying the overlay, we add an <code>spi</code> group and change the owner of the <code>spidev</code> device to it, similarly to [[#Using GPIO pins as non root |GPIO]].
After applying the overlay, we add an <code>spi</code> group and change the owner of the <code>spidev</code> device to it, similarly to [[#Using GPIO pins as non root |GPIO]].


<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = true;
hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = true;
hardware.deviceTree = {
hardware.deviceTree = {
Line 194: Line 223:
     {
     {
       name = "spi";
       name = "spi";
       dtsoFile = ./spi0-0cd.dtso;
       dtboFile = ./spi0-0cs.dtbo;
     }
     }
   ];
   ];
Line 204: Line 233:
   SUBSYSTEM=="spidev", KERNEL=="spidev0.0", GROUP="spi", MODE="0660"
   SUBSYSTEM=="spidev", KERNEL=="spidev0.0", GROUP="spi", MODE="0660"
'';
'';
</syntaxHighlight>
</syntaxhighlight>


The the <code>spi0-0cd.dtso</code> file can be downloaded [https://github.com/raspberrypi/firmware/blob/master/boot/overlays/spi0-0cs.dtbo here].
The the <code>spi0-0cs.dtso</code> file can be downloaded [https://github.com/raspberrypi/firmware/blob/master/boot/overlays/spi0-0cs.dtbo here].
You might have to change the <code>compatible</code> field to "raspberrypi" in the dtbo file.
You might have to change the <code>compatible</code> field to "raspberrypi" in the dtbo file.


Line 263: Line 292:
}
}
</nowiki>}}
</nowiki>}}
=== Enabling Bluetooth ===
One might get bluetooth to work with this in the configuration file:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
  systemd.services.btattach = {
    before = [ "bluetooth.service" ];
    after = [ "dev-ttyAMA0.device" ];
    wantedBy = [ "multi-user.target" ];
    serviceConfig = {
      ExecStart = "${pkgs.bluez}/bin/btattach -B /dev/ttyAMA0 -P bcm -S 3000000";
    };
  };
</nowiki>}}
== Customizing & Generating SD image without installation step ==
There's a nix-community project to support fine-grained kernel & config.txt, and generate the image directly:
[https://github.com/nix-community/raspberry-pi-nix/ nix-community/raspberry-pi-nix]


== Notes about the boot process ==
== Notes about the boot process ==