Skip to content

Commit

Permalink
feat: default to native-compilation, add options for cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan4yin committed Mar 13, 2024
1 parent 52d63fe commit 0b48e49
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 39 deletions.
22 changes: 18 additions & 4 deletions U-Boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,28 @@ Build an sdImage by `nix build`, and then flash it to a SD card using `dd`(pleas

> **Instead of build from source, you can also download the prebuilt image from [Releases](https://github.com/ryan4yin/nixos-rk3588/releases)**.
```shell
# for orange pi 5 plus
> To understand how this flakes works, please read [Cross-platform Compilation](https://nixos-and-flakes.thiscute.world/development/cross-platform-compilation).
```bash
# ==================================
# For Orange PI 5 Plus
# ==================================
# 1. Build using the qemu-emulated aarch64 environment
# In this way, we can take advantage of the official build cache on NixOS to greatly speed up the build
nix build .#sdImage-opi5plus
# 2. Build using the cross-compilation environment
# NOTE: This will take a long time to build, as the official build cache is not available for the cross-compilation environment,
# you have to build everything from scratch.
nix build .#sdImage-opi5plus-cross

zstdcat result/sd-image/orangepi5plus-sd-image-*.img.zst | sudo dd status=progress bs=8M of=/dev/sdX

# for orange pi 5
# ==================================
# For Orange PI 5
# ==================================
nix build .#sdImage-opi5
zstdcat result/sd-image/orangepi5-sd-image-*.img.zst | sudo dd status=progress bs=8M of=/dev/sdX
# nix build .#sdImage-opi5-cross # fully cross-compiled
stdcat result/sd-image/orangepi5-sd-image-*.img.zst | sudo dd status=progress bs=8M of=/dev/sdX
```

For Rock 5A, it requires a little more work to flash the image to the sd card:
Expand Down
94 changes: 59 additions & 35 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@
}: let
# Local system's architecture, the host you are running this flake on.
localSystem = "x86_64-linux";
pkgsLocal = import nixpkgs {system = localSystem;};
# The native system of the target SBC.
aarch64System = "aarch64-linux";
pkgsLocal = import nixpkgs {system = localSystem;};
pkgsNative = import nixpkgs {system = aarch64System;};

# Cross-compilation toolchain for building on the local system.
pkgsCross = import nixpkgs {
inherit localSystem;
crossSystem = aarch64System;
};

specialArgs = {
rk3588 = {
inherit nixpkgs;
# Compile the kernel using a cross-compilation tool chain
# Which is faster than emulating the target system.
pkgsKernel = pkgsCross;
};
};
in
{
nixosModules = {
Expand All @@ -69,15 +63,14 @@
};

nixosConfigurations =
# sdImage - boot via U-Boot
# sdImage - boot via U-Boot - fully native
(builtins.mapAttrs (name: board:
nixpkgs.lib.nixosSystem {
# Use emulated target system here.
# NOTE: we use pkgsCross for the kernel build only,
# and emulated the target system for everything else,
# so that we can use nixos's official binary cache for the rest of the packages.
system = aarch64System;
inherit specialArgs;
system = aarch64System; # native or qemu-emulated
specialArgs.rk3588 = {
inherit nixpkgs;
pkgsKernel = pkgsNative;
};
modules = [
./modules/configuration.nix
board.core
Expand All @@ -90,15 +83,43 @@
];
})
self.nixosModules)
# UEFI system, boot via edk2-rk3588
# sdImage - boot via U-Boot - fully cross-compiled
// (nixpkgs.lib.mapAttrs'
(name: board:
nixpkgs.lib.nameValuePair
(name + "-cross")
(nixpkgs.lib.nixosSystem {
system = localSystem; # x64
specialArgs.rk3588 = {
inherit nixpkgs;
pkgsKernel = pkgsCross;
};
modules = [
./modules/configuration.nix
board.core
board.sd-image

{
networking.hostName = name;
sdImage.imageBaseName = "${name}-sd-image";

# Use the cross-compilation toolchain to build the whole system.
nixpkgs.crossSystem.config = "aarch64-unknown-linux-gnu";
}
];
}))
self.nixosModules)
# UEFI system, boot via edk2-rk3588 - fully native
// (nixpkgs.lib.mapAttrs'
(name: board:
nixpkgs.lib.nameValuePair
(name + "-uefi")
(nixpkgs.lib.nixosSystem {
# Use emulated target system here.
system = aarch64System;
inherit specialArgs;
system = aarch64System; # native or qemu-emulated
specialArgs.rk3588 = {
inherit nixpkgs;
pkgsKernel = pkgsNative;
};
modules = [
board.core
./modules/configuration.nix
Expand All @@ -112,33 +133,36 @@
self.nixosModules);
}
// flake-utils.lib.eachDefaultSystem (system: let
kernelPackages = pkgsCross.linuxPackagesFor (pkgsCross.callPackage ../../pkgs/kernel/legacy.nix {});
pkgs = pkgsLocal;
pkgs = import nixpkgs {inherit system;};
in {
packages = {
# sdImage
sdImage-opi5 = self.nixosConfigurations.orangepi5.config.system.build.sdImage;
sdImage-opi5plus = self.nixosConfigurations.orangepi5plus.config.system.build.sdImage;
sdImage-rock5a = self.nixosConfigurations.rock5a.config.system.build.sdImage;

sdImage-opi5-cross = self.nixosConfigurations.orangepi5-cross.config.system.build.sdImage;
sdImage-opi5plus-cross = self.nixosConfigurations.orangepi5plus-cross.config.system.build.sdImage;
sdImage-rock5a-cross = self.nixosConfigurations.rock5a-cross.config.system.build.sdImage;

# UEFI raw image
rawEfiImage-opi5 = self.nixosConfigurations.orangepi5-uefi.config.formats.raw-efi;
rawEfiImage-opi5plus = self.nixosConfigurations.orangepi5plus-uefi.config.formats.raw-efi;
rawEfiImage-rock5a = self.nixosConfigurations.rock5a-uefi.config.formats.raw-efi;

# the custom kernel for debugging
# use `nix develop` to enter the environment with the custom kernel build environment available.
# and then use `unpackPhase` to unpack the kernel source code and cd into it.
# then you can use `make menuconfig` to configure the kernel.
#
# problem
# - using `make menuconfig` - Unable to find the ncurses package.
# Solution
# - unpackPhase, and the use `nix develop .#fhsEnv` to enter the fhs test environment.
# - Then use `make menuconfig` to configure the kernel.
kernel = kernelPackages.kernel.dev;
};

# the custom kernel for debugging
# use `nix develop .#.kernel` to enter the environment with the custom kernel build environment available.
# and then use `unpackPhase` to unpack the kernel source code and cd into it.
# then you can use `make menuconfig` to configure the kernel.
#
# problem
# - using `make menuconfig` - Unable to find the ncurses package.
# Solution
# - unpackPhase, and the use `nix develop .#fhsEnv` to enter the fhs test environment.
# - Then use `make menuconfig` to configure the kernel.
# devShells.kernel = (pkgsCross.linuxPackagesFor (pkgsCross.callPackage ./pkgs/kernel/legacy.nix {})).kernel.dev;

# use `nix develop .#fhsEnv` to enter the fhs test environment defined here.
# for kernel debugging
devShells.fhsEnv =
Expand Down

0 comments on commit 0b48e49

Please sign in to comment.