NixOS bring-up on an Alibaba RISC-V board

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

NixOS bring-up on an Alibaba RISC-V board

NixOS 在阿里巴巴 RISC-V 开发板上的启动尝试 2026-11-20 在阿里巴巴上搜寻廉价 Versal 开发套件的过程中,我意外地获得了一块 Zhihe Computing A210 RISC-V SoC 开发板。这块板子很酷:底板暴露出的 IO 数量多得离谱,更不用说 8 个主频为 1.9 GHz 的核心对于 RISC-V CPU 来说也不容小觑。我不确定目前是否推荐购买任何 RISC-V SoC,但所有开始支持 RISC-V 的发行版都需要在 Spike/QEMU 之外的地方运行测试。我决定将这块板子变成一个 NixOS 构建服务器,以便深入了解 NixOS 中 RISC-V 支持的状态,并练习 Linux 的启动流程。我完整的 A210 NixOS 配置可以在这里找到;我确认了声音、HDMI、蓝牙/Wi-Fi、以太网和 GPU 都能正常工作,但具体情况可能因人而异。顺便说一句,我用的是 NixOS。

Nix makes cross compiling to RISC-V a breeze: Nix pkgs = import { system = “x86_64-linux”; crossSystem.system = “riscv64-linux”; }; It really is that easy—building pkgs.hello will produce a bona fide RISC-V executable (you don’t even need a Nix file for one-off builds, just run nix build nixpkgs#pkgsCross.riscv64.hello). Some black magic in callPackage ensures that compile-time tools are built for buildPlatform and build artifacts are built for hostPlatform.

Nix 让交叉编译到 RISC-V 变得轻而易举:Nix pkgs = import { system = “x86_64-linux”; crossSystem.system = “riscv64-linux”; }; 确实就是这么简单——构建 pkgs.hello 将生成一个真正的 RISC-V 可执行文件(一次性构建甚至不需要 Nix 文件,只需运行 nix build nixpkgs#pkgsCross.riscv64.hello)。callPackage 中的一些“黑魔法”确保了编译时工具是为 buildPlatform 构建的,而构建产物是为 hostPlatform 构建的。

In the process of compiling over a thousand packages I only ran into two issues: a problem with rust-bindgen using the wrong version of clang and ministream not being properly configured for cross compilation, neither of which was specific to RISC-V. That having been said, I did run into some pretty bizarre OOM issues for some of the heavy hitters like Clang and GFortran. I was seeing >9GB memory usage per linking process(!) when compiling from x86_64-linux, which will quickly exhaust all of your memory if you have max-jobs/cores set to sane values. I sidestepped this issue by temporarily nerfing build parallelism into oblivion and running the offending builds overnight: Bash nix-store —realise /nix/store/…-clang-riscv64-unknown-linux-gnu-21.1.8.drv -j1 —cores 3 —builders ""

在编译一千多个软件包的过程中,我只遇到了两个问题:rust-bindgen 使用了错误的 clang 版本,以及 ministream 没有正确配置交叉编译,这两者都不是 RISC-V 特有的问题。话虽如此,我在编译 Clang 和 GFortran 等重量级软件时确实遇到了一些非常奇怪的内存溢出(OOM)问题。当从 x86_64-linux 进行编译时,我观察到每个链接进程的内存使用量超过 9GB(!),如果将 max-jobs/cores 设置为合理的值,这会迅速耗尽所有内存。我通过暂时将构建并行度降至极低并让这些有问题的构建在夜间运行来规避了这个问题:Bash nix-store —realise /nix/store/…-clang-riscv64-unknown-linux-gnu-21.1.8.drv -j1 —cores 3 —builders ""

I don’t (yet) have access to a RISC-V remote Nix builder, but even if I did, it’s substantially faster to cross compile everything from my desktop. Cross compilation can get a little wonky with Nix flakes because you have to encode the buildPlatform in the output schema somehow, but nixosConfigurations does not allow nesting (nixosConfigurations.x86_64-linux-a210 is valid, but nixosConfigurations.x86_64-linux.a210 isn’t). My solution to this is to expose the relevant build artifacts of the NixOS configuration (ISO + flashing script) under packages.${system} by creating a “wrapper” NixOS configuration for each buildPlatform that extends nixosConfigurations.a210: Nix packages = forAllSystems (pkgs: let nixosCross = self.nixosConfigurations.a210.extendModules { modules = [ ({ … }: { nixpkgs = { inherit pkgs; overlays = [ # … ]; buildPlatform = { inherit (pkgs.stdenv.buildPlatform) system; }; hostPlatform = “riscv64-linux”; }; }) ]; }; in { inherit (nixosCross.config.system.build) toplevel image initialRamdisk; }; This way nixosConfigurations is agnostic to buildPlatform, and cross compilation is straightforward—running nix build .#image on an x86_64-linux machine will use x86_64-linux as the buildPlatform, as one would expect.

我(目前)还没有远程 RISC-V Nix 构建器,但即使有,从我的台式机交叉编译所有内容也要快得多。使用 Nix flakes 进行交叉编译可能会有点麻烦,因为你必须以某种方式在输出模式中编码 buildPlatform,但 nixosConfigurations 不允许嵌套(nixosConfigurations.x86_64-linux-a210 是有效的,但 nixosConfigurations.x86_64-linux.a210 不是)。我的解决方案是通过为每个 buildPlatform 创建一个扩展 nixosConfigurations.a210 的“包装器”NixOS 配置,将 NixOS 配置的相关构建产物(ISO + 刷机脚本)暴露在 packages.${system} 下:Nix packages = forAllSystems (pkgs: let nixosCross = self.nixosConfigurations.a210.extendModules { modules = [ ({ … }: { nixpkgs = { inherit pkgs; overlays = [ # … ]; buildPlatform = { inherit (pkgs.stdenv.buildPlatform) system; }; hostPlatform = “riscv64-linux”; }; }) ]; }; in { inherit (nixosCross.config.system.build) toplevel image initialRamdisk; }; 这样,nixosConfigurations 就与 buildPlatform 无关了,交叉编译也变得简单直接——在 x86_64-linux 机器上运行 nix build .#image 将按预期使用 x86_64-linux 作为 buildPlatform。

Boot flow Ok, so the goal is to boot U-Boot FSBL -> OpenSBI -> U-Boot SSBL -> Linux -> NixOS userspace, using the vendor’s Buildroot scripts as reference. Using vendor forks of U-Boot/Linux/OpenSBI is a simple matter of overriding src and sprinkling in a few patches (almost always to fix compiler warnings that have been promoted to errors in the latest version of GCC/Clang in nixpkgs). All we really have to do is slot in the vendor kernel by setting boot.kernelPackages = pkgs.linuxPackagesFor pkgs.linuxA210;

启动流程:好的,目标是启动 U-Boot FSBL -> OpenSBI -> U-Boot SSBL -> Linux -> NixOS 用户空间,并参考供应商的 Buildroot 脚本。使用供应商的 U-Boot/Linux/OpenSBI 分支只是覆盖 src 并添加一些补丁的简单问题(几乎总是为了修复在 nixpkgs 中最新版本的 GCC/Clang 中被提升为错误的编译器警告)。我们所要做的就是通过设置 boot.kernelPackages = pkgs.linuxPackagesFor pkgs.linuxA210 来插入供应商内核。

Armed with a bootloader and NixOS image, I plugged in the A210 to my MacBook and turned on Fastboot flashing mode, but to my dismay macOS refused to enumerate the device. Initially I figured the board was a lemon (due to it being a freebie and all), but I wasn’t ready to give up hope that easily. I pored over the datasheet looking for alternative boot methods, and I investigated using PD resistors to force QSPI boot (using NORbert in lieu of a real QSPI device) to bootstrap into Linux userspace, at which point I could flash the eMMC directly without relying on Fastboot. But before pulling out my soldering iron I decided to try plugging the board into a different computer, which instantly detected the board. D’oh! Ok, so Fastboot works and I’m able to load the flasher firmware and program eMMC (I based my Fastboot payload on openEuler’s A210 guide instead of the vendor’s script which flashes 7(!) different partitions), but after that I still got dead silence on UART. At this point I was pretty suspicious of my serial connection, so I decided to test it on a Pi Pico: lo and behold, it turns out leaving my BMP’s vRef pin floating was the culprit. After losing several hours and my dignity to the aforementioned skill issue, I was greeted with the following panic: sbi_trap_error: hart0: trap1: illegal instruction handler failed (error -2) …

准备好引导加载程序和 NixOS 镜像后,我将 A210 插入我的 MacBook 并开启了 Fastboot 刷机模式,但令我沮丧的是,macOS 拒绝识别该设备。起初我以为这块板子是坏的(毕竟它是免费赠送的),但我还没准备好轻易放弃。我仔细研读了数据手册寻找替代的启动方法,并研究了使用 PD 电阻强制进行 QSPI 启动(使用 NORbert 代替真实的 QSPI 设备)以引导进入 Linux 用户空间,这样我就能直接刷写 eMMC 而无需依赖 Fastboot。但在拿出电烙铁之前,我决定尝试将板子插入另一台电脑,结果它立即识别出了板子。哎呀!好吧,Fastboot 可以工作,我也能够加载刷机固件并对 eMMC 进行编程(我的 Fastboot 有效载荷基于 openEuler 的 A210 指南,而不是供应商那种会刷写 7(!) 个不同分区的脚本),但之后 UART 依然死寂一片。此时我非常怀疑我的串口连接,于是决定在 Pi Pico 上进行测试:瞧,原来是我 BMP 的 vRef 引脚悬空导致的。在因为上述技术问题浪费了几个小时并丢尽了尊严之后,我看到了以下错误:sbi_trap_error: hart0: trap1: illegal instruction handler failed (error -2) …