diff --git a/nixos/modules/virtualisation/virtualbox-host.nix b/nixos/modules/virtualisation/virtualbox-host.nix index 5fb472ebfc32..ce4abecd6762 100644 --- a/nixos/modules/virtualisation/virtualbox-host.nix +++ b/nixos/modules/virtualisation/virtualbox-host.nix @@ -5,7 +5,7 @@ with lib; let cfg = config.virtualisation.virtualbox.host; virtualbox = config.boot.kernelPackages.virtualbox.override { - inherit (cfg) enableHardening; + inherit (cfg) enableHardening headless; }; in @@ -47,6 +47,15 @@ in ''; }; + + headless = mkOption { + type = types.bool; + 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. + ''; + }; }; config = mkIf cfg.enable (mkMerge [{ diff --git a/nixos/tests/virtualbox.nix b/nixos/tests/virtualbox.nix index e85ff2380893..ab4d46ab7e15 100644 --- a/nixos/tests/virtualbox.nix +++ b/nixos/tests/virtualbox.nix @@ -314,6 +314,9 @@ let test2.vmFlags = hostonlyVMFlags; test2.vmScript = dhcpScript; + + headless.virtualisation.virtualbox.headless = true; + headless.services.xserver.enable = false; }; mkVBoxTest = name: testScript: makeTest { @@ -402,6 +405,14 @@ in mapAttrs mkVBoxTest { shutdownVM_simple; ''; + headless = '' + createVM_headless; + $machine->succeed(ru("VBoxHeadless --startvm headless & disown %1")); + waitForStartup_headless; + waitForVMBoot_headless; + shutdownVM_headless; + ''; + host-usb-permissions = '' my $userUSB = removeUUIDs vbm("list usbhost"); print STDERR $userUSB; diff --git a/pkgs/applications/virtualization/virtualbox/default.nix b/pkgs/applications/virtualization/virtualbox/default.nix index 6ef014f457f9..8bcf122132e3 100644 --- a/pkgs/applications/virtualization/virtualbox/default.nix +++ b/pkgs/applications/virtualization/virtualbox/default.nix @@ -1,5 +1,6 @@ { stdenv, fetchurl, lib, iasl, dev86, pam, libxslt, libxml2, libX11, xproto, libXext , libXcursor, libXmu, qt4, libIDL, SDL, libcap, zlib, libpng, glib, kernel, lvm2 +, libXrandr , which, alsaLib, curl, libvpx, gawk, nettools, dbus , xorriso, makeself, perl, pkgconfig, nukeReferences , javaBindings ? false, jdk ? null @@ -7,6 +8,7 @@ , enableExtensionPack ? false, requireFile ? null, patchelf ? null, fakeroot ? null , pulseSupport ? false, libpulseaudio ? null , enableHardening ? false +, headless ? false }: with stdenv.lib; @@ -67,12 +69,14 @@ in stdenv.mkDerivation { }; buildInputs = - [ iasl dev86 libxslt libxml2 xproto libX11 libXext libXcursor qt4 libIDL SDL + [ iasl dev86 libxslt libxml2 xproto libX11 libXext libXcursor libIDL libcap glib lvm2 python alsaLib curl libvpx pam xorriso makeself perl pkgconfig which libXmu nukeReferences ] ++ optional javaBindings jdk ++ optional pythonBindings python - ++ optional pulseSupport libpulseaudio; + ++ optional pulseSupport libpulseaudio + ++ optionals (headless) [ libXrandr libpng ] + ++ optionals (!headless) [ qt4 SDL ]; hardeningDisable = [ "fortify" "pic" "stackprotector" ]; @@ -129,7 +133,9 @@ in stdenv.mkDerivation { ''} LOCAL_CONFIG - ./configure --with-qt4-dir=${qt4} \ + ./configure \ + ${optionalString headless "--build-headless"} \ + ${optionalString (!headless) "--with-qt4-dir=${qt4}"} \ ${optionalString (!javaBindings) "--disable-java"} \ ${optionalString (!pythonBindings) "--disable-python"} \ ${optionalString (!pulseSupport) "--disable-pulse"} \ @@ -180,16 +186,18 @@ in stdenv.mkDerivation { EXTHELPER ''} - # Create and fix desktop item - mkdir -p $out/share/applications - sed -i -e "s|Icon=VBox|Icon=$libexec/VBox.png|" $libexec/virtualbox.desktop - ln -sfv $libexec/virtualbox.desktop $out/share/applications - # Icons - mkdir -p $out/share/icons/hicolor - for size in `ls -1 $libexec/icons`; do - mkdir -p $out/share/icons/hicolor/$size/apps - ln -s $libexec/icons/$size/*.png $out/share/icons/hicolor/$size/apps - done + ${optionalString (!headless) '' + # Create and fix desktop item + mkdir -p $out/share/applications + sed -i -e "s|Icon=VBox|Icon=$libexec/VBox.png|" $libexec/virtualbox.desktop + ln -sfv $libexec/virtualbox.desktop $out/share/applications + # Icons + mkdir -p $out/share/icons/hicolor + for size in `ls -1 $libexec/icons`; do + mkdir -p $out/share/icons/hicolor/$size/apps + ln -s $libexec/icons/$size/*.png $out/share/icons/hicolor/$size/apps + done + ''} # Get rid of a reference to linux.dev. nuke-refs $out/lib/modules/*/misc/*.ko diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 7f1c4aebae35..9c53f2208a11 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -11345,6 +11345,11 @@ in enableHardening = true; }); + virtualboxHeadless = lowPrio (virtualbox.override { + enableHardening = true; + headless = true; + }); + virtualboxGuestAdditions = callPackage ../applications/virtualization/virtualbox/guest-additions { }; wireguard = callPackage ../os-specific/linux/wireguard { };