1 { config, lib, pkgs, ... }:
6 cfg = config.virtualisation.virtualbox.host;
8 virtualbox = cfg.package.override {
9 inherit (cfg) enableHardening headless enableWebService;
10 extensionPack = if cfg.enableExtensionPack then pkgs.virtualboxExtpack else null;
13 kernelModules = config.boot.kernelPackages.virtualbox.override {
20 options.virtualisation.virtualbox.host = {
21 enable = mkEnableOption (lib.mdDoc "VirtualBox") // {
22 description = lib.mdDoc ''
23 Whether to enable VirtualBox.
26 In order to pass USB devices from the host to the guests, the user
27 needs to be in the `vboxusers` group.
32 enableExtensionPack = mkEnableOption (lib.mdDoc "VirtualBox extension pack") // {
33 description = lib.mdDoc ''
34 Whether to install the Oracle Extension Pack for VirtualBox.
37 You must set `nixpkgs.config.allowUnfree = true` in
38 order to use this. This requires you accept the VirtualBox PUEL.
45 default = pkgs.virtualbox;
46 defaultText = literalExpression "pkgs.virtualbox";
47 description = lib.mdDoc ''
48 Which VirtualBox package to use.
52 addNetworkInterface = mkOption {
55 description = lib.mdDoc ''
56 Automatically set up a vboxnet0 host-only network interface.
60 enableHardening = mkOption {
63 description = lib.mdDoc ''
64 Enable hardened VirtualBox, which ensures that only the binaries in the
65 system path get access to the devices exposed by the kernel modules
66 instead of all users in the vboxusers group.
69 Disabling this can put your system's security at risk, as local users
70 in the vboxusers group can tamper with the VirtualBox device files.
78 description = lib.mdDoc ''
79 Use VirtualBox installation without GUI and Qt dependency. Useful to enable on servers
80 and when virtual machines are controlled only via SSH.
84 enableWebService = mkOption {
87 description = lib.mdDoc ''
88 Build VirtualBox web service tool (vboxwebsrv) to allow managing VMs via other webpage frontend tools. Useful for headless servers.
93 config = mkIf cfg.enable (mkMerge [{
94 warnings = mkIf (pkgs.config.virtualbox.enableExtensionPack or false)
95 ["'nixpkgs.virtualbox.enableExtensionPack' has no effect, please use 'virtualisation.virtualbox.host.enableExtensionPack'"];
96 boot.kernelModules = [ "vboxdrv" "vboxnetadp" "vboxnetflt" ];
97 boot.extraModulePackages = [ kernelModules ];
98 environment.systemPackages = [ virtualbox ];
100 security.wrappers = let
102 source = "${virtualbox}/libexec/virtualbox/${program}";
113 ] ++ (lib.optionals (!cfg.headless) [
117 in mkIf cfg.enableHardening
118 (builtins.listToAttrs (map (x: { name = x; value = mkSuid x; }) executables));
120 users.groups.vboxusers.gid = config.ids.gids.vboxusers;
122 services.udev.extraRules =
124 KERNEL=="vboxdrv", OWNER="root", GROUP="vboxusers", MODE="0660", TAG+="systemd"
125 KERNEL=="vboxdrvu", OWNER="root", GROUP="root", MODE="0666", TAG+="systemd"
126 KERNEL=="vboxnetctl", OWNER="root", GROUP="vboxusers", MODE="0660", TAG+="systemd"
127 SUBSYSTEM=="usb_device", ACTION=="add", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh $major $minor $attr{bDeviceClass}"
128 SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh $major $minor $attr{bDeviceClass}"
129 SUBSYSTEM=="usb_device", ACTION=="remove", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh --remove $major $minor"
130 SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="${virtualbox}/libexec/virtualbox/VBoxCreateUSBNode.sh --remove $major $minor"
133 # Since we lack the right setuid/setcap binaries, set up a host-only network by default.
134 } (mkIf cfg.addNetworkInterface {
135 systemd.services.vboxnet0 =
136 { description = "VirtualBox vboxnet0 Interface";
137 requires = [ "dev-vboxnetctl.device" ];
138 after = [ "dev-vboxnetctl.device" ];
139 wantedBy = [ "network.target" "sys-subsystem-net-devices-vboxnet0.device" ];
140 path = [ virtualbox ];
141 serviceConfig.RemainAfterExit = true;
142 serviceConfig.Type = "oneshot";
143 serviceConfig.PrivateTmp = true;
144 environment.VBOX_USER_HOME = "/tmp";
147 if ! [ -e /sys/class/net/vboxnet0 ]; then
148 VBoxManage hostonlyif create
149 cat /tmp/VBoxSVC.log >&2
154 VBoxManage hostonlyif remove vboxnet0
158 networking.interfaces.vboxnet0.ipv4.addresses = [{ address = "192.168.56.1"; prefixLength = 24; }];
159 # Make sure NetworkManager won't assume this interface being up
160 # means we have internet access.
161 networking.networkmanager.unmanaged = ["vboxnet0"];
162 }) (mkIf config.networking.useNetworkd {
163 systemd.network.networks."40-vboxnet0".extraConfig = ''