mitchellh / nixos-config

My NixOS configurations.
https://twitter.com/mitchellh/status/1346136404682625024
MIT License
1.93k stars 191 forks source link

allow to prepare the VMWare image #1

Open zimbatm opened 2 years ago

zimbatm commented 2 years ago

This is more of a showcase of a feature which I really like with NixOS.

If the CI or another machine has Nix installed, it's possible to generate a new VMDK directly from the NixOS configuration. Building and copying the VMDK would replace almost all the bootstrap steps from the video. A similar technique is also possible to generate AMIs and other system images from the same config.

I wasn't able to test this unfortunately because I don't have VMWare installed. You might have to set a password to the user using the NixOS config as well in order to be able to login.

mitchellh commented 2 years ago

Well god damn this is cool. I'll have to give this a shot.

fud commented 2 years ago

I tried this - does this require nixos to build? I tried standard nix on my mac and it didn't work, but this would be a much easier way.

zimbatm commented 2 years ago

There is a bit of a bootstrap problem because you need a Linux machine with Nix installed to generate the image.

One way to work around the issue is to use GitHub Actions to build and push the VM. Or you can install Nix on your macOS, and set up a Nix remote builder like https://nixbuild.net .

lucamaraschi commented 2 years ago

@zimbatm do you have any example of how the GH action would look like?This could really create pre-baked images , making the whole re-produceability and idempotency phenomenal

zimbatm commented 2 years ago

First, we need to add a GHA to the repo. So I created https://github.com/mitchellh/nixos-config/pull/14

Then append the following steps to the action (make sure to fix the indent so it's the same as the other steps):

- run: nix build .#vmwareImage
- uses: actions/upload-artifact@v2
  with:
    name: vmware
    path: ./result/*.vmdk
zimbatm commented 2 years ago

Note that this approach is also applicable to building VirtualBox, Qemu, Vagrant, AMIs, ... by including the right NixOS module. It takes a bit of fiddling and reading the code still, but here is a good inspiration source: https://github.com/nix-community/nixos-generators/

jtara1 commented 6 months ago

I was able to build a qcow2 image with minimal nix config using

nix-build '<nixpkgs/nixos>' -A config.system.build.qcow2 --arg configuration "{ imports = [ ./build-qcow2.nix ]; }"

where the nixos config for the build and for the guest OS are defined here