Cross-compile kexec image (x86_64 host, arm64 target)

Hello nix community!

I am trying to follow an existing guide for installing nixOS onto an Oracle Cloud VM, which is written assuming the VM is x86_64 – however, in my case I am attempting to adapt the guide/approach for an ARM64 VM.

Link to the guide:

The general approach is to replace an initial Ubuntu install by booting a kexec-based image and install nixOS from there.

My difficulty at the moment is replacing the config.system.build.kernel (and config.system.build.netbootRamdisk) in the following section with something that cross-compiles a kernel from an x86_64 Linux host to arm64:

kexec.nix
{ config, pkgs, ... }:
{
  imports = [
    # this will work only under qemu, uncomment next line for full image
    # <nixpkgs/nixos/modules/installer/netboot/netboot-minimal.nix>
    <nixpkgs/nixos/modules/installer/netboot/netboot.nix>
    <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
  ];

  # stripped down version of https://github.com/cleverca22/nix-tests/tree/master/kexec
  system.build = rec {
    image = pkgs.runCommand "image" { buildInputs = [ pkgs.nukeReferences ]; } ''
      mkdir $out
      cp ${config.system.build.kernel}/bzImage $out/kernel
      cp ${config.system.build.netbootRamdisk}/initrd $out/initrd
      nuke-refs $out/kernel
    '';

...
<omitted remaining contents of kexec.nix>
...

So far I added the following snippet to the top of the file, and replaced some of the occurences of “pkgs” with “pkgs.crossSystem”:

let pkgs = import <nixpkgs> {
  crossSystem = {
    config = "aarch64-unknown-linux-gnu";
  };
};
in
<kexec.nix contents from the guide...>

Hopefully someone can suggest a way how to replace config with something that is compatible with the cross-compile target!

Unless you’re pursuing this for educational value, consider using nixos-infect - works out of the box on Oracle’s ARM offerings.

Wow @ius, thanks for the tip on nixos-infect; that was super fast – and easy! – and no intermediate build machine needed. nixOS is happily running on my (free) OCI ARM64 instance; I can move on with putting Oracle’s resources to work for Folding@Home.

2 Likes

I’m still curious how to get a cross-compiled equivalent to the “cp ${config.system.build.kernel}/bzImage $out/kernel” line, since I do some cross-compiling occasionally and I think nix has the potential to be very powerful in my cross-compiling toolbox.

1 Like