52 # If open-watcom-bin is not passed, VirtualBox will fall back to use
53 # the shipped alternative sources (assembly).
59 jdk, # Almost doesn't affect closure size
60 pythonBindings ? false,
64 pulseSupport ? config.pulseaudio or stdenv.hostPlatform.isLinux,
66 enableHardening ? false,
68 enable32bitGuests ? true,
69 enableWebService ? false,
71 extraConfigureFlags ? "",
74 # The web services use Java infrastructure.
75 assert enableWebService -> javaBindings;
78 buildType = "release";
79 # Use maintainers/scripts/update.nix to update the version and all related hashes or
80 # change the hashes in extpack.nix and guest-additions/default.nix as well manually.
81 virtualboxVersion = "7.0.22";
82 virtualboxSha256 = "cf3ddf633ca410f1b087b0722413e83247cda4f14d33323dc122a4a42ff61981";
84 kvmPatchVersion = "20240828";
85 kvmPatchHash = "sha256-g0esJbB1IGyLGZMLFJIY8ZYdHWuiM5IZtLMHZvCY6bs=";
87 # The KVM build is not compatible to VirtualBox's kernel modules. So don't export
89 withModsrc = !enableKvm;
91 virtualboxGuestAdditionsIso = callPackage guest-additions-iso/default.nix { };
101 stdenv.mkDerivation (finalAttrs: {
102 pname = "virtualbox";
103 version = finalAttrs.virtualboxVersion;
111 virtualboxGuestAdditionsIso
115 url = "https://download.virtualbox.org/virtualbox/${finalAttrs.virtualboxVersion}/VirtualBox-${finalAttrs.virtualboxVersion}.tar.bz2";
116 sha256 = finalAttrs.virtualboxSha256;
119 outputs = [ "out" ] ++ optional withModsrc "modsrc";
121 nativeBuildInputs = [
128 ] ++ optional (!headless) wrapQtAppsHook;
130 # Wrap manually because we wrap just a small number of executables.
131 dontWrapQtApps = true;
161 ++ optional javaBindings jdk
162 ++ optional pythonBindings python3 # Python is needed even when not building bindings
163 ++ optional pulseSupport libpulseaudio
164 ++ optionals headless [ libGL ]
165 ++ optionals (!headless) [
172 ++ optionals enableWebService [
186 sed -e 's@MKISOFS --version@MKISOFS -version@' \
187 -e 's@PYTHONDIR=.*@PYTHONDIR=${optionalString pythonBindings python3}@' \
188 -e 's@CXX_FLAGS="\(.*\)"@CXX_FLAGS="-std=c++11 \1"@' \
190 optionalString (!headless) ''
191 -e 's@TOOLQT5BIN=.*@TOOLQT5BIN="${getDev qtbase}/bin"@' \
194 ls kBuild/bin/linux.x86/k* tools/linux.x86/bin/* | xargs -n 1 patchelf --set-interpreter ${stdenv.cc.libc}/lib/ld-linux.so.2
195 ls kBuild/bin/linux.amd64/k* tools/linux.amd64/bin/* | xargs -n 1 patchelf --set-interpreter ${stdenv.cc.libc}/lib/ld-linux-x86-64.so.2
197 grep 'libpulse\.so\.0' src include -rI --files-with-match | xargs sed -i -e '
198 ${optionalString pulseSupport ''s@"libpulse\.so\.0"@"${libpulseaudio.out}/lib/libpulse.so.0"@g''}'
200 grep 'libdbus-1\.so\.3' src include -rI --files-with-match | xargs sed -i -e '
201 s@"libdbus-1\.so\.3"@"${dbus.lib}/lib/libdbus-1.so.3"@g'
203 grep 'libasound\.so\.2' src include -rI --files-with-match | xargs sed -i -e '
204 s@"libasound\.so\.2"@"${alsa-lib.out}/lib/libasound.so.2"@g'
211 optional enableHardening ./hardened.patch
212 # Since VirtualBox 7.0.8, VBoxSDL requires SDL2, but the build framework uses SDL1
213 ++ optionals (!headless) [
215 # No update patch disables check for update function
216 # https://bugs.launchpad.net/ubuntu/+source/virtualbox-ose/+bug/272212
218 url = "https://salsa.debian.org/pkg-virtualbox-team/virtualbox/-/raw/debian/7.0.14-dfsg-1/debian/patches/16-no-update.patch";
219 hash = "sha256-UJHpuB6QB/BbxJorlqZXUF12lgq8gbLMRHRMsbyqRpY=";
222 ++ [ ./extra_symbols.patch ]
223 # When hardening is enabled, we cannot use wrapQtApp to ensure that VirtualBoxVM sees
224 # the correct environment variables needed for Qt to work, specifically QT_PLUGIN_PATH.
225 # This is because VirtualBoxVM would detect that it is wrapped that and refuse to run,
226 # and also because it would unset QT_PLUGIN_PATH for security reasons. We work around
227 # these issues by patching the code to set QT_PLUGIN_PATH to the necessary paths,
228 # after the code that unsets it. Note that qtsvg is included so that SVG icons from
229 # the user's icon theme can be loaded.
230 ++ optional (!headless && enableHardening) (substituteAll {
231 src = ./qt-env-vars.patch;
232 qtPluginPath = "${qtbase.bin}/${qtbase.qtPluginPrefix}:${qtsvg.bin}/${qtbase.qtPluginPrefix}:${qtwayland.bin}/${qtbase.qtPluginPrefix}";
234 # While the KVM patch should not break any other behavior if --with-kvm is not specified,
235 # we don't take any chances and only apply it if people actually want to use KVM support.
236 ++ optional enableKvm (
239 # There is no updated patch for 7.0.22 yet, but the older one still applies.
240 if finalAttrs.virtualboxVersion == "7.0.22" then "7.0.20" else finalAttrs.virtualboxVersion;
243 name = "virtualbox-${finalAttrs.virtualboxVersion}-kvm-dev-${finalAttrs.kvmPatchVersion}.patch";
244 url = "https://github.com/cyberus-technology/virtualbox-kvm/releases/download/dev-${finalAttrs.kvmPatchVersion}/kvm-backend-${patchVboxVersion}-dev-${finalAttrs.kvmPatchVersion}.patch";
245 hash = finalAttrs.kvmPatchHash;
249 ./qt-dependency-paths.patch
250 # https://github.com/NixOS/nixpkgs/issues/123851
251 ./fix-audio-driver-loading.patch
255 sed -i -e 's|/sbin/ifconfig|${nettools}/bin/ifconfig|' \
256 src/VBox/HostDrivers/adpctl/VBoxNetAdpCtl.cpp
259 # first line: ugly hack, and it isn't yet clear why it's a problem
261 NIX_CFLAGS_COMPILE=$(echo "$NIX_CFLAGS_COMPILE" | sed 's,\-isystem ${lib.getDev stdenv.cc.libc}/include,,g')
263 cat >> LocalConfig.kmk <<LOCAL_CONFIG
264 VBOX_WITH_TESTCASES :=
265 VBOX_WITH_TESTSUITE :=
266 VBOX_WITH_VALIDATIONKIT :=
268 VBOX_WITH_WARNINGS_AS_ERRORS :=
271 VBOX_PATH_APP_PRIVATE_ARCH_TOP := $out/share/virtualbox
272 VBOX_PATH_APP_PRIVATE_ARCH := $out/libexec/virtualbox
273 VBOX_PATH_SHARED_LIBS := $out/libexec/virtualbox
274 VBOX_WITH_RUNPATH := $out/libexec/virtualbox
275 VBOX_PATH_APP_PRIVATE := $out/share/virtualbox
276 VBOX_PATH_APP_DOCS := $out/doc
278 VBOX_WITH_UPDATE_AGENT :=
280 ${optionalString javaBindings ''
281 VBOX_JAVA_HOME := ${jdk}
283 ${optionalString (!headless) ''
284 VBOX_WITH_VBOXSDL := 1
285 PATH_QT5_X11_EXTRAS_LIB := ${getLib qtx11extras}/lib
286 PATH_QT5_X11_EXTRAS_INC := ${getDev qtx11extras}/include
287 PATH_QT5_TOOLS_LIB := ${getLib qttools}/lib
288 PATH_QT5_TOOLS_INC := ${getDev qttools}/include
290 ${optionalString enableWebService ''
291 # fix gsoap missing zlib include and produce errors with --as-needed
292 VBOX_GSOAP_CXX_LIBS := gsoapssl++ z
294 TOOL_QT5_LRC := ${getDev qttools}/bin/lrelease
298 ${optionalString headless "--build-headless"} \
299 ${optionalString (!javaBindings) "--disable-java"} \
300 ${optionalString (!pythonBindings) "--disable-python"} \
301 ${optionalString (!pulseSupport) "--disable-pulse"} \
302 ${optionalString (!enableHardening) "--disable-hardening"} \
303 ${optionalString (!enable32bitGuests) "--disable-vmmraw"} \
304 ${optionalString enableWebService "--enable-webservice"} \
305 ${optionalString (open-watcom-bin != null) "--with-ow-dir=${open-watcom-bin}"} \
306 ${optionalString (enableKvm) "--with-kvm"} \
307 ${extraConfigureFlags} \
309 sed -e 's@PKG_CONFIG_PATH=.*@PKG_CONFIG_PATH=${libIDL}/lib/pkgconfig:${glib.dev}/lib/pkgconfig ${libIDL}/bin/libIDL-config-2@' \
311 sed -e 's@arch/x86/@@' \
313 substituteInPlace Config.kmk --replace-fail "VBOX_WITH_TESTCASES = 1" "#"
316 enableParallelBuilding = true;
320 kmk -j $NIX_BUILD_CORES BUILD_TYPE="${finalAttrs.buildType}"
324 libexec="$out/libexec/virtualbox"
325 share="${if enableHardening then "$out/share/virtualbox" else "$libexec"}"
327 # Install VirtualBox files
329 find out/linux.*/${finalAttrs.buildType}/bin -mindepth 1 -maxdepth 1 \
330 -name src -o -exec cp -avt "$libexec" {} +
334 optionalString (!headless) "VirtualBox VBoxSDL"
335 } ${optionalString enableWebService "vboxwebsrv"} VBoxManage VBoxBalloonCtrl VBoxHeadless; do
336 echo "Linking $file to /bin"
337 test -x "$libexec/$file"
338 ln -s "$libexec/$file" $out/bin/$file
341 ${optionalString (extensionPack != null) ''
343 "${fakeroot}/bin/fakeroot" "${stdenv.shell}" <<EOF
344 "$libexec/VBoxExtPackHelperApp" install \
345 --base-dir "$share/ExtensionPacks" \
346 --cert-dir "$share/ExtPackCertificates" \
347 --name "Oracle VM VirtualBox Extension Pack" \
348 --tarball "${extensionPack}" \
349 --sha-256 "${extensionPack.outputHash}"
353 ${optionalString (!headless) ''
354 # Create and fix desktop item
355 mkdir -p $out/share/applications
356 sed -i -e "s|Icon=VBox|Icon=$libexec/VBox.png|" $libexec/virtualbox.desktop
357 ln -sfv $libexec/virtualbox.desktop $out/share/applications
359 mkdir -p $out/share/icons/hicolor
360 for size in `ls -1 $libexec/icons`; do
361 mkdir -p $out/share/icons/hicolor/$size/apps
362 ln -s $libexec/icons/$size/*.png $out/share/icons/hicolor/$size/apps
365 mkdir -p "$out/share/virtualbox"
366 ln -sv $libexec/nls "$out/share/virtualbox/nls"
369 ${optionalString withModsrc ''
370 cp -rv out/linux.*/${finalAttrs.buildType}/bin/src "$modsrc"
373 mkdir -p "$out/share/virtualbox"
374 cp -rv src/VBox/Main/UnattendedTemplates "$out/share/virtualbox"
375 ln -s "${finalAttrs.virtualboxGuestAdditionsIso}" "$out/share/virtualbox/VBoxGuestAdditions.iso"
379 optionalString (!headless) ''
380 wrapQtApp $out/bin/VirtualBox
382 # If hardening is disabled, wrap the VirtualBoxVM binary instead of patching
383 # the source code (see postPatch).
384 + optionalString (!headless && !enableHardening) ''
385 wrapQtApp $out/libexec/virtualbox/VirtualBoxVM \
386 --prefix LD_LIBRARY_PATH : "${lib.makeLibraryPath [ vulkan-loader ]}"
390 inherit extensionPack; # for inclusion in profile to prevent gc
391 tests = nixosTests.virtualbox;
392 updateScript = ./update.sh;
396 description = "PC emulator";
398 VirtualBox is an x86 and AMD64/Intel64 virtualization product for enterprise and home use.
400 To install on NixOS, please use the option `virtualisation.virtualbox.host.enable = true`.
401 Please also check other options under `virtualisation.virtualbox`.
403 sourceProvenance = with lib.sourceTypes; [
407 license = lib.licenses.gpl2;
408 homepage = "https://www.virtualbox.org/";
409 maintainers = with lib.maintainers; [
414 platforms = [ "x86_64-linux" ];
415 mainProgram = "VirtualBox";