1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 boolToStr = value: if value then "on" else "off";
7 cfg = config.vmware;
8
9 subformats = [
10 "monolithicSparse"
11 "monolithicFlat"
12 "twoGbMaxExtentSparse"
13 "twoGbMaxExtentFlat"
14 "streamOptimized"
15 ];
16
17in {
18 options = {
19 vmware = {
20 baseImageSize = mkOption {
21 type = with types; either (enum [ "auto" ]) int;
22 default = "auto";
23 example = 2048;
24 description = lib.mdDoc ''
25 The size of the VMWare base image in MiB.
26 '';
27 };
28 vmDerivationName = mkOption {
29 type = types.str;
30 default = "nixos-vmware-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
31 description = lib.mdDoc ''
32 The name of the derivation for the VMWare appliance.
33 '';
34 };
35 vmFileName = mkOption {
36 type = types.str;
37 default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.vmdk";
38 description = lib.mdDoc ''
39 The file name of the VMWare appliance.
40 '';
41 };
42 vmSubformat = mkOption {
43 type = types.enum subformats;
44 default = "monolithicSparse";
45 description = lib.mdDoc "Specifies which VMDK subformat to use.";
46 };
47 vmCompat6 = mkOption {
48 type = types.bool;
49 default = false;
50 example = true;
51 description = lib.mdDoc "Create a VMDK version 6 image (instead of version 4).";
52 };
53 };
54 };
55
56 config = {
57 system.build.vmwareImage = import ../../lib/make-disk-image.nix {
58 name = cfg.vmDerivationName;
59 postVM = ''
60 ${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o compat6=${boolToStr cfg.vmCompat6},subformat=${cfg.vmSubformat} -O vmdk $diskImage $out/${cfg.vmFileName}
61 rm $diskImage
62 '';
63 format = "raw";
64 diskSize = cfg.baseImageSize;
65 partitionTableType = "efi";
66 inherit config lib pkgs;
67 };
68
69 fileSystems."/" = {
70 device = "/dev/disk/by-label/nixos";
71 autoResize = true;
72 fsType = "ext4";
73 };
74
75 fileSystems."/boot" = {
76 device = "/dev/disk/by-label/ESP";
77 fsType = "vfat";
78 };
79
80 boot.growPartition = true;
81
82 boot.loader.grub = {
83 version = 2;
84 device = "nodev";
85 efiSupport = true;
86 efiInstallAsRemovable = true;
87 };
88
89 virtualisation.vmware.guest.enable = true;
90 };
91}