1{ lib
2, bash
3, bash-completion
4, bridge-utils
5, coreutils
6, curl
7, darwin
8, dbus
9, dnsmasq
10, docutils
11, fetchFromGitLab
12, gettext
13, glib
14, gnutls
15, iproute2
16, iptables
17, libgcrypt
18, libpcap
19, libtasn1
20, libxml2
21, libxslt
22, makeWrapper
23, meson
24, ninja
25, openssh
26, perl
27, perlPackages
28, polkit
29, pkg-config
30, pmutils
31, python3
32, readline
33, rpcsvc-proto
34, stdenv
35, substituteAll
36, xhtml1
37, yajl
38, writeScript
39, nixosTests
40
41 # Linux
42, acl ? null
43, attr ? null
44, audit ? null
45, dmidecode ? null
46, fuse3 ? null
47, kmod ? null
48, libapparmor ? null
49, libcap_ng ? null
50, libnl ? null
51, libpciaccess ? null
52, libtirpc ? null
53, lvm2 ? null
54, numactl ? null
55, numad ? null
56, parted ? null
57, systemd ? null
58, util-linux ? null
59
60 # Darwin
61, gmp
62, libiconv
63, qemu
64, Carbon
65, AppKit
66
67 # Options
68, enableCeph ? false
69, ceph
70, enableGlusterfs ? false
71, glusterfs
72, enableIscsi ? false
73, openiscsi
74, libiscsi
75, enableXen ? false
76, xen
77, enableZfs ? stdenv.isLinux
78, zfs
79}:
80
81let
82 inherit (stdenv) isDarwin isLinux isx86_64;
83 binPath = lib.makeBinPath ([
84 dnsmasq
85 ] ++ lib.optionals isLinux [
86 bridge-utils
87 dmidecode
88 dnsmasq
89 iproute2
90 iptables
91 kmod
92 lvm2
93 numactl
94 numad
95 openssh
96 pmutils
97 systemd
98 ] ++ lib.optionals enableIscsi [
99 libiscsi
100 openiscsi
101 ] ++ lib.optionals enableZfs [
102 zfs
103 ]);
104in
105
106assert enableXen -> isLinux && isx86_64;
107assert enableCeph -> isLinux;
108assert enableGlusterfs -> isLinux;
109assert enableZfs -> isLinux;
110
111# if you update, also bump <nixpkgs/pkgs/development/python-modules/libvirt/default.nix> and SysVirt in <nixpkgs/pkgs/top-level/perl-packages.nix>
112stdenv.mkDerivation rec {
113 pname = "libvirt";
114 # NOTE: You must also bump:
115 # <nixpkgs/pkgs/development/python-modules/libvirt/default.nix>
116 # SysVirt in <nixpkgs/pkgs/top-level/perl-packages.nix>
117 version = "9.9.0";
118
119 src = fetchFromGitLab {
120 owner = pname;
121 repo = pname;
122 rev = "v${version}";
123 sha256 = "sha256-8Tmn99wDkRoA+pnOjeCzHoCeR3P3MwCA1kqY6SZpkqw=";
124 fetchSubmodules = true;
125 };
126
127 patches = [
128 ./0001-meson-patch-in-an-install-prefix-for-building-on-nix.patch
129 ] ++ lib.optionals enableZfs [
130 (substituteAll {
131 src = ./0002-substitute-zfs-and-zpool-commands.patch;
132 zfs = "${zfs}/bin/zfs";
133 zpool = "${zfs}/bin/zpool";
134 })
135 ];
136
137 # remove some broken tests
138 postPatch = ''
139 sed -i '/commandtest/d' tests/meson.build
140 sed -i '/virnetsockettest/d' tests/meson.build
141 # delete only the first occurrence of this
142 sed -i '0,/qemuxml2argvtest/{/qemuxml2argvtest/d;}' tests/meson.build
143
144 '' + lib.optionalString isLinux ''
145 for binary in mount umount mkfs; do
146 substituteInPlace meson.build \
147 --replace "find_program('$binary'" "find_program('${lib.getBin util-linux}/bin/$binary'"
148 done
149
150 '' + ''
151 substituteInPlace meson.build \
152 --replace "'dbus-daemon'," "'${lib.getBin dbus}/bin/dbus-daemon',"
153 '' + lib.optionalString isLinux ''
154 sed -i 's,define PARTED "parted",define PARTED "${parted}/bin/parted",' \
155 src/storage/storage_backend_disk.c \
156 src/storage/storage_util.c
157 '' + lib.optionalString isDarwin ''
158 # Darwin doesn’t support -fsemantic-interposition, but the problem doesn’t seem to affect Mach-O.
159 # See https://gitlab.com/libvirt/libvirt/-/merge_requests/235
160 sed -i "s/not supported_cc_flags.contains('-fsemantic-interposition')/false/" meson.build
161 sed -i '/qemufirmwaretest/d' tests/meson.build
162 sed -i '/qemuvhostusertest/d' tests/meson.build
163 sed -i '/qemuxml2xmltest/d' tests/meson.build
164 '';
165
166 strictDeps = true;
167
168 nativeBuildInputs = [
169 meson
170 docutils
171 libxml2 # for xmllint
172 libxslt # for xsltproc
173 gettext
174 makeWrapper
175 ninja
176 pkg-config
177 perl
178 perlPackages.XMLXPath
179 ]
180 ++ lib.optional (!isDarwin) rpcsvc-proto
181 # NOTE: needed for rpcgen
182 ++ lib.optional isDarwin darwin.developer_cmds;
183
184 buildInputs = [
185 bash
186 bash-completion
187 curl
188 dbus
189 glib
190 gnutls
191 libgcrypt
192 libpcap
193 libtasn1
194 libxml2
195 python3
196 readline
197 xhtml1
198 yajl
199 ] ++ lib.optionals isLinux [
200 acl
201 attr
202 audit
203 fuse3
204 libapparmor
205 libcap_ng
206 libnl
207 libpciaccess
208 libtirpc
209 lvm2
210 numactl
211 numad
212 parted
213 systemd
214 util-linux
215 ] ++ lib.optionals isDarwin [
216 AppKit
217 Carbon
218 gmp
219 libiconv
220 ]
221 ++ lib.optionals enableCeph [ ceph ]
222 ++ lib.optionals enableGlusterfs [ glusterfs ]
223 ++ lib.optionals enableIscsi [ libiscsi openiscsi ]
224 ++ lib.optionals enableXen [ xen ]
225 ++ lib.optionals enableZfs [ zfs ];
226
227 preConfigure =
228 let
229 overrides = {
230 QEMU_BRIDGE_HELPER = "/run/wrappers/bin/qemu-bridge-helper";
231 QEMU_PR_HELPER = "/run/libvirt/nix-helpers/qemu-pr-helper";
232 };
233
234 patchBuilder = var: value: ''
235 sed -i meson.build -e "s|conf.set_quoted('${var}',.*|conf.set_quoted('${var}','${value}')|"
236 '';
237 in
238 ''
239 PATH="${binPath}:$PATH"
240 # the path to qemu-kvm will be stored in VM's .xml and .save files
241 # do not use "''${qemu_kvm}/bin/qemu-kvm" to avoid bound VMs to particular qemu derivations
242 substituteInPlace src/lxc/lxc_conf.c \
243 --replace 'lxc_path,' '"/run/libvirt/nix-emulators/libvirt_lxc",'
244
245 substituteInPlace build-aux/meson.build \
246 --replace "gsed" "sed" \
247 --replace "gmake" "make" \
248 --replace "ggrep" "grep"
249
250 substituteInPlace src/util/virpolkit.h \
251 --replace '"/usr/bin/pkttyagent"' '"${if isLinux then polkit.bin else "/usr"}/bin/pkttyagent"'
252
253 patchShebangs .
254 ''
255 + (lib.concatStringsSep "\n" (lib.mapAttrsToList patchBuilder overrides));
256
257 mesonAutoFeatures = "disabled";
258
259 mesonFlags =
260 let
261 cfg = option: val: "-D${option}=${val}";
262 feat = option: enable: cfg option (if enable then "enabled" else "disabled");
263 driver = name: feat "driver_${name}";
264 storage = name: feat "storage_${name}";
265 in
266 [
267 "--sysconfdir=/var/lib"
268 (cfg "install_prefix" (placeholder "out"))
269 (cfg "localstatedir" "/var")
270 (cfg "runstatedir" "/run")
271
272 (cfg "init_script" (if isDarwin then "none" else "systemd"))
273 (cfg "qemu_datadir" (lib.optionalString isDarwin "${qemu}/share/qemu"))
274
275 (feat "apparmor" isLinux)
276 (feat "attr" isLinux)
277 (feat "audit" isLinux)
278 (feat "bash_completion" true)
279 (feat "blkid" isLinux)
280 (feat "capng" isLinux)
281 (feat "curl" true)
282 (feat "docs" true)
283 (feat "expensive_tests" true)
284 (feat "firewalld" isLinux)
285 (feat "firewalld_zone" isLinux)
286 (feat "fuse" isLinux)
287 (feat "glusterfs" enableGlusterfs)
288 (feat "host_validate" true)
289 (feat "libiscsi" enableIscsi)
290 (feat "libnl" isLinux)
291 (feat "libpcap" true)
292 (feat "libssh2" true)
293 (feat "login_shell" isLinux)
294 (feat "nss" (isLinux && !stdenv.hostPlatform.isMusl))
295 (feat "numactl" isLinux)
296 (feat "numad" isLinux)
297 (feat "pciaccess" isLinux)
298 (feat "polkit" isLinux)
299 (feat "readline" true)
300 (feat "secdriver_apparmor" isLinux)
301 (feat "tests" true)
302 (feat "udev" isLinux)
303 (feat "yajl" true)
304
305 (driver "ch" isLinux)
306 (driver "esx" true)
307 (driver "interface" isLinux)
308 (driver "libvirtd" true)
309 (driver "libxl" enableXen)
310 (driver "lxc" isLinux)
311 (driver "network" true)
312 (driver "openvz" isLinux)
313 (driver "qemu" true)
314 (driver "remote" true)
315 (driver "secrets" true)
316 (driver "test" true)
317 (driver "vbox" true)
318 (driver "vmware" true)
319
320 (storage "dir" true)
321 (storage "disk" isLinux)
322 (storage "fs" isLinux)
323 (storage "gluster" enableGlusterfs)
324 (storage "iscsi" enableIscsi)
325 (storage "iscsi_direct" enableIscsi)
326 (storage "lvm" isLinux)
327 (storage "mpath" isLinux)
328 (storage "rbd" enableCeph)
329 (storage "scsi" true)
330 (storage "vstorage" isLinux)
331 (storage "zfs" enableZfs)
332 ];
333
334 doCheck = true;
335
336 postInstall = ''
337 substituteInPlace $out/bin/virt-xml-validate \
338 --replace xmllint ${libxml2}/bin/xmllint
339
340 substituteInPlace $out/libexec/libvirt-guests.sh \
341 --replace 'ON_BOOT="start"' 'ON_BOOT=''${ON_BOOT:-start}' \
342 --replace 'ON_SHUTDOWN="suspend"' 'ON_SHUTDOWN=''${ON_SHUTDOWN:-suspend}' \
343 --replace 'PARALLEL_SHUTDOWN=0' 'PARALLEL_SHUTDOWN=''${PARALLEL_SHUTDOWN:-0}' \
344 --replace "$out/bin" '${gettext}/bin' \
345 --replace 'lock/subsys' 'lock' \
346 --replace 'gettext.sh' 'gettext.sh
347 # Added in nixpkgs:
348 gettext() { "${gettext}/bin/gettext" "$@"; }
349 '
350 '' + lib.optionalString isLinux ''
351 for f in $out/lib/systemd/system/*.service ; do
352 substituteInPlace $f --replace /bin/kill ${coreutils}/bin/kill
353 done
354 rm $out/lib/systemd/system/{virtlockd,virtlogd}.*
355 wrapProgram $out/sbin/libvirtd \
356 --prefix PATH : /run/libvirt/nix-emulators:${binPath}
357 '';
358
359 passthru.updateScript = writeScript "update-libvirt" ''
360 #!/usr/bin/env nix-shell
361 #!nix-shell -i bash -p curl jq common-updater-scripts
362
363 set -eu -o pipefail
364
365 libvirtVersion=$(curl https://gitlab.com/api/v4/projects/192693/repository/tags | jq -r '.[].name|select(. | contains("rc") | not)' | head -n1 | sed "s/v//g")
366 sysvirtVersion=$(curl https://gitlab.com/api/v4/projects/192677/repository/tags | jq -r '.[].name|select(. | contains("rc") | not)' | head -n1 | sed "s/v//g")
367 update-source-version ${pname} "$libvirtVersion"
368 update-source-version python3Packages.${pname} "$libvirtVersion"
369 update-source-version perlPackages.SysVirt "$sysvirtVersion" --file="pkgs/top-level/perl-packages.nix"
370 '';
371
372 passthru.tests.libvirtd = nixosTests.libvirtd;
373
374 meta = with lib; {
375 description = "A toolkit to interact with the virtualization capabilities of recent versions of Linux and other OSes";
376 homepage = "https://libvirt.org/";
377 changelog = "https://gitlab.com/libvirt/libvirt/-/raw/v${version}/NEWS.rst";
378 license = licenses.lgpl2Plus;
379 platforms = platforms.unix;
380 maintainers = with maintainers; [ fpletz globin lovesegfault ];
381 };
382}