2020-04-24 23:36:52 +00:00
|
|
|
# Module for VirtualBox guests.
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.virtualisation.virtualbox.guest;
|
|
|
|
kernel = config.boot.kernelPackages;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
mkVirtualBoxUserService = serviceArgs: {
|
|
|
|
description = "VirtualBox Guest User Services ${serviceArgs}";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
wantedBy = [ "graphical-session.target" ];
|
|
|
|
partOf = [ "graphical-session.target" ];
|
|
|
|
|
|
|
|
# The graphical session may not be ready when starting the service
|
|
|
|
# Hence, check if the DISPLAY env var is set, otherwise fail, wait and retry again
|
|
|
|
startLimitBurst = 20;
|
|
|
|
|
|
|
|
unitConfig.ConditionVirtualization = "oracle";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
# Check if the display environment is ready, otherwise fail
|
|
|
|
preStart = "${pkgs.bash}/bin/bash -c \"if [ -z $DISPLAY ]; then exit 1; fi\"";
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxClient --foreground ${serviceArgs}";
|
|
|
|
# Wait after a failure, hoping that the display environment is ready after waiting
|
|
|
|
RestartSec = 2;
|
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2024-06-20 14:57:18 +00:00
|
|
|
imports = [
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRenamedOptionModule [ "virtualisation" "virtualbox" "guest" "draganddrop" ] [ "virtualisation" "virtualbox" "guest" "dragAndDrop" ])
|
2024-06-20 14:57:18 +00:00
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
options.virtualisation.virtualbox.guest = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to enable the VirtualBox service and other guest additions.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
clipboard = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = true;
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to enable clipboard support.";
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
seamless = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
default = true;
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to enable seamless mode. When activated windows from the guest appear next to the windows of the host.";
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
dragAndDrop = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
default = true;
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to enable drag and drop support.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf cfg.enable (lib.mkMerge [
|
2024-04-21 15:54:59 +00:00
|
|
|
{
|
|
|
|
assertions = [{
|
|
|
|
assertion = pkgs.stdenv.hostPlatform.isx86;
|
|
|
|
message = "Virtualbox not currently supported on ${pkgs.stdenv.hostPlatform.system}";
|
|
|
|
}];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
environment.systemPackages = [ kernel.virtualboxGuestAdditions ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
boot.extraModulePackages = [ kernel.virtualboxGuestAdditions ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
boot.supportedFilesystems = [ "vboxsf" ];
|
|
|
|
boot.initrd.supportedFilesystems = [ "vboxsf" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
users.groups.vboxsf.gid = config.ids.gids.vboxsf;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
systemd.services.virtualbox = {
|
|
|
|
description = "VirtualBox Guest Services";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
requires = [ "dev-vboxguest.device" ];
|
|
|
|
after = [ "dev-vboxguest.device" ];
|
|
|
|
|
|
|
|
unitConfig.ConditionVirtualization = "oracle";
|
|
|
|
|
|
|
|
serviceConfig.ExecStart = "@${kernel.virtualboxGuestAdditions}/bin/VBoxService VBoxService --foreground";
|
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
services.udev.extraRules =
|
|
|
|
''
|
|
|
|
# /dev/vboxuser is necessary for VBoxClient to work. Maybe we
|
|
|
|
# should restrict this to logged-in users.
|
|
|
|
KERNEL=="vboxuser", OWNER="root", GROUP="root", MODE="0666"
|
|
|
|
|
|
|
|
# Allow systemd dependencies on vboxguest.
|
|
|
|
SUBSYSTEM=="misc", KERNEL=="vboxguest", TAG+="systemd"
|
|
|
|
'';
|
|
|
|
|
|
|
|
systemd.user.services.virtualboxClientVmsvga = mkVirtualBoxUserService "--vmsvga-session";
|
|
|
|
}
|
|
|
|
(
|
2024-09-19 14:19:46 +00:00
|
|
|
lib.mkIf cfg.clipboard {
|
2024-04-21 15:54:59 +00:00
|
|
|
systemd.user.services.virtualboxClientClipboard = mkVirtualBoxUserService "--clipboard";
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
2024-09-19 14:19:46 +00:00
|
|
|
lib.mkIf cfg.seamless {
|
2024-04-21 15:54:59 +00:00
|
|
|
systemd.user.services.virtualboxClientSeamless = mkVirtualBoxUserService "--seamless";
|
|
|
|
}
|
|
|
|
)
|
2024-06-05 15:53:02 +00:00
|
|
|
(
|
2024-09-19 14:19:46 +00:00
|
|
|
lib.mkIf cfg.dragAndDrop {
|
2024-06-05 15:53:02 +00:00
|
|
|
systemd.user.services.virtualboxClientDragAndDrop = mkVirtualBoxUserService "--draganddrop";
|
|
|
|
}
|
|
|
|
)
|
2024-04-21 15:54:59 +00:00
|
|
|
]);
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|