Mic92/nixos-shell

macOS support?

Opened this issue ยท 9 comments

what is needed?

Mic92 commented

This would need some major refactorings in nixpkgs itself. Qemu would need to be built for macOS itself, while the image itself needs to target Linux. Basically the NixOS module that provides our qemu abstraction needs to evaluate on macOS and than nixpkgs is imported again with Linux as the target for the rest.

Mic92 commented

Interestingly enough there is a macOS target for the qemu binary: https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/qemu-flags.nix#L28
Not sure where is this used so.

I don't think it needs refactoring for that reason. I almost got it running with this config:

{ pkgs, ... }: {
  nixos-shell.mounts.mountHome = false;
  system.build.qemu = (import <nixpkgs> {}).pkgs.qemu_test;
}

However, it seems like virtfs isn't supported on macos, so it still failed:

$ nixos-shell -- --arg system '"x86_64-linux"'
/nix/store/4292rkypmhb9fzmc4yj4dnjy27j5iy3h-nixos-vm
qemu-system-x86_64: -virtfs local,path=/nix/store,security_model=none,mount_tag=store: There is no option group 'virtfs'
qemu-system-x86_64: -virtfs local,path=/nix/store,security_model=none,mount_tag=store: virtfs support is disabled

I don't know if there are other options for mounting the nix store, but this is how far I've gotten now.

Mic92 commented

qemu has builtin support for smb, which might work. Otherwise NFS could be used. The latter lone might require a bit more setup code.

I've managed to get nixos-shell working on macOS by tying together PRs waiting to be merged to nixpkgs as well as some of my own changes:

Mic92 commented

Might be also interesting for you: @kloenk

@r2r-dev what about aarch64? I have a m1 Mac, so would be interested in that.

Haven't tried that yet. afaik x86_64-darwin qemu should work just fine via rosetta, however, it might not be the best way to go. To make it somehow more usable:

  1. I'll need to compile an aarch64-darwin version of qemu v6.0.0 target, so we could skip rosetta and use TCG for emulating aarch64 and x86_64 guests. [0]
  2. Enable HVF support on aarch64-darwin qemu by applying [1] on top of the v6.0.0 tree.

With the above changes in place, it should be possible to run virtualized aarch64-linux guests, as well as emulated x86_64 guests.

Finally, there's some additional patching required to make a network adapter somewhat more performant [2].

Also, it looks like @benpye has some experience in this field [3].

I'll test nixos-shell on m1 Mac with this overlay applied on top of my nixpkgs fork. Stay tuned.

[0]: https://patchwork.kernel.org/project/qemu-devel/patch/20210103145055.11074-1-r.bolshakov@yadro.com/
[1]: https://patchwork.kernel.org/project/qemu-devel/list/?series=400619&state=%2A&archive=both
[2]: https://lists.gnu.org/archive/html/qemu-devel/2021-02/msg04637.html
[3]: https://github.com/benpye/nix-config/blob/main/overlays/qemu/default.nix