summary refs log tree commit diff
path: root/nixos/modules/virtualisation/vagrant-virtualbox-image.nix
blob: 2a921894ab612e4b7216b513d551c0208e962460 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Vagrant + VirtualBox

{ config, pkgs, ... }:

{
  imports = [
    ./vagrant-guest.nix
    ./virtualbox-image.nix
  ];

  virtualbox.params = {
    audio = "none";
    audioin = "off";
    audioout = "off";
    usb = "off";
    usbehci = "off";
  };
  sound.enable = false;
  documentation.man.enable = false;
  documentation.nixos.enable = false;

  users.extraUsers.vagrant.extraGroups = [ "vboxsf" ];

  # generate the box v1 format which is much easier to generate
  # https://www.vagrantup.com/docs/boxes/format.html
  system.build.vagrantVirtualbox = pkgs.runCommand
    "virtualbox-vagrant.box"
    {}
    ''
      mkdir workdir
      cd workdir

      # 1. create that metadata.json file
      echo '{"provider":"virtualbox"}' > metadata.json

      # 2. create a default Vagrantfile config
      cat <<VAGRANTFILE > Vagrantfile
      Vagrant.configure("2") do |config|
        config.vm.base_mac = "0800275F0936"
      end
      VAGRANTFILE

      # 3. add the exported VM files
      tar xvf ${config.system.build.virtualBoxOVA}/*.ova

      # 4. move the ovf to the fixed location
      mv *.ovf box.ovf

      # 5. generate OVF manifest file
      rm *.mf
      touch box.mf
      for fname in *; do
        checksum=$(sha256sum $fname | cut -d' ' -f 1)
        echo "SHA256($fname)= $checksum" >> box.mf
      done

      # 6. compress everything back together
      tar --owner=0 --group=0 --sort=name --numeric-owner -czf $out .
    '';
}