2024-10-14 04:46:43 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2014-11-27 17:24:57 +00:00
|
|
|
let
|
2015-08-13 10:17:32 +00:00
|
|
|
cfg = config.virtualisation.virtualbox.host;
|
2016-09-13 01:42:16 +00:00
|
|
|
|
2018-08-11 18:40:00 +00:00
|
|
|
virtualbox = cfg.package.override {
|
2024-10-14 04:46:43 +00:00
|
|
|
inherit (cfg)
|
|
|
|
enableHardening
|
|
|
|
headless
|
|
|
|
enableWebService
|
|
|
|
enableKvm
|
|
|
|
;
|
2018-08-10 17:48:29 +00:00
|
|
|
extensionPack = if cfg.enableExtensionPack then pkgs.virtualboxExtpack else null;
|
2014-12-15 06:08:56 +00:00
|
|
|
};
|
|
|
|
|
2016-09-13 01:42:16 +00:00
|
|
|
kernelModules = config.boot.kernelPackages.virtualbox.override {
|
|
|
|
inherit virtualbox;
|
|
|
|
};
|
|
|
|
|
2014-11-27 17:24:57 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2015-08-13 10:17:32 +00:00
|
|
|
options.virtualisation.virtualbox.host = {
|
2024-08-28 19:19:13 +00:00
|
|
|
enable = lib.mkEnableOption "VirtualBox" // {
|
2014-12-15 06:33:56 +00:00
|
|
|
description = ''
|
2015-08-12 12:11:03 +00:00
|
|
|
Whether to enable VirtualBox.
|
2014-12-15 06:33:56 +00:00
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
In order to pass USB devices from the host to the guests, the user
|
|
|
|
needs to be in the `vboxusers` group.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2014-12-15 06:33:56 +00:00
|
|
|
'';
|
|
|
|
};
|
2014-12-15 06:08:56 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
enableExtensionPack = lib.mkEnableOption "VirtualBox extension pack" // {
|
2018-08-10 17:48:29 +00:00
|
|
|
description = ''
|
|
|
|
Whether to install the Oracle Extension Pack for VirtualBox.
|
|
|
|
|
|
|
|
::: {.important}
|
|
|
|
You must set `nixpkgs.config.allowUnfree = true` in
|
|
|
|
order to use this. This requires you accept the VirtualBox PUEL.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2018-08-10 17:48:29 +00:00
|
|
|
'';
|
|
|
|
};
|
2018-05-01 20:45:31 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
package = lib.mkPackageOption pkgs "virtualbox" { };
|
2018-05-01 20:45:31 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
addNetworkInterface = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2014-12-15 06:08:56 +00:00
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Automatically set up a vboxnet0 host-only network interface.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
enableHardening = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2014-12-18 17:12:25 +00:00
|
|
|
default = true;
|
2014-12-15 06:08:56 +00:00
|
|
|
description = ''
|
|
|
|
Enable hardened VirtualBox, which ensures that only the binaries in the
|
|
|
|
system path get access to the devices exposed by the kernel modules
|
|
|
|
instead of all users in the vboxusers group.
|
|
|
|
|
|
|
|
::: {.important}
|
|
|
|
Disabling this can put your system's security at risk, as local users
|
|
|
|
in the vboxusers group can tamper with the VirtualBox device files.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2014-12-15 06:08:56 +00:00
|
|
|
'';
|
2014-12-11 22:28:09 +00:00
|
|
|
};
|
2016-09-01 18:54:58 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
headless = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2016-09-01 18:54:58 +00:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Use VirtualBox installation without GUI and Qt dependency. Useful to enable on servers
|
|
|
|
and when virtual machines are controlled only via SSH.
|
|
|
|
'';
|
|
|
|
};
|
2020-11-02 06:54:00 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
enableWebService = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2020-11-02 06:54:00 +00:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Build VirtualBox web service tool (vboxwebsrv) to allow managing VMs via other webpage frontend tools. Useful for headless servers.
|
|
|
|
'';
|
|
|
|
};
|
2024-02-13 14:53:54 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
enableKvm = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2024-02-13 14:53:54 +00:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Enable KVM support for VirtualBox. This increases compatibility with Linux kernel versions, because the VirtualBox kernel modules
|
|
|
|
are not required.
|
|
|
|
|
2024-06-18 22:12:16 +00:00
|
|
|
This option is incompatible with `addNetworkInterface`.
|
2024-02-13 14:53:54 +00:00
|
|
|
|
|
|
|
Note: This is experimental. Please check https://github.com/cyberus-technology/virtualbox-kvm/issues.
|
|
|
|
'';
|
|
|
|
};
|
2014-11-27 17:24:57 +00:00
|
|
|
};
|
|
|
|
|
2024-10-14 04:46:43 +00:00
|
|
|
config = lib.mkIf cfg.enable (
|
|
|
|
lib.mkMerge [
|
2024-02-13 14:53:54 +00:00
|
|
|
{
|
2024-10-14 04:46:43 +00:00
|
|
|
warnings = lib.mkIf (pkgs.config.virtualbox.enableExtensionPack or false) [
|
|
|
|
"'nixpkgs.virtualbox.enableExtensionPack' has no effect, please use 'virtualisation.virtualbox.host.enableExtensionPack'"
|
|
|
|
];
|
|
|
|
environment.systemPackages = [ virtualbox ];
|
|
|
|
|
|
|
|
security.wrappers =
|
|
|
|
let
|
|
|
|
mkSuid = program: {
|
|
|
|
source = "${virtualbox}/libexec/virtualbox/${program}";
|
|
|
|
owner = "root";
|
|
|
|
group = "vboxusers";
|
|
|
|
setuid = true;
|
|
|
|
};
|
|
|
|
executables =
|
|
|
|
[
|
|
|
|
"VBoxHeadless"
|
|
|
|
"VBoxNetAdpCtl"
|
|
|
|
"VBoxNetDHCP"
|
|
|
|
"VBoxNetNAT"
|
|
|
|
"VBoxVolInfo"
|
|
|
|
]
|
|
|
|
++ (lib.optionals (!cfg.headless) [
|
|
|
|
"VBoxSDL"
|
|
|
|
"VirtualBoxVM"
|
|
|
|
]);
|
|
|
|
in
|
|
|
|
lib.mkIf cfg.enableHardening (
|
|
|
|
builtins.listToAttrs (
|
|
|
|
map (x: {
|
|
|
|
name = x;
|
|
|
|
value = mkSuid x;
|
|
|
|
}) executables
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
users.groups.vboxusers.gid = config.ids.gids.vboxusers;
|
|
|
|
|
|
|
|
services.udev.extraRules = ''
|
|
|
|
SUBSYSTEM=="usb_device", ACTION=="add", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh $major $minor $attr{bDeviceClass}"
|
|
|
|
SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh $major $minor $attr{bDeviceClass}"
|
|
|
|
SUBSYSTEM=="usb_device", ACTION=="remove", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh --remove $major $minor"
|
|
|
|
SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh --remove $major $minor"
|
|
|
|
'';
|
2024-02-13 14:53:54 +00:00
|
|
|
}
|
2024-10-14 04:46:43 +00:00
|
|
|
(lib.mkIf cfg.enableKvm {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !cfg.addNetworkInterface;
|
|
|
|
message = "VirtualBox KVM only supports standard NAT networking for VMs. Please turn off virtualisation.virtualbox.host.addNetworkInterface.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
})
|
|
|
|
(lib.mkIf (!cfg.enableKvm) {
|
|
|
|
boot.kernelModules = [
|
|
|
|
"vboxdrv"
|
|
|
|
"vboxnetadp"
|
|
|
|
"vboxnetflt"
|
|
|
|
];
|
|
|
|
boot.extraModulePackages = [ kernelModules ];
|
|
|
|
|
|
|
|
services.udev.extraRules = ''
|
|
|
|
KERNEL=="vboxdrv", OWNER="root", GROUP="vboxusers", MODE="0660", TAG+="systemd"
|
|
|
|
KERNEL=="vboxdrvu", OWNER="root", GROUP="root", MODE="0666", TAG+="systemd"
|
|
|
|
KERNEL=="vboxnetctl", OWNER="root", GROUP="vboxusers", MODE="0660", TAG+="systemd"
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Since we lack the right setuid/setcap binaries, set up a host-only network by default.
|
|
|
|
})
|
|
|
|
(lib.mkIf cfg.addNetworkInterface {
|
|
|
|
systemd.services.vboxnet0 = {
|
|
|
|
description = "VirtualBox vboxnet0 Interface";
|
|
|
|
requires = [ "dev-vboxnetctl.device" ];
|
|
|
|
after = [ "dev-vboxnetctl.device" ];
|
|
|
|
wantedBy = [
|
|
|
|
"network.target"
|
|
|
|
"sys-subsystem-net-devices-vboxnet0.device"
|
|
|
|
];
|
|
|
|
path = [ virtualbox ];
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.PrivateTmp = true;
|
|
|
|
environment.VBOX_USER_HOME = "/tmp";
|
|
|
|
script = ''
|
2014-11-27 17:24:57 +00:00
|
|
|
if ! [ -e /sys/class/net/vboxnet0 ]; then
|
|
|
|
VBoxManage hostonlyif create
|
2014-12-15 18:12:58 +00:00
|
|
|
cat /tmp/VBoxSVC.log >&2
|
2014-11-27 17:24:57 +00:00
|
|
|
fi
|
|
|
|
'';
|
2024-10-14 04:46:43 +00:00
|
|
|
postStop = ''
|
2014-11-27 17:24:57 +00:00
|
|
|
VBoxManage hostonlyif remove vboxnet0
|
|
|
|
'';
|
2024-10-14 04:46:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
networking.interfaces.vboxnet0.ipv4.addresses = [
|
|
|
|
{
|
|
|
|
address = "192.168.56.1";
|
|
|
|
prefixLength = 24;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
# Make sure NetworkManager won't assume this interface being up
|
|
|
|
# means we have internet access.
|
|
|
|
networking.networkmanager.unmanaged = [ "vboxnet0" ];
|
|
|
|
})
|
|
|
|
(lib.mkIf config.networking.useNetworkd {
|
|
|
|
systemd.network.networks."40-vboxnet0".extraConfig = ''
|
|
|
|
[Link]
|
|
|
|
RequiredForOnline=no
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
|
|
|
|
]
|
|
|
|
);
|
2014-11-27 17:24:57 +00:00
|
|
|
}
|