• Stars
    star
    456
  • Rank 92,478 (Top 2 %)
  • Language
    Nix
  • License
    MIT License
  • Created 7 months ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run macOS, Windows and more via a single Nix command, or simple nixosModules


NixThePlanet

This is a Nix flake that allows you to run medieval operating systems, some new and some old. Some other candidate names for this flake were:

  • ProprietareKackscheisse
  • nix-zoo
  • Windows 12
  • menagerie

Inspired by the great Astro and his WFVM flake for building Windows VM images

It took at least a painstaking month to make this project. If you use this project and enjoy it, it would mean a lot if you could sponsor me via GitHub Sponsors, and whilst you're at it, why not sponsor Hercules CI too for making the CI in this repo so incredible.

This code was made whilst listening to Windows96

Thanks

Massive thanks to the following for various kinds of help!

  • Michael Hoang - Getting started with the idea and nerd sniping me at nix.camp
  • Robert Hensing - Putting up with endless questions about determinism
  • Max Headroom - Figuring out non deterministic fetching in fetch-macOS-v2.py, tl;dr it's the Board ID
  • cleverca22 - Helping me sift through the QEMU source code and so much more
  • pkharvey - Unmatched DOS/Amiga expertise and talking for countless hours about how to do this
  • Raito Bezarius - Explaining theory about VMs, Kernels, IO and deadlines to me in a way I could understand
  • Gaétan Lepage - Allowing me to test for weeks on his machine, installing macOS hundreds of times
  • Felix - Allowing me to test for weeks on his machine, installing macOS hundreds of times
  • ElvishJerricco - Helping me figure out some QEMU CLI flags

Prior Art

Without work such as OSX-KVM by Kholia, and macOS-Simple-KVM by Foxlet, a repo like mine could not possibly exist. To bootstrap the project, I use some of the OSX-KVM repo as a Flake input. It contains some qcow2 files that I don't know how to reproduce yet, as noted below in the TODOs.

As pointed out to me on Twitter, Cirrus Labs had made a Hashicorp Packer template to do similar automation by using VNC and Sleeps. The major difference between this and what I am doing, is that I use TCL Expect and Tesseract OCR to more reliably get the same result, without relying as much on sleeps/waits. Additionally, the CI for this repo runs the macOS installer 10 times whenever anything changes, to validate that nothing is broken, and that the function makeDarwinImage works reliably. There is also a NixOS test that boots the VM, in a VM, and tests that the macOS VM is able to be SSH'd into on port 22.

macOS

Currently, only macOS Ventura is supported, building will take *at least 40-50 minutes as the official 11GiB macOS installer is downloaded and used in the Nix sandbox. No user interaction is required. Be patient and sit tight.

Launch macOS Ventura with a single nix command

GTK

nix run github:matthewcroughan/NixThePlanet#macos-ventura

VNC (Port 5901)

You can pass QEMU flags like -vnc

nix run github:matthewcroughan/NixThePlanet#macos-ventura -- -vnc 0.0.0.0:1

Using the nixosModule

To enable the VM as a NixOS service via the nixosModule enable the macos-ventura module on a nixosConfiguration in your flake.nix

  • SSH is accessible on port 2222 by default, but is configurable via services.macos-ventura.sshPort
  • VNC is accessible on port 5900 by default, but is configurable via services.macos-ventura.vncDisplayNumber
{
  inputs = {
    nixtheplanet.url = "github:matthewcroughan/nixtheplanet";
    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
  };
  outputs = { self, nixpkgs, nixtheplanet }: {
    nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
      modules = [
        nixtheplanet.nixosModules.macos-ventura
        {
          services.macos-ventura = {
            enable = true;
            openFirewall = true;
            vncListenAddr = "0.0.0.0";
          };
        }
      ];
    };
  };
}

Using the makeDarwinImage function

This flake exports a function makeDarwinImage which takes a diskSizeBytes argument in order to influence the disk size of the resulting VM, it could be used like this in a flake.nix for example

{
  inputs = {
    nixtheplanet.url = "github:matthewcroughan/nixtheplanet";
    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
  };
  outputs = { self, nixpkgs, nixtheplanet }: {
    # Create a 60GB Darwin disk image, two ways of doing the same thing
    # x is accessing legacyPackages directly from the flake
    # y is applying the overlay from nixtheplanet unto its own instance of nixpkgs

    x = nixtheplanet.legacyPackages.x86_64-linux.makeDarwinImage { diskSizeBytes = 60000000000; };
    y = (import nixpkgs { system = "x86_64-linux"; overlays = [ nixtheplanet.overlays.default ]; }).makeDarwinImage { diskSizeBytes = 60000000000; };
  };
}

Using the makeDarwinImage function directly, you could increase the size of the macOS image used by services.macos-ventura.enable = true in your NixOS config as follows:

{ pkgs, ... }:
{
  services.macos-ventura = {
    enable = true;
    package = pkgs.makeDarwinImage { diskSizeBytes = 60000000000; };
  };
}

Windows/DOS

Each of the outputs in this flake have their own image builders and runScript.

  • makeMsDos622Image
  • makeWin30Image
  • makeWfwg311Image
  • makeWin98Image

They can each be passed the dosPostInstall argument arbitrary dos commands to be ran after Windows has been installed, for example here's how you can use them to build an image that adds win to the AUTOEXEC.BAT

Example
makeWin30Image {
  dosPostInstall = ''
    c:
    echo win >> AUTOEXEC.BAT
  '';
}

The runScript is a method of the image builder, for example makeWin30Image {}).runScript. Additionally there is a makeRunScript method which can be passed arguments like diskImage.

Example
(makeWin30Image {}).makeRunScript {
  diskImage = makeWin30Image {
    dosPostInstall = "echo foo";
  };
}

MS Dos 6.22

Launch MS Dos 6.22 with a single nix command

nix run github:matthewcroughan/NixThePlanet#msdos622

msdos622

Windows 3.0

Launch Windows 3.0 with a single nix command

nix run github:matthewcroughan/NixThePlanet#win30

win30

Windows 3.11 (For Workgroups)

Launch Windows For WorkGroups 3.11 with a single nix command

nix run github:matthewcroughan/NixThePlanet#wfwg311

wfwg311

Windows 98

Launch Windows 98 with a single nix command

nix run github:matthewcroughan/NixThePlanet#win98

win98

TODO

  • Allow diskSizeBytes to be grown by a second layer, sshing into the base image and performing the necessary operations to grow the disk, instead of reinstalling each time
  • Install Nix into the Darwin VM as part of the installation via SSH by using ${pkgs.nix.outPath}/scripts/install.in
  • Support installing/initialising a nix-darwin configuration as part of the installation
  • Remove dependency on vncdo, use qemu framebuffer directly
  • Create nixosModules and VM Tests for win30, wfwg311, msdos622
  • Create derivation based checks/tests for win30, wfwg311, msdos622 using telnet
  • Find a more reproducible way of fetching macOS BaseSystems, currently the board identifier determines what is fetched, and Apple changes what OS is compatible with which board identifier routinely
  • Add amiga, macOS System 1 to 7, Windows 1.0, Windows 95 and Windows 98, and the rest.
  • Find a way of getting serial access for Dosbox so we can make runInWin311 and similar functions
  • Implement runInDarwinVM using the makeDarwinImage primitive
  • Remove dependency on OSX-KVM that is currently being copied into the repository without a git submodule
  • Reproduce OpenCore qcow2 image ourselves
  • Put screen captures into $out for the image builders using vncdo, would help with debugging
  • Better logging in image builders
  • Maybe make a framework for using expect and tesseract together with Nix, similar to the NixOS Testing Framework to reduce code duplication in this repo
  • Make some installation options configurable, such as initial username/pass for all image builders
  • Create a watchdog to retry if failure/hanging is encountered
  • Create runInDos primitive using telnet and dosbox-x serial, would look something like:
    runInDos ''
      c:
      echo hello world > file
    ''
    
  • Make the Windows < 3.11 installers less dependent on source floppy disk count, perhaps by making a single 10MB FAT16/FAT12 HDD/FLOPPY with all the files in, so DOSBox only has to mount a single disk, or maybe we can just make a Windows rootfs ourselves from scratch

Known Issues

  • On some CPUs macOS will fail to boot when using multiple cores due to macOS lacking drivers for host CPU timers, this has been encountered on an AMD Ryzen 2700U for example,

  • If the VM is too slow, Apple's macOS installer can hit race conditions and hang. A retry of the build of the derivation will usually fix this.

Notes

Notable changes to the OpenCore-Boot.sh script for the OSX-KVM repository that I am copy-pasting into this repo temporarily for bootstrapping purposes are:

  • Using snapshot=on and generating QCOW2 images backed by raw disk images to improve performance and disk usage during installation phase
  • Using Virtio for all disks and gpu
  • Disabling nic during installation
  • Doing everything offline by default
  • Limiting CPU to one core and thread for determinism, there were some threading issues during installation that caused non deterministic behavior and failed installations that lead to this.

I am also changing scripts/run_offline.sh to automatically partition the Disk, and not embedding it into the InstallAssistant, to allow for reconfiguration of the run_offline script in a separate derivation.

Reviews

Chris McDonough

In this review, Chris McDonough appears very excited. Apparently, NixThePlanet has the power to make grown men giggle.

More Repositories

1

nixcfg

My nix configuration(s), using flakes. It's my laptop, it's my servers, it's my everything, in code.
Nix
243
star
2

nixinate

Another NixOS Deployment Tool - Nixinate your systems 🕶️
Nix
215
star
3

visionfive-nix

Nix
41
star
4

raspberrypi-nixos-example

The simplest possible way to begin using and extending a NixOS Configuration with a Raspberry Pi
Nix
29
star
5

hamnix

A collection of Amateur Radio software packaged for Nix
Nix
22
star
6

filestash-nix

A flake for testing, building and using Filestash by Mickael Kerjean
Nix
19
star
7

nix-dropper-bundle

Bundlers to drop software onto systems, for good or bad intent
Nix
11
star
8

nrf-nix

A Nix flake for developing and working with Nordic Semiconductor hardware
Nix
6
star
9

nixblitz

NixBlitz - Using Nix to implement RaspiBlitz with NixOS as the base
Nix
6
star
10

mobile-nixos-oneplus-enchilada-template

A template for the oneplus-enchilada (OnePlus 6) using Mobile NixOS
Nix
6
star
11

dolphin-emu-nix

A flake for testing, building and packaging the Dolphin Gamecube emulator
Nix
5
star
12

nixkvm

A Flake for distributing PiKVM running on Embedded NixOS
Nix
4
star
13

static-nix-in-repo

A ridiculous idea, to provide the Nix binary as part of the git repo
Shell
4
star
14

disko-utils

Fun with https://github.com/nix-community/disko
Nix
4
star
15

esp-r-nix

A Nix flake for building ESP-R
Nix
3
star
16

painter

Nix + Stable Diffusion + Vosk = Fun
Nix
1
star
17

haskell.fix

Nix
1
star
18

userland-mqtt-stack

Installs and provisions mqtt, grafana, jupyter-notebook, node-red and influxdb for usage inside UserLAnd on Android
Shell
1
star
19

nix-pi3-pi4-systemimage-example

Creates an arm64 image for usage with a Pi3 and or Pi4
Nix
1
star
20

talks

A repository of my talks
Nix
1
star
21

h96-max-v58-nixos

Nix
1
star
22

OpenFlam

1
star
23

mobile-nixos-templates

Templates for Mobile NixOS devices
Nix
1
star
24

pi-intercom

Personal rudimentary files to produce a two-way intercom with two raspberry pi's using Seren
Python
1
star
25

xmimsim-nix

Nix
1
star