about summary refs log tree commit diff stats
path: root/content/post/nixos-on-nanopi-r5s.md
diff options
context:
space:
mode:
authorAlan Pearce2023-07-30 10:38:40 +0200
committerAlan Pearce2023-07-30 10:38:40 +0200
commit737630933b4cc5a0f6c18d8b51f9e42a4e8db45a (patch)
tree4b28e2c7b31b1d8552e5b5e33abea61eb2a8e95d /content/post/nixos-on-nanopi-r5s.md
parent85426feafa3b77aa84159c45fa606aa075b10b64 (diff)
downloadwebsite-737630933b4cc5a0f6c18d8b51f9e42a4e8db45a.tar.lz
website-737630933b4cc5a0f6c18d8b51f9e42a4e8db45a.tar.zst
website-737630933b4cc5a0f6c18d8b51f9e42a4e8db45a.zip
Add new post about getting NixOS running on NanoPi R5S
Diffstat (limited to 'content/post/nixos-on-nanopi-r5s.md')
-rw-r--r--content/post/nixos-on-nanopi-r5s.md139
1 files changed, 139 insertions, 0 deletions
diff --git a/content/post/nixos-on-nanopi-r5s.md b/content/post/nixos-on-nanopi-r5s.md
new file mode 100644
index 0000000..f6e3e75
--- /dev/null
+++ b/content/post/nixos-on-nanopi-r5s.md
@@ -0,0 +1,139 @@
++++
+title = "Running NixOS on a NanoPi R5S"
+date = 2023-07-30T08:51:46Z
+[taxonomies]
+tags = ["NixOS", "home-networking", "infrastructure"]
++++
+
+I managed to get [NixOS](https://nixos.org) running on my [NanoPi R5S](https://www.friendlyelec.com/index.php?route=product/product&product_id=287) ([FriendlyElec Wiki](https://wiki.friendlyelec.com/wiki/index.php/NanoPi_R5S)).
+
+Firstly, I flashed a pre-built stock Debian image from [inindev](https://github.com/inindev/nanopi-r5) to an SD card. This can be used as a rescue system later on.
+
+From that SD card, I then flashed the same system onto the internal <abbr title="embedded MultiMediaCard">eMMC</abbr> Storage. I only really needed to this to ensure UBoot was correctly installed; I think there will be an easier way to do it.
+
+I had nix already installed on the <abbr title="Non-Volatile Memory Express">NVMe</abbr> <abbr title="Solid-State Drive">SSD</abbr> along with a home directory. I bind-mounted `/nix` and `/home` following the fstab I had previously set up:
+
+```conf
+UUID=replaceme	/mnt	ext4	relatime,lazytime	0 2
+/mnt/nix		/nix	none		defaults,bind		0 0
+/mnt/srv		/srv	none		defaults,bind		0 0
+/mnt/home		/home	none 		defaults,bind 		0 0
+```
+
+I then created a user for myself using that home directory, I had full access to nix in the new Debian environment. This meant I had access to `nixos-install`. 
+
+I wanted to use the [extlinux support in UBoot](https://u-boot.readthedocs.io/en/latest/develop/distro.html#boot-configuration-files), so I made `/mnt/boot` point to `/boot` on the <abbr>eMMC</abbr>:
+
+```sh
+mkdir /mnt/{emmc,boot}
+mount LABEL=rootfs /mnt/emmc
+mount --bind /mnt/emmc /mnt/boot
+```
+
+<aside>
+<p>One could <em>probably</em> delete everything else on the <abbr>eMMC</abbr> and move the contents of <code>/mnt/emmc/boot</code> to <code>/mnt/emmc</code>, thus obviating the need to bind-mount <code>/boot</code>
+</aside>
+
+I ran `nixos-generate-config` as usual, which set up the mount points in `hardware-configuration.nix` correctly. `configuration.nix` needed a bit of tweaking. My first booting configuration was something like this, mostly borrowed from [Artem Boldariev's comment](https://github.com/inindev/nanopi-r5/issues/11#issue-1789308883):
+
+```nix
+{ config
+, pkgs
+, lib
+, ...
+}:
+let
+  fsTypes = [ "f2fs" "ext" "exfat" "vfat" ];
+in
+{
+  imports = [ ./hardware-configuration.nix ];
+  boot = {
+    kernelParams = [ "console=tty0" ];
+    # partial Rockchip related changes from Debian 12 kernel version 6.1
+    # Also, see here:
+    # https://discourse.nixos.org/t/how-to-provide-missing-headers-to-a-kernel-build/11422/3
+    kernelPatches = [
+      {
+        name = "rockchip-config.patch";
+        patch = null;
+        extraConfig = ''
+          PHY_ROCKCHIP_PCIE Y
+          PCIE_ROCKCHIP_EP y
+          PCIE_ROCKCHIP_DW_HOST y
+          ROCKCHIP_VOP2 y
+        '';
+      }
+      {
+        name = "status-leds.patch";
+        patch = null;
+        # old:
+        # LEDS_TRIGGER_NETDEV y
+        extraConfig = ''
+          LED_TRIGGER_PHY y
+          USB_LED_TRIG y
+          LEDS_BRIGHTNESS_HW_CHANGED y
+          LEDS_TRIGGER_MTD y
+        '';
+      }
+    ];
+    
+    supportedFilesystems = fsTypes;
+    initrd.supportedFilesystems = fsTypes;
+
+    initrd.availableKernelModules = [
+      ## Rockchip
+      ## Storage
+      "sdhci_of_dwcmshc"
+      "dw_mmc_rockchip"
+
+      "analogix_dp"
+      "io-domain"
+      "rockchip_saradc"
+      "rockchip_thermal"
+      "rockchipdrm"
+      "rockchip-rga"
+      "pcie_rockchip_host"
+      "phy-rockchip-pcie"
+      "phy_rockchip_snps_pcie3"
+      "phy_rockchip_naneng_combphy"
+      "phy_rockchip_inno_usb2"
+      "dwmac_rk"
+      "dw_wdt"
+      "dw_hdmi"
+      "dw_hdmi_cec"
+      "dw_hdmi_i2s_audio"
+      "dw_mipi_dsi"
+    ];
+    loader = {
+      timeout = 3;
+      grub.enable = false;
+      generic-extlinux-compatible = {
+        enable = true;
+        useGenerationDeviceTree = true;
+      };
+    };
+  };
+  # Most Rockchip CPUs (especially with hybrid cores) work best with "schedutil"
+  powerManagement.cpuFreqGovernor = "schedutil";
+  
+  boot.kernelParams = [
+    "console=tty1"
+    "console=ttyS2,1500000"
+    "earlycon=uart8250,mmio32,0xfe660000"
+  ];
+  # Let's blacklist the Rockchips RTC module so that the
+  # battery-powered HYM8563 (rtc_hym8563 kernel module) will be used
+  # by default
+  boot.blacklistedKernelModules = [ "rtc_rk808" ];
+
+  # ... typical config omitted for brevity
+}
+```
+
+Due to the custom kernel configuration, building takes a while. I set up a [distributed build](https://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html) to speed things up, using a [Hetzner Cloud](https://www.hetzner.com/cloud) CAX21 ARM64 instance (although I could have used an x86_64 system with one of the methods mentioned on the [NixOS on ARM NixOS wiki page](https://nixos.wiki/wiki/NixOS_on_ARM#Build_your_own_image_natively)). This made for a very long `nixos-install` command line:
+
+```sh
+sudo env PATH=$PATH =nixos-install --root /mnt/internal --no-channel-copy --channel https://nixos.org/channels/nixos-23.05 --option builders'ssh://my-host aarch64-linux /root/.ssh/id_pappel_nixpkgs 4 2 big-parallel' --option builders-use-substitutes true --max-jobs 0
+```
+
+I added `setenv bootmeths "extlinux"` to `/boot/boot.txt` and ran `/boot/mkscr.sh` as root to ensure that UBoot would search for the `extlinux.conf` file
\ No newline at end of file