NixOS on RISCV/VisionFive 2: Difference between revisions

Onny (talk | contribs)
Update mainline kernel information
Onny (talk | contribs)
Setup: Add note on required patch for btrfs root image
 
(24 intermediate revisions by 6 users not shown)
Line 35: Line 35:
Please note that HDMI display patches haven't been merged yet.  
Please note that HDMI display patches haven't been merged yet.  


There's also a port of the UEFI reference implementation EDK2 available at https://github.com/starfive-tech/edk2 to support a future generic RISCV Linux image that can be booted from any RISCV device.
= Setup =
Precompiled SD-card images with unstable NixOS can be found [https://hydra.ztier.in/job/nixos/nixos-unstable/sd-image-riscv64-new-kernel-no-zfs-installer-cross/latest on misuzu's Hydra instance]. Before flashing the image, use <code>unzstd</code> to unpack the downloaded archive.


= Building a SD-card image =
=== Manually build a SD-card image ===
 
First create this [[Flake]] file
This example assumes you have the latest revision of the board (v1.3) with 8GB memory. For other configurations please consult the [https://github.com/NixOS/nixos-hardware/blob/master/starfive/visionfive/v2/README.md nixos-hardware documentation on this board]. First create this [[Flake]] file


{{file|flake.nix|nix|<nowiki>
{{file|flake.nix|nix|<nowiki>
{
{
   inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
   inputs.nixpkgs.url = "nixpkgs/nixos-25.11";
   inputs.nixos-hardware.url = "github:nixos/nixos-hardware";
   inputs.nixos-hardware.url = "github:nixos/nixos-hardware";


Line 72: Line 72:


               # Additional configuration goes here
               # Additional configuration goes here
              hardware.deviceTree.overlays = [{
                name = "8GB-patch";
                dtsFile = "${nixos-hardware}/starfive/visionfive/v2/8gb-patch.dts";
              }];


               sdImage.compressImage = false;
               sdImage.compressImage = false;
Line 85: Line 80:
               };
               };


               system.stateVersion = "23.11";
               system.stateVersion = "25.11";
             };
             };
           inherit system;
           inherit system;
Line 92: Line 87:
}
}
</nowiki>}}
</nowiki>}}
If you want to use an alternative filesystem for system root, for example [[Btrfs]], you could change the <code>sdImage</code>-part to this. Currently [https://github.com/NixOS/nixpkgs/pull/434122 this patch] is required to produce a functioning btrfs image.
{{file|flake.nix|nix|<nowiki>
sdImage = {
  compressImage = false;
  rootFilesystemCreator = "${pkgs.path}/nixos/lib/make-btrfs-fs.nix";
  # do this manually as replacing the resize2fs string in postBootCommands
  # might be too complex. btrfs uses its own resize command.
  expandOnBoot = false;
};
nixpkgs.overlays = [
  (final: prev: {
    ubootVisionFive2 = prev.ubootVisionFive2.overrideAttrs (oldAttrs: {
      extraConfig = (oldAttrs.extraConfig or "") + ''
        CONFIG_FS_BTRFS=y
        CONFIG_CMD_BTRFS=y
      '';
    });
  })
];
fileSystems."/" = {
  fsType = lib.mkForce "btrfs";
  options = [
    "compress=zstd"
    "noatime"
  ];
};
</nowiki>}}
It might be helpful to add [[RISC-V#Binary cache|third-party binary cache configuration]] to this system configuration.


Run following command to build the SD-card image
Run following command to build the SD-card image
Line 99: Line 127:
</syntaxhighlight>
</syntaxhighlight>


After successfull build, flash the resulting file in the directory <code>results/sd-image</code> to the target device, in this example the SD-card (<code>/dev/mmcblk*</code>). Note that everything on the target device gets erased.
=== Flashing the image ===
After successfull build or unpack, flash the resulting file (build file is in the directory <code>results/sd-image</code>) to the target device such as a NVME SSD or in this example the SD-card (<code>/dev/mmcblk*</code>). Note that everything on the target device gets erased.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 111: Line 140:
See official documentation https://doc-en.rvspace.org/VisionFive2/Quick_Start_Guide/VisionFive2_SDK_QSG/boot_mode_settings.html .
See official documentation https://doc-en.rvspace.org/VisionFive2/Quick_Start_Guide/VisionFive2_SDK_QSG/boot_mode_settings.html .


First enable booting from SD-card by setting jumper 1 and 2 to "FLASH/QSPI mode" (both QSPI and SDIO mode support booting from an SD card):
First enable booting from SD-card or NVME SSD by setting jumper 1 and 2 to "FLASH/QSPI mode" (both QSPI and SDIO mode support booting from an SD card):


[[File:Visionfive_2_jumper_config_sdcard_boot.jpg|border|frameless|803x803px]]
[[File:Visionfive_2_jumper_config_sdcard_boot.jpg|border|frameless|803x803px]]
Line 121: Line 150:
</syntaxhighlight>Bootstrap NixOS system configuration at <code>/etc/nixos/configuration.nix</code><syntaxhighlight lang="bash">
</syntaxhighlight>Bootstrap NixOS system configuration at <code>/etc/nixos/configuration.nix</code><syntaxhighlight lang="bash">
nixos-generate-config
nixos-generate-config
</syntaxhighlight>
</syntaxhighlight>It is recommended to [[RISC-V#Binary cache|configure third-party binary caches]] to speed up build times.
 
== Tips and tricks ==


== Using the Visionfive 2 as a remote builder to build native RISCV packages for e.g. the Visionfive 2 ==
=== Using the Visionfive 2 as a remote builder to build native RISCV packages for e.g. the Visionfive 2 ===
Building an NixOS system image that can be flashed to an SD card or NVMe SSD requires to '''build RISCV binaries''', more specifically for the <code>"riscv64-linux"</code>platform. From a typical Intel/AMD computer we can either
Building an NixOS system image that can be flashed to an SD card or NVMe SSD requires to '''build RISCV binaries''', more specifically for the <code>"riscv64-linux"</code>platform. From a typical Intel/AMD computer we can either


Line 138: Line 169:
# '''flash that resulting image onto an SD card''' or NVMe SSD using a call similar to <code>zstdcat result/sd-image/nixos-sd-image-23.11.20230703.ea4c80b-riscv64-linux.img.zst | sudo dd of=/dev/mmcblk0 bs=100M status=progress</code> and place that card into the Visionfive 2.
# '''flash that resulting image onto an SD card''' or NVMe SSD using a call similar to <code>zstdcat result/sd-image/nixos-sd-image-23.11.20230703.ea4c80b-riscv64-linux.img.zst | sudo dd of=/dev/mmcblk0 bs=100M status=progress</code> and place that card into the Visionfive 2.


== Deploy and Update the Visionfive 2 NixOS system once it's running NixOS ==
=== Deploy and Update the Visionfive 2 NixOS system once it's running NixOS ===
Once the Pi 5 is running NixOS, you can update it with newer NixOS system configurations using e.g. the usual  <code>nix-rebuild</code>
Once the Visionfive 2 is running NixOS, you can update it with newer NixOS system configurations using e.g. the usual  <code>nix-rebuild</code>


tool with a call similar to
tool with a call similar to


<code>nixos-rebuild --flake .#visionfive2 --build-host piuser@visionfive2 --target-host piuser@visionfive2 --use-remote-sudo switch</code>
<code>nixos-rebuild --flake .#visionfive2 --build-host user@visionfive2 --target-host user@visionfive2 --use-remote-sudo switch</code>


that uses the SSH connection from the remote builder section.
that uses the SSH connection from the remote builder section.


See [https://nixcademy.com/2023/08/10/nixos-rebuild-remote-deployment/ this guide] for a good explanation of this terminal call.
See [https://nixcademy.com/2023/08/10/nixos-rebuild-remote-deployment/ this guide] for a good explanation of this terminal call.
== See also ==
* There's also a port of the UEFI reference implementation EDK2 available at https://github.com/starfive-tech/edk2 to support a future generic RISCV Linux image that can be booted from any RISCV device.