Flake to create aarch64 iso for a rpi from a mac

Hello everyone, I’m trying to create an SD card for a raspberry 3b+ from a macbook x86_64-darwin.

My attempts have been a total failure so far.
I’ve been trying to use nixos-generators but it doesn’t seem to work. I have something like this:

/**
  nix build .#rpi
 */
{
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
    nixos-generators = {
      url = "github:nix-community/nixos-generators";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { self, nixpkgs, nixos-generators, ... }: {
    packages.aarch64-linux = {
      rpi = nixos-generators.nixosGenerate {
        system = "aarch64-linux";
        modules = [
          # you can include your own nixos configuration here, i.e.
          # ./configuration.nix
          {
            config = {
              boot.binfmt.emulatedSystems = [ "aarch64-linux" ];

              networking.hostName = "woile-1";
              system = {
                stateVersion = "22.11";

                # Disable zstd compression
                # build.sdImage.compressImage = false;
              };
              users.users.root = {
                openssh.authorizedKeys.keys = [
                  "ssh-rsa ... "
                ];
              };
            };
          }
        ];
        format = "sd-aarch64";
      };
      # vbox = nixos-generators.nixosGenerate {
      #   system = "aarch64-linux";
      #   format = "virtualbox";
      # };
    };
  };
}

And when running nix build .#rpi I get this error:

error: flake 'git+file:///REDACTED/home-cluster?dir=images' does not provide attribute 'packages.x86_64-darwin.rpi', 'legacyPackages.x86_64-darwin.rpi' or 'rpi'

When I change packages.aarch64-linux to packages.x86_64-darwin I get the following error:

error: flake 'git+file:///REDACTED/home-cluster?dir=images' does not provide attribute 'packages.x86_64-darwin.rpi', 'legacyPackages.x86_64-darwin.rpi' or 'rpi'

If I run the build like this: nix build --option system aarch64-linux --option sandbox false .#rpi I get another error:

error: 1 dependencies of derivation '/nix/store/g0rc1vxk4qadgd0dxfr0csp16ywag6b1-nixos-sd-image-23.05.20230113.befc839-aarch64-linux.img.drv' failed to build

And the logs:

bash: /nix/store/m45cjiwspshg01wrk2pln2qndasky23r-bash-5.1-p16/bin/bash: cannot execute binary file

Any idea or tips? Ideally I would like to tell the flake to use something like qemu or a vm, but it’s really hard.

Thanks!

There’s a number of issues here:

  • You’re trying to build an aarch64-linux derivation on a mac. That’s why you see does not provide attribute 'packages.x86_64-darwin.rpi'. The derivation you’ve written must be built on aarch64-linux.
  • boot.binfmt.emulatedSystems = [ "aarch64-linux" ]; You’ve placed this in the config for the ISO. That means the ISO environment will be able to emulate aarch64-linux, not your mac that you’re building on.
  • The mac doesn’t have a boot.binfmt.emulatedSystems = [ "aarch64-linux" ]; equivalent, so you actually cannot build an aarch64-linux derivation on that operating system. You will need to set up an emulator or something somehow and build with that.

In theory, ISOs can be cross compiled so you wouldn’t need an emulator or other hardware, but currently this is only possible when the build machine is a Linux system, and it’s much less likely to build successfully anyway (depending on your exact nixpkgs revision).

TL;DR: Do not try building for linux from macOS. Maybe try cross compiling with a linux builder, but frankly it’s a pain and not all that likely to work. Just set up an emulator or get some aarch64 hardware to build with.

2 Likes

Thanks a lot! That explains a lot. Any idea if there’s a dummy-proof way to run an emulator on the mac?
I wish I could import a flake to do that.

I’ve used UTM to run a Linux VM on my mac. It’s based on qemu so I’m fairly confident it’ll do emulation basically exactly the same as it does virtualization, just without the virtualization :stuck_out_tongue:

1 Like

Hey @ElvishJerricco I was wondering if you’ve seen https://mynixos.com/?
I found out it has a raspberry sample: MyNixOS
I’m not entirely sure what mynixos does, but it seems to run on a server and it could potentially output my iso image. Am I on the right track there?

Thanks a lot!