1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.virtualisation.vmware.guest;
7 open-vm-tools = if cfg.headless then pkgs.open-vm-tools-headless else pkgs.open-vm-tools;
8 xf86inputvmmouse = pkgs.xorg.xf86inputvmmouse;
9in
10{
11 imports = [
12 (mkRenamedOptionModule [ "services" "vmwareGuest" ] [ "virtualisation" "vmware" "guest" ])
13 ];
14
15 options.virtualisation.vmware.guest = {
16 enable = mkEnableOption "VMWare Guest Support";
17 headless = mkOption {
18 type = types.bool;
19 default = !config.services.xserver.enable;
20 defaultText = "!config.services.xserver.enable";
21 description = "Whether to disable X11-related features.";
22 };
23 };
24
25 config = mkIf cfg.enable {
26 assertions = [ {
27 assertion = pkgs.stdenv.hostPlatform.isx86 || pkgs.stdenv.hostPlatform.isAarch64;
28 message = "VMWare guest is not currently supported on ${pkgs.stdenv.hostPlatform.system}";
29 } ];
30
31 boot.initrd.availableKernelModules = [ "mptspi" ];
32 boot.initrd.kernelModules = lib.optionals pkgs.stdenv.hostPlatform.isx86 [ "vmw_pvscsi" ];
33
34 environment.systemPackages = [ open-vm-tools ];
35
36 systemd.services.vmware =
37 { description = "VMWare Guest Service";
38 wantedBy = [ "multi-user.target" ];
39 after = [ "display-manager.service" ];
40 unitConfig.ConditionVirtualization = "vmware";
41 serviceConfig.ExecStart = "${open-vm-tools}/bin/vmtoolsd";
42 };
43
44 # Mount the vmblock for drag-and-drop and copy-and-paste.
45 systemd.mounts = mkIf (!cfg.headless) [
46 {
47 description = "VMware vmblock fuse mount";
48 documentation = [ "https://github.com/vmware/open-vm-tools/blob/master/open-vm-tools/vmblock-fuse/design.txt" ];
49 unitConfig.ConditionVirtualization = "vmware";
50 what = "${open-vm-tools}/bin/vmware-vmblock-fuse";
51 where = "/run/vmblock-fuse";
52 type = "fuse";
53 options = "subtype=vmware-vmblock,default_permissions,allow_other";
54 wantedBy = [ "multi-user.target" ];
55 }
56 ];
57
58 security.wrappers.vmware-user-suid-wrapper = mkIf (!cfg.headless) {
59 setuid = true;
60 owner = "root";
61 group = "root";
62 source = "${open-vm-tools}/bin/vmware-user-suid-wrapper";
63 };
64
65 environment.etc.vmware-tools.source = "${open-vm-tools}/etc/vmware-tools/*";
66
67 services.xserver = mkIf (!cfg.headless) {
68 modules = [ xf86inputvmmouse ];
69
70 config = ''
71 Section "InputClass"
72 Identifier "VMMouse"
73 MatchDevicePath "/dev/input/event*"
74 MatchProduct "ImPS/2 Generic Wheel Mouse"
75 Driver "vmmouse"
76 EndSection
77 '';
78
79 displayManager.sessionCommands = ''
80 ${open-vm-tools}/bin/vmware-user-suid-wrapper
81 '';
82 };
83
84 services.udev.packages = [ open-vm-tools ];
85 };
86}