1{ stdenv, fetchurl, lib, iasl, dev86, pam, libxslt, libxml2, libX11, xproto, libXext
2, libXcursor, libXmu, qt4, libIDL, SDL, libcap, zlib, libpng, glib, kernel, lvm2
3, which, alsaLib, curl, libvpx, gawk, nettools, dbus
4, xorriso, makeself, perl, pkgconfig, nukeReferences
5, javaBindings ? false, jdk ? null
6, pythonBindings ? false, python ? null
7, enableExtensionPack ? false, requireFile ? null, patchelf ? null, fakeroot ? null
8, pulseSupport ? false, libpulseaudio ? null
9, enableHardening ? false
10}:
11
12with stdenv.lib;
13
14let
15 buildType = "release";
16
17 # When changing this, update ./guest-additions and the extpack
18 # revision/hash as well. See
19 # http://download.virtualbox.org/virtualbox/${version}/SHA256SUMS
20 # for hashes.
21 version = "5.0.4";
22
23 forEachModule = action: ''
24 for mod in \
25 out/linux.*/${buildType}/bin/src/vboxdrv \
26 out/linux.*/${buildType}/bin/src/vboxpci \
27 out/linux.*/${buildType}/bin/src/vboxnetadp \
28 out/linux.*/${buildType}/bin/src/vboxnetflt
29 do
30 if [ "x$(basename "$mod")" != xvboxdrv -a ! -e "$mod/Module.symvers" ]
31 then
32 cp -v out/linux.*/${buildType}/bin/src/vboxdrv/Module.symvers \
33 "$mod/Module.symvers"
34 fi
35 INSTALL_MOD_PATH="$out" INSTALL_MOD_DIR=misc \
36 make -C "$MODULES_BUILD_DIR" DEPMOD=/do_not_use_depmod \
37 "M=\$(PWD)/$mod" BUILD_TYPE="${buildType}" ${action}
38 done
39 '';
40
41 # See https://github.com/NixOS/nixpkgs/issues/672 for details
42 extpackRevision = "102546";
43 extensionPack = requireFile rec {
44 name = "Oracle_VM_VirtualBox_Extension_Pack-${version}-${extpackRevision}.vbox-extpack";
45 # IMPORTANT: Hash must be base16 encoded because it's used as an input to
46 # VBoxExtPackHelperApp!
47 sha256 = "e4618e7847eff7c31426f4639bcd83c37bd817147081d3218f21c8e7b6bc7cfa";
48 message = ''
49 In order to use the extension pack, you need to comply with the VirtualBox Personal Use
50 and Evaluation License (PUEL) by downloading the related binaries from:
51
52 https://www.virtualbox.org/wiki/Downloads
53
54 Once you have downloaded the file, please use the following command and re-run the
55 installation:
56
57 nix-prefetch-url file://${name}
58 '';
59 };
60
61in stdenv.mkDerivation {
62 name = "virtualbox-${version}-${kernel.version}";
63
64 src = fetchurl {
65 url = "http://download.virtualbox.org/virtualbox/${version}/VirtualBox-${version}.tar.bz2";
66 sha256 = "b19e23fc8e71f38aef7c059f44e59fcbff3bb2ce85baa8de81f1629b85f68fcf";
67 };
68
69 buildInputs =
70 [ iasl dev86 libxslt libxml2 xproto libX11 libXext libXcursor qt4 libIDL SDL
71 libcap glib lvm2 python alsaLib curl libvpx pam xorriso makeself perl
72 pkgconfig which libXmu nukeReferences ]
73 ++ optional javaBindings jdk
74 ++ optional pythonBindings python
75 ++ optional pulseSupport libpulseaudio;
76
77 prePatch = ''
78 set -x
79 MODULES_BUILD_DIR=`echo ${kernel.dev}/lib/modules/*/build`
80 sed -e 's@/lib/modules/`uname -r`/build@'$MODULES_BUILD_DIR@ \
81 -e 's@MKISOFS --version@MKISOFS -version@' \
82 -e 's@PYTHONDIR=.*@PYTHONDIR=${if pythonBindings then python else ""}@' \
83 -i configure
84 ls kBuild/bin/linux.x86/k* tools/linux.x86/bin/* | xargs -n 1 patchelf --set-interpreter ${stdenv.glibc}/lib/ld-linux.so.2
85 ls kBuild/bin/linux.amd64/k* tools/linux.amd64/bin/* | xargs -n 1 patchelf --set-interpreter ${stdenv.glibc}/lib/ld-linux-x86-64.so.2
86 find . -type f -iname '*makefile*' -exec sed -i -e 's/depmod -a/:/g' {} +
87 sed -i -e '
88 s@"libdbus-1\.so\.3"@"${dbus}/lib/libdbus-1.so.3"@g
89 s@"libasound\.so\.2"@"${alsaLib}/lib/libasound.so.2"@g
90 ${optionalString pulseSupport ''
91 s@"libpulse\.so\.0"@"${libpulseaudio}/lib/libpulse.so.0"@g
92 ''}
93 ' src/VBox/Main/xml/Settings.cpp \
94 src/VBox/Devices/Audio/{alsa,pulse}_stubs.c \
95 include/VBox/dbus-calls.h
96 export USER=nix
97 set +x
98 '';
99
100 patches = optional enableHardening ./hardened.patch;
101
102 postPatch = ''
103 sed -i -e 's|/sbin/ifconfig|${nettools}/bin/ifconfig|' \
104 src/apps/adpctl/VBoxNetAdpCtl.cpp
105 '';
106
107 # first line: ugly hack, and it isn't yet clear why it's a problem
108 configurePhase = ''
109 NIX_CFLAGS_COMPILE=$(echo "$NIX_CFLAGS_COMPILE" | sed 's,\-isystem ${stdenv.cc.libc}/include,,g')
110
111 cat >> LocalConfig.kmk <<LOCAL_CONFIG
112 VBOX_WITH_TESTCASES :=
113 VBOX_WITH_TESTSUITE :=
114 VBOX_WITH_VALIDATIONKIT :=
115 VBOX_WITH_DOCS :=
116 VBOX_WITH_WARNINGS_AS_ERRORS :=
117
118 VBOX_WITH_ORIGIN :=
119 VBOX_PATH_APP_PRIVATE_ARCH_TOP := $out/share/virtualbox
120 VBOX_PATH_APP_PRIVATE_ARCH := $out/libexec/virtualbox
121 VBOX_PATH_SHARED_LIBS := $out/libexec/virtualbox
122 VBOX_WITH_RUNPATH := $out/libexec/virtualbox
123 VBOX_PATH_APP_PRIVATE := $out/share/virtualbox
124 VBOX_PATH_APP_DOCS := $out/doc
125 ${optionalString javaBindings ''
126 VBOX_JAVA_HOME := ${jdk}
127 ''}
128 LOCAL_CONFIG
129
130 ./configure --with-qt4-dir=${qt4} \
131 ${optionalString (!javaBindings) "--disable-java"} \
132 ${optionalString (!pythonBindings) "--disable-python"} \
133 ${optionalString (!pulseSupport) "--disable-pulse"} \
134 ${optionalString (!enableHardening) "--disable-hardening"} \
135 --disable-kmods --with-mkisofs=${xorriso}/bin/xorrisofs
136 sed -e 's@PKG_CONFIG_PATH=.*@PKG_CONFIG_PATH=${libIDL}/lib/pkgconfig:${glib}/lib/pkgconfig ${libIDL}/bin/libIDL-config-2@' \
137 -i AutoConfig.kmk
138 sed -e 's@arch/x86/@@' \
139 -i Config.kmk
140 substituteInPlace Config.kmk --replace "VBOX_WITH_TESTCASES = 1" "#"
141 '';
142
143 enableParallelBuilding = true;
144
145 buildPhase = ''
146 source env.sh
147 kmk BUILD_TYPE="${buildType}"
148 ${forEachModule "modules"}
149 '';
150
151 installPhase = ''
152 libexec="$out/libexec/virtualbox"
153 share="${if enableHardening then "$out/share/virtualbox" else "$libexec"}"
154
155 # Install VirtualBox files
156 mkdir -p "$libexec"
157 find out/linux.*/${buildType}/bin -mindepth 1 -maxdepth 1 \
158 -name src -o -exec cp -avt "$libexec" {} +
159
160 # Install kernel modules
161 ${forEachModule "modules_install"}
162
163 # Create wrapper script
164 mkdir -p $out/bin
165 for file in VirtualBox VBoxManage VBoxSDL VBoxBalloonCtrl VBoxBFE VBoxHeadless; do
166 ln -s "$libexec/$file" $out/bin/$file
167 done
168
169 ${optionalString enableExtensionPack ''
170 mkdir -p "$share"
171 "${fakeroot}/bin/fakeroot" "${stdenv.shell}" <<EXTHELPER
172 "$libexec/VBoxExtPackHelperApp" install \
173 --base-dir "$share/ExtensionPacks" \
174 --cert-dir "$share/ExtPackCertificates" \
175 --name "Oracle VM VirtualBox Extension Pack" \
176 --tarball "${extensionPack}" \
177 --sha-256 "${extensionPack.outputHash}"
178 EXTHELPER
179 ''}
180
181 # Create and fix desktop item
182 mkdir -p $out/share/applications
183 sed -i -e "s|Icon=VBox|Icon=$libexec/VBox.png|" $libexec/virtualbox.desktop
184 ln -sfv $libexec/virtualbox.desktop $out/share/applications
185 # Icons
186 mkdir -p $out/share/icons/hicolor
187 for size in `ls -1 $libexec/icons`; do
188 mkdir -p $out/share/icons/hicolor/$size/apps
189 ln -s $libexec/icons/$size/*.png $out/share/icons/hicolor/$size/apps
190 done
191
192 # Get rid of a reference to linux.dev.
193 nuke-refs $out/lib/modules/*/misc/*.ko
194 '';
195
196 passthru = { inherit version; /* for guest additions */ };
197
198 meta = {
199 description = "PC emulator";
200 homepage = http://www.virtualbox.org/;
201 maintainers = [ lib.maintainers.sander ];
202 platforms = lib.platforms.linux;
203 };
204}