1# Module for VirtualBox guests.
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
8 cfg = config.virtualisation.virtualbox.guest;
9 kernel = config.boot.kernelPackages;
10
11 mkVirtualBoxUserService = serviceArgs: {
12 description = "VirtualBox Guest User Services ${serviceArgs}";
13
14 wantedBy = [ "graphical-session.target" ];
15 partOf = [ "graphical-session.target" ];
16
17 # The graphical session may not be ready when starting the service
18 # Hence, check if the DISPLAY env var is set, otherwise fail, wait and retry again
19 startLimitBurst = 20;
20
21 unitConfig.ConditionVirtualization = "oracle";
22
23 # Check if the display environment is ready, otherwise fail
24 preStart = "${pkgs.bash}/bin/bash -c \"if [ -z $DISPLAY ]; then exit 1; fi\"";
25 serviceConfig = {
26 ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxClient --foreground ${serviceArgs}";
27 # Wait after a failure, hoping that the display environment is ready after waiting
28 RestartSec = 2;
29 Restart = "always";
30 };
31 };
32in
33{
34 ###### interface
35
36 options.virtualisation.virtualbox.guest = {
37 enable = mkOption {
38 default = false;
39 type = types.bool;
40 description = "Whether to enable the VirtualBox service and other guest additions.";
41 };
42
43 clipboard = mkOption {
44 default = true;
45 type = types.bool;
46 description = "Whether to enable clipboard support.";
47 };
48
49 seamless = mkOption {
50 default = true;
51 type = types.bool;
52 description = "Whether to enable seamless mode. When activated windows from the guest appear next to the windows of the host.";
53 };
54
55 draganddrop = mkOption {
56 default = true;
57 type = types.bool;
58 description = "Whether to enable drag and drop support.";
59 };
60 };
61
62 ###### implementation
63
64 config = mkIf cfg.enable (mkMerge [
65 {
66 assertions = [{
67 assertion = pkgs.stdenv.hostPlatform.isx86;
68 message = "Virtualbox not currently supported on ${pkgs.stdenv.hostPlatform.system}";
69 }];
70
71 environment.systemPackages = [ kernel.virtualboxGuestAdditions ];
72
73 boot.extraModulePackages = [ kernel.virtualboxGuestAdditions ];
74
75 boot.supportedFilesystems = [ "vboxsf" ];
76 boot.initrd.supportedFilesystems = [ "vboxsf" ];
77
78 users.groups.vboxsf.gid = config.ids.gids.vboxsf;
79
80 systemd.services.virtualbox = {
81 description = "VirtualBox Guest Services";
82
83 wantedBy = [ "multi-user.target" ];
84 requires = [ "dev-vboxguest.device" ];
85 after = [ "dev-vboxguest.device" ];
86
87 unitConfig.ConditionVirtualization = "oracle";
88
89 serviceConfig.ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxService VBoxService --foreground";
90 };
91
92 services.udev.extraRules =
93 ''
94 # /dev/vboxuser is necessary for VBoxClient to work. Maybe we
95 # should restrict this to logged-in users.
96 KERNEL=="vboxuser", OWNER="root", GROUP="root", MODE="0666"
97
98 # Allow systemd dependencies on vboxguest.
99 SUBSYSTEM=="misc", KERNEL=="vboxguest", TAG+="systemd"
100 '';
101
102 systemd.user.services.virtualboxClientVmsvga = mkVirtualBoxUserService "--vmsvga-session";
103 }
104 (
105 mkIf cfg.clipboard {
106 systemd.user.services.virtualboxClientClipboard = mkVirtualBoxUserService "--clipboard";
107 }
108 )
109 (
110 mkIf cfg.seamless {
111 systemd.user.services.virtualboxClientSeamless = mkVirtualBoxUserService "--seamless";
112 }
113 )
114 ]);
115}