MatthewCroughan / NixThePlanet

Run macOS, Windows and more via a single Nix command, or simple nixosModules
MIT License
534 stars 12 forks source link

Nix in Darwin #20

Open aciceri opened 6 months ago

aciceri commented 6 months ago

This adds an optionally executed (idempotent) script at the VM boot that installs Nix using the Determinate Systems' Nix installer and builds and activates a given nix-darwin config (it's also possible installing only Nix without the nix-darwin configuration).

It also adds the needed options in the module. I've been testing it on my NixOS machine with these options:

{flake, ...}: {
  services.macos-ventura = {
    enable = true;
    cores = 8;
    threads = 8;
    mem = "8G";
    vncListenAddr = "0.0.0.0";
    extraQemuFlags = [ "-nographic" ];
    sshPort = 2021;
    installNix = true;
    stateless = true;
    darwinConfig = flake.darwinConfigurations.foo;
  };
}

This is just a draft, I was considering the following improvements:

MatthewCroughan commented 6 months ago

would it make sense adding a NixOS test? I'm not very familiar with those, probably if we implement the first improvement here we can test that nix is installed but how can we test if we can activate a nix-darwin configuration? We would need to copy the realisations closure for a darwin config i.e. we would need a darwin builder to run the tests.

@roberth talked about a potential "builder-from-derivation", for Nix which would allow specifying a builder as part of a derivation. I don't see many other solutions to this problem today otherwise.

roberth commented 6 months ago

A NixOS test could work, if nested KVM virtualisation is supported and enabled on the host. You could build the nix-darwin toplevel in the VM by passing .drvPath; something like

-${toplevel}/bin/switch-to-configuration
+$(nix-store -r ${toplevel.drvPath})/bin/switch-to-configuration

(so to speak)

hercules-ci-effects has a flag for that sort of thing. It works well, and you could do something similar here. https://docs.hercules-ci.com/hercules-ci-effects/reference/nix-functions/runnixdarwin#param-buildOnDestination

builder-from-derivation

Not sure if that was a good idea tbh.