1 # Module for VirtualBox guests.
9 cfg = config.virtualisation.virtualbox.guest;
10 kernel = config.boot.kernelPackages;
12 mkVirtualBoxUserService = serviceArgs: {
13 description = "VirtualBox Guest User Services ${serviceArgs}";
15 wantedBy = [ "graphical-session.target" ];
16 partOf = [ "graphical-session.target" ];
18 # The graphical session may not be ready when starting the service
19 # Hence, check if the DISPLAY env var is set, otherwise fail, wait and retry again
22 unitConfig.ConditionVirtualization = "oracle";
24 # Check if the display environment is ready, otherwise fail
25 preStart = "${pkgs.bash}/bin/bash -c \"if [ -z $DISPLAY ]; then exit 1; fi\"";
27 ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxClient --foreground ${serviceArgs}";
28 # Wait after a failure, hoping that the display environment is ready after waiting
36 (lib.mkRenamedOptionModule
52 options.virtualisation.virtualbox.guest = {
53 enable = lib.mkOption {
55 type = lib.types.bool;
56 description = "Whether to enable the VirtualBox service and other guest additions.";
59 clipboard = lib.mkOption {
61 type = lib.types.bool;
62 description = "Whether to enable clipboard support.";
65 seamless = lib.mkOption {
67 type = lib.types.bool;
68 description = "Whether to enable seamless mode. When activated windows from the guest appear next to the windows of the host.";
71 dragAndDrop = lib.mkOption {
73 type = lib.types.bool;
74 description = "Whether to enable drag and drop support.";
80 config = lib.mkIf cfg.enable (
85 assertion = pkgs.stdenv.hostPlatform.isx86;
86 message = "Virtualbox not currently supported on ${pkgs.stdenv.hostPlatform.system}";
90 environment.systemPackages = [ kernel.virtualboxGuestAdditions ];
92 boot.extraModulePackages = [ kernel.virtualboxGuestAdditions ];
94 boot.supportedFilesystems = [ "vboxsf" ];
95 boot.initrd.supportedFilesystems = [ "vboxsf" ];
97 users.groups.vboxsf.gid = config.ids.gids.vboxsf;
99 systemd.services.virtualbox = {
100 description = "VirtualBox Guest Services";
102 wantedBy = [ "multi-user.target" ];
103 requires = [ "dev-vboxguest.device" ];
104 after = [ "dev-vboxguest.device" ];
106 unitConfig.ConditionVirtualization = "oracle";
108 serviceConfig.ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxService VBoxService --foreground";
111 services.udev.extraRules = ''
112 # /dev/vboxuser is necessary for VBoxClient to work. Maybe we
113 # should restrict this to logged-in users.
114 KERNEL=="vboxuser", OWNER="root", GROUP="root", MODE="0666"
116 # Allow systemd dependencies on vboxguest.
117 SUBSYSTEM=="misc", KERNEL=="vboxguest", TAG+="systemd"
120 systemd.user.services.virtualboxClientVmsvga = mkVirtualBoxUserService "--vmsvga-session";
122 (lib.mkIf cfg.clipboard {
123 systemd.user.services.virtualboxClientClipboard = mkVirtualBoxUserService "--clipboard";
125 (lib.mkIf cfg.seamless {
126 systemd.user.services.virtualboxClientSeamless = mkVirtualBoxUserService "--seamless";
128 (lib.mkIf cfg.dragAndDrop {
129 systemd.user.services.virtualboxClientDragAndDrop = mkVirtualBoxUserService "--draganddrop";