1 # Module for VirtualBox guests.
2 { config, lib, pkgs, ... }:
4 cfg = config.virtualisation.virtualbox.guest;
5 kernel = config.boot.kernelPackages;
7 mkVirtualBoxUserService = serviceArgs: {
8 description = "VirtualBox Guest User Services ${serviceArgs}";
10 wantedBy = [ "graphical-session.target" ];
11 partOf = [ "graphical-session.target" ];
13 # The graphical session may not be ready when starting the service
14 # Hence, check if the DISPLAY env var is set, otherwise fail, wait and retry again
17 unitConfig.ConditionVirtualization = "oracle";
19 # Check if the display environment is ready, otherwise fail
20 preStart = "${pkgs.bash}/bin/bash -c \"if [ -z $DISPLAY ]; then exit 1; fi\"";
22 ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxClient --foreground ${serviceArgs}";
23 # Wait after a failure, hoping that the display environment is ready after waiting
31 (lib.mkRenamedOptionModule [ "virtualisation" "virtualbox" "guest" "draganddrop" ] [ "virtualisation" "virtualbox" "guest" "dragAndDrop" ])
34 options.virtualisation.virtualbox.guest = {
35 enable = lib.mkOption {
37 type = lib.types.bool;
38 description = "Whether to enable the VirtualBox service and other guest additions.";
41 clipboard = lib.mkOption {
43 type = lib.types.bool;
44 description = "Whether to enable clipboard support.";
47 seamless = lib.mkOption {
49 type = lib.types.bool;
50 description = "Whether to enable seamless mode. When activated windows from the guest appear next to the windows of the host.";
53 dragAndDrop = lib.mkOption {
55 type = lib.types.bool;
56 description = "Whether to enable drag and drop support.";
62 config = lib.mkIf cfg.enable (lib.mkMerge [
65 assertion = pkgs.stdenv.hostPlatform.isx86;
66 message = "Virtualbox not currently supported on ${pkgs.stdenv.hostPlatform.system}";
69 environment.systemPackages = [ kernel.virtualboxGuestAdditions ];
71 boot.extraModulePackages = [ kernel.virtualboxGuestAdditions ];
73 boot.supportedFilesystems = [ "vboxsf" ];
74 boot.initrd.supportedFilesystems = [ "vboxsf" ];
76 users.groups.vboxsf.gid = config.ids.gids.vboxsf;
78 systemd.services.virtualbox = {
79 description = "VirtualBox Guest Services";
81 wantedBy = [ "multi-user.target" ];
82 requires = [ "dev-vboxguest.device" ];
83 after = [ "dev-vboxguest.device" ];
85 unitConfig.ConditionVirtualization = "oracle";
87 serviceConfig.ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxService VBoxService --foreground";
90 services.udev.extraRules =
92 # /dev/vboxuser is necessary for VBoxClient to work. Maybe we
93 # should restrict this to logged-in users.
94 KERNEL=="vboxuser", OWNER="root", GROUP="root", MODE="0666"
96 # Allow systemd dependencies on vboxguest.
97 SUBSYSTEM=="misc", KERNEL=="vboxguest", TAG+="systemd"
100 systemd.user.services.virtualboxClientVmsvga = mkVirtualBoxUserService "--vmsvga-session";
103 lib.mkIf cfg.clipboard {
104 systemd.user.services.virtualboxClientClipboard = mkVirtualBoxUserService "--clipboard";
108 lib.mkIf cfg.seamless {
109 systemd.user.services.virtualboxClientSeamless = mkVirtualBoxUserService "--seamless";
113 lib.mkIf cfg.dragAndDrop {
114 systemd.user.services.virtualboxClientDragAndDrop = mkVirtualBoxUserService "--draganddrop";