summary refs log tree commit diff
path: root/nixos/modules/virtualisation/virtualbox-image.nix
blob: da9e75a003ad70c9afc2cc75d70b03bb1f7d0b94 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.virtualbox;

in {

  options = {
    virtualbox = {
      baseImageSize = mkOption {
        type = types.int;
        default = 10 * 1024;
        description = ''
          The size of the VirtualBox base image in MiB.
        '';
      };
    };
  };

  config = {

    system.build.virtualBoxImage = import ../../lib/make-disk-image.nix {
      inherit pkgs lib config;
      partitioned = true;
      diskSize = cfg.baseImageSize;

      configFile = pkgs.writeText "configuration.nix"
        ''
          {
            imports = [ <nixpkgs/nixos/modules/virtualisation/virtualbox-image.nix> ];
          }
        '';

      postVM =
        ''
          echo "creating VirtualBox disk image..."
          ${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vdi $diskImage $out/disk.vdi
          rm $diskImage
        '';
    };

    system.build.virtualBoxOVA = pkgs.runCommand "virtualbox-ova"
      { buildInputs = [ pkgs.linuxPackages.virtualbox ];
        vmName = "NixOS ${config.system.nixosLabel} (${pkgs.stdenv.system})";
        fileName = "nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.ova";
      }
      ''
        echo "creating VirtualBox VM..."
        export HOME=$PWD
        VBoxManage createvm --name "$vmName" --register \
          --ostype ${if pkgs.stdenv.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
        VBoxManage modifyvm "$vmName" \
          --memory 1536 --acpi on --vram 32 \
          ${optionalString (pkgs.stdenv.system == "i686-linux") "--pae on"} \
          --nictype1 virtio --nic1 nat \
          --audiocontroller ac97 --audio alsa \
          --rtcuseutc on \
          --usb on --mouse usbtablet
        VBoxManage storagectl "$vmName" --name SATA --add sata --portcount 4 --bootable on --hostiocache on
        VBoxManage storageattach "$vmName" --storagectl SATA --port 0 --device 0 --type hdd \
          --medium ${config.system.build.virtualBoxImage}/disk.vdi

        echo "exporting VirtualBox VM..."
        mkdir -p $out
        VBoxManage export "$vmName" --output "$out/$fileName"
      '';

    fileSystems."/".device = "/dev/disk/by-label/nixos";

    boot.loader.grub.device = "/dev/sda";

    virtualisation.virtualbox.guest.enable = true;

  };
}