-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allowing NixOS VM's to be run on macOS #108984
Comments
Another benefit if we get MacOS + QEMU testing story working: ATM, running NixOS tests on various CI providers such as CircleCI and GitHub Actions is very slow since these services do not provide |
I don't have a lot of time right now, but here's a basic skeleton that could be used to create a libvirt xml config for seeing if that approach is viable: let
pkgs = import <nixpkgs> {};
config = (import <nixpkgs/nixos> {
system = "x86_64-linux";
configuration = <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>;
}).config;
in pkgs.writeText "nixos.xml" ''
<domain type='qemu' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
...
<os>
...
<kernel>${config.system.build.toplevel}/kernel</kernel>
...
</os>
...
<filesystem type='mount' accessmode='$security_model'>
<source dir='/nix/store'/>
<target dir='store'/>
</filesystem>
...
</domain>
'' |
Refs #5241 |
Did you see https://passthroughpo.st/mac-os-adds-early-support-for-virtio-qemu/ |
@domenkozar Yeah, click the "uncovered by Qemu developer" link there, it leads to a blogpost I linked above, which only mentions virtio-9p, not virtio-fs. I'm now just seeing that that news article mistakenly used virtio-fs in its picture.. |
Ah! That was confused me. |
Benchmarks show that should also speed up the NixOS tests https://matrix.org/_matrix/media/r0/download/johnguant.com/qgAVstnDkCydsABgKmOXRPGW cc @JJJollyjim do you have a branch for this? |
The patch you linked seems to be only one of a series - presumably you need all of them for it to work. The series is visible here: https://lore.kernel.org/qemu-devel/[email protected]/. Not sure if there's a way to get the whole series from Patchwork. |
Oh hey, just catching up here, I'm a little confused - the stuff where apple has added support for something is all about osx guests, not osx hosts which is what we care about here right? I don't believe the current virtiofsd (host component) is able to run on osx (e.g. it uses namespaces to sandbox itself), but I don't think there is any technical limitation stopping it being ported? I recommend patchew if you want a nice way to search for qemu patches and their status: https://patchew.org/QEMU/[email protected]/ (wish the kernel had something like this... -_-). Anyway, let me know if you find that switching to virtio-fs will make OSX support work: I have a branch, but haven't worked on cleaning it up and posting it because for some reason the DAX patches still haven't been submitted to qemu (last I checked), and without DAX the tiny performance improvement isn't worth the complexity. That calculus changes if it will fix OSX though :) |
On second thoughts, I patch out the namespace stuff (so it works in the nix sandbox), so it's possible it will run on osx without any further changes. I don't have an osx machine to test on :( |
I have a macOS machine I'm happy to test stuff on if it'd be helpful for you.
Not quite sure what you're referring to, but here's a quick run-down of Apple's recent-ish features for macOS hosts:
(As a side note, I've been experimenting with using Virtualization.framework to implement a simple Linux VM, using a tiny linux kernel with u-root as a simple "bootloader" to find and exec the real kernel image off a Linux filesystem; I've got it working with Ubuntu, but not NixOS; I suspect there's some issue with virtio-console support on the install ISO.) |
@Gaelan I was referring to Domen's Passthru Post link about virtio :) |
Ah, my bad. |
I ported the original 9p patchset (mentioned earlier in this thread) to QEMU 5.2.0. In initial tests, 9p support on Darwin appears to be working. But this is a large change that should probably go to upstream and not to Nixpkgs. If you want to try anyways: mroi@839559b |
@mroi Would be help to at least submit the patch to Homebrew. Thanks for the work! I'll look on giving it a try. |
I would really love the test driver to run on MacOS with HAXM enabled. (-enable-hax) I tried to merge both patches from @mroi and @infinisil and ran the same command as above :
But I get this error :
|
For testing patches you'll need a remote builder to build the Linux bits. The quickest way is to follow https://nix.dev/tutorials/continuous-integration-github-actions.html and get them from your binary cache. |
Steps left:
|
You mean as a PR toward Nixpkgs or toward upstream QEMU? I can easily do the former. For the latter I would need to familiarise myself with their e-mail-based workflow. (Anyone here with experience in that?) |
Former, although upstream patch would be really nice. |
The linux kernel has a bot explaining their email workflow which you might find similar with QEMU |
@r2r-dev did you manage to get it working? |
Yup. I've prepared 2 test branches:
Obviously, both of these branches aren't pretty and surely will need some cleanup. Some remarks:
Refs: Mic92/nixos-shell#16 |
When I try to build % nix build github:NixOS/nixpkgs/594b94b4c3038f5c2cfb2f5d9c10ef30c7070a4c#darwin.builder
error: build of '/nix/store/rnwbjdkq9wz43nlsmggr7ysn7k9h9z2w-nixos-disk-image.drv' on 'ssh-ng://builder@localhost' failed: builder for '/nix/store/rnwbjdkq9wz43nlsmggr7ysn7k9h9z2w-nixos-disk-image.drv' failed with exit code 32;
last 10 log lines:
> copying path '/nix/store/65cvxfd36l9cawzj1gkx3zpzj0bdfg0b-unit-serial-getty-.service' to 'local'...
> copying path '/nix/store/8izgxj2nzpwkd0hpm3r38zkiklm1jkzr-unit-nixos-activation.service' to 'local'...
> copying path '/nix/store/82nc5jrzri2w4hw6rhffis9m6n25xnbv-unit-systemd-fsck-.service' to 'local'...
> copying path '/nix/store/r9h4h2j299iz32gnnr2qksz2hsi6i8cw-unit-systemd-udevd.service' to 'local'...
> copying path '/nix/store/klkbaapdbcs35bxyp0l97ic3lpp0njnb-user-units' to 'local'...
> copying path '/nix/store/c04f008fdyqmarjl6nf35fdrzfm9h6lk-system-units' to 'local'...
> copying path '/nix/store/786ah3bs0n5lcvjq2qhp4laiqkg60wr0-etc' to 'local'...
> copying path '/nix/store/mj26i1zj0bjv1b732bafrg2ffly34adj-nixos-system-nixos-23.05pre-git' to 'local'...
> mount: /build/root/build/root: must be superuser to use mount.
> dmesg(1) may have more information after failed mount system call.
For full logs, run 'nix log /nix/store/rnwbjdkq9wz43nlsmggr7ysn7k9h9z2w-nixos-disk-image.drv'.
error: builder for '/nix/store/rnwbjdkq9wz43nlsmggr7ysn7k9h9z2w-nixos-disk-image.drv' failed with exit code 1
error: 1 dependencies of derivation '/nix/store/wr12pkszc3gj021fy08cch9syhqvwyj2-run-nixos-vm.drv' failed to build
error: 1 dependencies of derivation '/nix/store/p5pa1imyb5yc6l3wy1cda5bz38dc430f-nixos-vm.drv' failed to build
error: 1 dependencies of derivation '/nix/store/ld4rlr7sf599qi53bmd4d2xrjsm53n11-create-builder.drv' failed to build #210812 also seems to have broken other builds - which triggered this emergency fix: #211218. Doesn't seem to have made it to the unstable branch yet though. https://nixpk.gs/pr-tracker.html?pr=211218 |
Is it possible to add NixOS modules to the I ask because I'd like to run this on our |
@dhess: Yes. If you copy the code from here: nixpkgs/pkgs/top-level/darwin-packages.nix Lines 215 to 233 in 0cf78f5
… then you can add in additional modules of your own |
In case you need more than 3GB of memory for your builder# Starts darwin.builder VM with 8GB RAM
QEMU_OPTS="-m 8192" nix run nixpkgs#darwin.builder |
Does qemu support passing rosetta binary to guest system? Would be awesome to enable #202847 to support building for x86_64-linux too. |
@misuzu Rosetta "drive" is mounted via higher-level Virtualisation.framework while QEMU uses lower-level Hypervisor.framework. The later is more like kvm while the former is more like QEMU itself, so I doubt its support will happen in QEMU. I think, #5241 is relevant to bringing virtualisation backends other than QEMU for running NixOS VMs. |
UTM does support it, is supposedly based on QEMU, is packaged, and we have a NixOS module to support it on its guests #202847. If it has a qemu compatible command line it might be close to a drop-in replacement. If it's like qemu, you can set |
@roberth UTM docs clearly state https://docs.getutm.app/advanced/rosetta/:
QEMU VMs don't have this setting in UTM. |
@roberth IIUC, UTM only supports rosetta if you tell it to use Apple Virtualisation instead of QEMU. |
I love QEMU, but this is awesome. I still don't quiiite have copy/paste working this way (not sure if spice + wayland is a thing), but using Apple Virtualization solves the other major pain point of having normal-ish resolutions relative to my laptop and external monitor working with wayland/sway on a graphical NixOS VM. I feel like there should be a flashing exclamation point somewhere that this works so well. |
This was outdated information then. Thanks for correcting me. I was thinking about the VM tests in my last comment. It'd be far easier to add Apple Virtualisation support if it's just for the
|
Maybe this could be adapted to our use-case: Another approach might be to mount |
I do wish the kernel had support for enabling TSO mode per process. I don't even know if that's possible in a VM, but it'd be very nice for asahi linux to be able to use the rosetta binary via hacks and with TSO enabled. |
There's a vmcli project: https://github.com/gyf304/vmcli#vmcli-1 - it allows to run VMs with Virtualisation.framework from CLI. We could use it to start VM and add Rosetta support to it. |
There is also Tart that has Rosetta support. One just need to: brew install cirruslabs/cli/tart
tart clone ghcr.io/cirruslabs/ubuntu:20.04 ubuntu
tart run --rosetta="rosetta" ubuntu And make sure Rosetta is configured inside the VM according to these docs. |
I've gotten pretty far with Tart. Thanks to @fkorotkov for making me aware of it in the above comment!
I've spent most of my time on this so far just learning how Even better would be to use the Mac's
So far, so good. However, my particular use case for this VM is to use it as an Looking at the logs, the problem is apparent: This makes sense: Nix is running in multi-user mode on my Mac, so my Mac's Anyway, besides that small catch, this route looks very promising. I think the main issue for our use case is that |
@dhess: You probably don't want to share the host's nixpkgs/nixos/modules/profiles/macos-builder.nix Lines 159 to 168 in 9956319
|
@Gabriella439 Thanks, I did see that comment linked from elsewhere, either in this discussion, or in a related one. However, if I understand the comment correctly, I don't think it applies in our use case. We have several dedicated macOS remote builders (let's call them
Perhaps it's possible that |
Over in this podman issue, I see that virtiofs in MacOS doesn't bypass open file limits, which leads to unexpected behavoir in the guest OS: containers/podman#16106 I'm addressing the issue directly in podman, but I was hoping someone from this issue might have an opinion on if qemu should be bumping it's own ulimits up when it is acting as the virtiofs daemon? |
Closes #193336 Closes #261694 Related to #108984 The goal here was to get the following flake to build and run on `aarch64-darwin`: ```nix { inputs.nixpkgs.url = <this branch>; outputs = { nixpkgs, ... }: { checks.aarch64-darwin.default = nixpkgs.legacyPackages.aarch64-darwin.nixosTest { name = "test"; nodes.machine = { }; testScript = ""; }; }; } ``` … and after this change it does. There's no longer a need for the user to set `nodes.*.nixpkgs.pkgs` or `nodes.*.virtualisation.host.pkgs` as the correct values are inferred from the host system.
You can now run NixOS tests on macOS, too. See: #282401 Note that you still need a Linux builder to build the test VM, though |
People using macOS currently can't interactively run NixOS VM's as described e.g. here, even with a remote Linux builder. In this issue I'm describing some ways in how that could be made to work. Note that speed is important as well, so
kvm
/hvf
and co. should be used if possible.Relevant issue is #64578. Ping @zupo @matthewbauer @nmattia @roberth. Any pointers/help with this is appreciated.
This issue is sponsored by Niteo :)
Using qemu directly (doesn't work)
With this change it's possible to build a NixOS VM run script that is executable on macOS (note that this requires a remote Linux builder)
However running the VM doesn't actually work:
This is because qemu doesn't support virtfs on macOS.
It was suggested that this patch could be used to make it support virtfs. This was attempted here, however the build doesn't succeed:
Attempting to remove all the missing header files from
virtfs-proxy-helper.c
, including<sys/fsuid.h>
,<sys/vfs.h>
,<linux/fs.h>
and<cap-ng.h>
just leads to compilation errors, seemingly indicating that this is a Linux-only functionality.This page however mentions that:
And indeed, the
run-nixos-vm
script only useslocal
, quoting the script:Attempting to not compile that tool with this commit however also doesn't work, failing again on seemingly Linux-specific headers:
So it seems that qemu just doesn't support virtfs on macOS.
Using libvirt (might work)
Above qemu documentation also has this section, which describes how the same can be achieved with
libvirt
. While I believe that underneath it just uses qemu as well, there might be some additional libvirt magic happening. And it seems that macOS Mojave supports virtio-9p, in which post the author also uses libvirt successfully.Currently the NixOS VM runner just passes arguments to qemu. In order to use libvirt we'll have to transform all these arguments to the libvirt equivalent in its XML configuration. There even is a libvirt page describing the equivalent of qemu arguments, which will be very useful. There is also the
virsh domxml-from-native qemu-argv
command which can supposedly do this transformation automatically, though I didn't have any success with it yet.In above-linked document describing qemu argument equivalents, the
-virtfs
(or the-fsdev
and-device
options it's a shorthand for) option is notably missing. This should be replaced with a<filesystem>
section as described in the qemu documentation link above.It would be a good idea to first manually create a libvirt configuration and verifying that it works on a NixOS VM. libvirt has a graphical interface which could also be used.
More relevant links:
virtio-9p
since version 6.9.0virtio-9p
can be slow. There is a faster replacementvirtio-fs
. However as far as I know, macOS doesn't have a driver for that.Removing the need for filesystem mapping (might work)
The main reason this
-virtfs
argument is used at all is so that the guest machine in the VM can access the host machines/nix/store
, which is where the whole system that the guest runs resides in.An alternate approach however could be to create a
/nix/store
image that can be used by qemu as the/nix/store
directly, therefore not depending on this filesystem mapping to the host.See the various
make-*
files in https://github.com/NixOS/nixpkgs/tree/master/nixos/lib, which could be useful for thisThe text was updated successfully, but these errors were encountered: