NixOS on ARM: Difference between revisions

imported>Lheckemann
Add NanoPi M4
imported>Pickfire
Add cross compile instructions, flake and qemu/kvm note
Line 349: Line 349:
A binary cache for <code>armv7l-linux</code>, containing a [https://github.com/thefloweringash/thefloweringash-armv7/blob/master/release.nix subset] of [https://hydra.cons.org.nz/project/thefloweringash-armv7 several] channels, is hosted at https://app.cachix.org/cache/thefloweringash-armv7 .
A binary cache for <code>armv7l-linux</code>, containing a [https://github.com/thefloweringash/thefloweringash-armv7/blob/master/release.nix subset] of [https://hydra.cons.org.nz/project/thefloweringash-armv7 several] channels, is hosted at https://app.cachix.org/cache/thefloweringash-armv7 .


== Build your own image ==
== Build your own image natively ==


You can customize image by using the following snippet.
You can customize image by using the following snippet.
Line 364: Line 364:
   ];
   ];
}
}
</syntaxHighlight>
if you use the experimental flake, can do this instead and build with <code>nix build .#images.rpi2</code>:
<syntaxHighlight lang=nix>
{
  description = "Build image";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-21.11";
  outputs = { self, nixpkgs }: rec {
    nixosConfigurations.rpi2 = nixpkgs.lib.nixosSystem {
      system = "armv7l-linux";
      modules = [
        "${nixpkgs}/nixos/modules/installer/sd-card/sd-image-raspberrypi.nix"
        {
          nixpkgs.config.allowUnsupportedSystem = true;
          nixpkgs.crossSystem.system = "armv7l-linux";
          # ... extra configs as above
        }
      ];
    };
    images.rpi2 = nixosConfigurations.rpi2.config.system.build.sdImage;
  };
}
</syntaxHighlight>
</syntaxHighlight>


Line 374: Line 398:
Note that this requires a machine with aarch64. You can however also build it from your laptop using an aarch64 remote builder as described in [[Distributed build]] or ask for access on the [https://github.com/nix-community/aarch64-build-box community aarch64 builder].
Note that this requires a machine with aarch64. You can however also build it from your laptop using an aarch64 remote builder as described in [[Distributed build]] or ask for access on the [https://github.com/nix-community/aarch64-build-box community aarch64 builder].


=== Compiling through QEMU ===
=== Cross-compiling ===


It is also possible to compile for aarch64 on your non-aarch64 local machine, or a remote builder, by registering QEMU as a binfmt wrapper for the aarch64 architecture. This <b>wrapper uses emulation</b> and will therefore be slower than comparable native machines.
It is possible to cross-compile from a different architecture. To cross-compile to <code>armv7l</code>, on the same <code>sd-image.nix</code> add in <code>crossSystem</code>:
 
<syntaxHighlight lang=nix>
{ ... }: {
  nixpkgs.crossSystem.system = "armv7l-linux";
  imports = [
    <nixpkgs/nixos/modules/installer/sd-card/sd-image-aarch64.nix>
  ];
  # ...
}
</syntaxHighlight>
 
=== Compiling through binfmt QEMU ===
 
It is also possible to compile for aarch64 on your non-aarch64 local machine, or a remote builder, by registering QEMU as a binfmt wrapper for the aarch64 architecture. This <b>wrapper uses emulation</b> and will therefore be slower than comparable native machines or cross-compiling.


To enable the binfmt wrapper on NixOS, add the following to <code>configuration.nix</code>
To enable the binfmt wrapper on NixOS, add the following to <code>configuration.nix</code>
Line 396: Line 434:
</syntaxHighlight>
</syntaxHighlight>
(the last option should not be required on NixOS machines)
(the last option should not be required on NixOS machines)
=== Compiling through QEMU/kvm ===
It is also possible to build nixos images through full emulation using QEMU/kvm but will be way slower than native and binfmt QEMU.


== Installer image with custom U-Boot ==
== Installer image with custom U-Boot ==