NixOS on ARM/Raspberry Pi 4: Difference between revisions

Nixoszz (talk | contribs)
Tags: Reverted Visual edit
m add udev rule for non-root access through libgpiod
 
(8 intermediate revisions by 4 users not shown)
Line 27: Line 27:
|}
|}
</div>
</div>
Raspberry Pi系列设备是由Raspberry Pi基金会制造的一系列单板计算机。它们都基于Broadcom片上系统(SoC)。
The Raspberry Pi family of devices is a series of single-board computers made by the Raspberry Pi Foundation. They are all based on Broadcom System-on-a-chip (SoCs).


== 状态 ==
== Status ==


Raspberry Pi 4家族仅支持AArch64。社区支持使用armv7。
The Raspberry Pi 4 Family is only supported as '''AArch64'''. Use as armv7 is community supported.


== Board-specific 安装说明 ==
== Board-specific installation notes ==


首先按照 [[NixOS_on_ARM#Installation|generic installation steps]] 获取安装程序映像,并使用 [[NixOS_on_ARM#NixOS_installation_.26_configuration|installation and configuration steps]].
First follow the [[NixOS_on_ARM#Installation|generic installation steps]] to get the installer image and install using the [[NixOS_on_ARM#NixOS_installation_.26_configuration|installation and configuration steps]].


Raspberry Pi 4B可与 [https://hydra.nixos.org/job/nixos/trunk-combined/nixos.sd_image.aarch64-linux generic SD image]配合使用.
The Raspberry Pi 4B works with the [https://hydra.nixos.org/job/nixos/trunk-combined/nixos.sd_image.aarch64-linux generic SD image].


nix.dev上提供了在Raspberry Pi上安装NixOS的示例说明。[https://nix.dev/tutorials/installing-nixos-on-a-raspberry-pi installing NixOS on a Raspberry Pi]  
Sample instructions for [https://nix.dev/tutorials/installing-nixos-on-a-raspberry-pi installing NixOS on a Raspberry Pi] are available at nix.dev.


{{warning|请注意,Raspberry Pi 4有两个HDMI输出,显然有时控制台/TTY的用户提示显示在HDMI 1上,而启动过程显示在HDMI 0上(官方(非NixOs)非图形精简版图像也可能出现这种情况)。因此,如果在第2阶段结束时收到“欢迎使用NixOs”的消息后,您的屏幕变黑/断开连接,请尝试使用另一个HDMI端口。请在此处查看相关错误。
{{warning| Note that the Raspberry Pi 4 has two HDMI outputs, and apparently sometimes the user prompt for the console/TTY is displayed on HDMI 1 while the boot process is displayed on HDMI 0 (this may even [https://raspberrypi.stackexchange.com/a/112071/149250 be the case] with the official (non NixOs) non-graphical lite image). So if after the message "Welcome on NixOs" at the end of phase 2 your screen goes black/disconnects, try to use the other HDMI port. See the related bug [https://github.com/NixOS/nixpkgs/issues/179701 here].}}
Note that the Raspberry Pi 4 has two HDMI outputs, and apparently sometimes the user prompt for the console/TTY is displayed on HDMI 1 while the boot process is displayed on HDMI 0 (this may even [https://raspberrypi.stackexchange.com/a/112071/149250 be the case] with the official (non NixOs) non-graphical lite image). So if after the message "Welcome on NixOs" at the end of phase 2 your screen goes black/disconnects, try to use the other HDMI port. See the related bug [https://github.com/NixOS/nixpkgs/issues/179701 here].}}


=== 配置 ===
=== Configuration ===


使用nixos-generate-config将生成所需的最小配置。
Using <code>nixos-generate-config</code> will generate the required minimal configuration.


Raspberry Pi 4在现代内核上得到了很好的支持。但是,如果您遇到GPU支持或其他deviceTree怪癖的问题,您可能希望添加nixos硬件通道: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:
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 78: Line 77:
   system.stateVersion = "24.11";
   system.stateVersion = "24.11";
}
}
</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>}}
</nowiki>}}


Line 91: Line 106:
Now reboot the device so it can update the firmware from the boot partition.
Now reboot the device so it can update the firmware from the boot partition.


=== GPU支持 ===
=== GPU support ===


以下配置示例是基于将其添加到已工作的配置中的假设而构建的。它们不是完整的配置。The following configuration samples are built on the assumption that they are added to an already working configuration. They are not complete configurations.
The following configuration samples are built on the assumption that they are added to an already working configuration. They are not complete configurations.


==== Without GPU ====
==== Without GPU ====
Line 140: Line 155:
   # Enable audio devices
   # Enable audio devices
   boot.kernelParams = [ "snd_bcm2835.enable_hdmi=1" "snd_bcm2835.enable_headphones=1" ];
   boot.kernelParams = [ "snd_bcm2835.enable_hdmi=1" "snd_bcm2835.enable_headphones=1" ];
  boot.loader.raspberryPi.firmwareConfig = ''
    dtparam=audio=on
  '';
}
}
</nowiki>}}
{{file|config.txt|txt|<nowiki>
dtparam=audio=on
</nowiki>}}
</nowiki>}}


Line 170: 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 180: 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 218: Line 235:
</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.