forgejo-lts: 7.0.10 -> 7.0.11
[NixPkgs.git] / pkgs / development / libraries / libvirt / default.nix
blobb1e565266f8613f2728a4143abf1b71f8972ce00
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 , json_c
38 , writeScript
39 , nixosTests
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
60   # Darwin
61 , gmp
62 , libiconv
63 , qemu
64 , Carbon
65 , AppKit
67   # Options
68 , enableCeph ? false
69 , ceph
70 , enableGlusterfs ? false
71 , glusterfs
72 , enableIscsi ? false
73 , openiscsi
74 , libiscsi
75 , enableXen ? stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isx86_64
76 , xen
77 , enableZfs ? stdenv.hostPlatform.isLinux
78 , zfs
81 let
82   inherit (stdenv.hostPlatform) 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   ]);
106 assert enableXen -> isLinux && isx86_64;
107 assert enableCeph -> isLinux;
108 assert enableGlusterfs -> isLinux;
109 assert enableZfs -> isLinux;
111 # if you update, also bump <nixpkgs/pkgs/development/python-modules/libvirt/default.nix> and SysVirt in <nixpkgs/pkgs/top-level/perl-packages.nix>
112 stdenv.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 = "10.9.0";
119   src = fetchFromGitLab {
120     owner = pname;
121     repo = pname;
122     rev = "v${version}";
123     hash = "sha256-LYQYA5UIKYs+8rSNZDymmrxuTWsgmukP5Y17lGB5UQs=";
124     fetchSubmodules = true;
125   };
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   ];
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,/qemuxmlconftest/{/qemuxmlconftest/d;}' tests/meson.build
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
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 '/qemuhotplugtest/d' tests/meson.build
163     sed -i '/qemuvhostusertest/d' tests/meson.build
164     sed -i '/qemuxml2xmltest/d' tests/meson.build
165     sed -i '/domaincapstest/d' tests/meson.build
166     # virshtest frequently times out on Darwin
167     substituteInPlace tests/meson.build \
168       --replace-fail "data.get('timeout', 30)" "data.get('timeout', 120)"
169   '' + lib.optionalString enableXen ''
170     # Has various hardcoded paths that don't exist outside of a Xen dom0.
171     sed -i '/libxlxml2domconfigtest/d' tests/meson.build
172     substituteInPlace src/libxl/libxl_capabilities.h \
173      --replace-fail /usr/lib/xen ${xen}/libexec/xen
174   '';
176   strictDeps = true;
178   nativeBuildInputs = [
179     meson
180     docutils
181     libxml2 # for xmllint
182     libxslt # for xsltproc
183     gettext
184     makeWrapper
185     ninja
186     pkg-config
187     perl
188     perlPackages.XMLXPath
189   ]
190   ++ lib.optional (!isDarwin) rpcsvc-proto
191   # NOTE: needed for rpcgen
192   ++ lib.optional isDarwin darwin.developer_cmds;
194   buildInputs = [
195     bash
196     bash-completion
197     curl
198     dbus
199     glib
200     gnutls
201     libgcrypt
202     libpcap
203     libtasn1
204     libxml2
205     python3
206     readline
207     xhtml1
208     json_c
209   ] ++ lib.optionals isLinux [
210     acl
211     attr
212     audit
213     fuse3
214     libapparmor
215     libcap_ng
216     libnl
217     libpciaccess
218     libtirpc
219     lvm2
220     numactl
221     numad
222     parted
223     systemd
224     util-linux
225   ] ++ lib.optionals isDarwin [
226     AppKit
227     Carbon
228     gmp
229     libiconv
230   ]
231   ++ lib.optionals enableCeph [ ceph ]
232   ++ lib.optionals enableGlusterfs [ glusterfs ]
233   ++ lib.optionals enableIscsi [ libiscsi openiscsi ]
234   ++ lib.optionals enableXen [ xen ]
235   ++ lib.optionals enableZfs [ zfs ];
237   preConfigure =
238     let
239       overrides = {
240         QEMU_BRIDGE_HELPER = "/run/wrappers/bin/qemu-bridge-helper";
241         QEMU_PR_HELPER = "/run/libvirt/nix-helpers/qemu-pr-helper";
242       };
244       patchBuilder = var: value: ''
245         sed -i meson.build -e "s|conf.set_quoted('${var}',.*|conf.set_quoted('${var}','${value}')|"
246       '';
247     in
248     ''
249       PATH="${binPath}:$PATH"
250       # the path to qemu-kvm will be stored in VM's .xml and .save files
251       # do not use "''${qemu_kvm}/bin/qemu-kvm" to avoid bound VMs to particular qemu derivations
252       substituteInPlace src/lxc/lxc_conf.c \
253         --replace 'lxc_path,' '"/run/libvirt/nix-emulators/libvirt_lxc",'
255       substituteInPlace build-aux/meson.build \
256         --replace "gsed" "sed" \
257         --replace "gmake" "make" \
258         --replace "ggrep" "grep"
260       substituteInPlace src/util/virpolkit.h \
261         --replace '"/usr/bin/pkttyagent"' '"${if isLinux then polkit.bin else "/usr"}/bin/pkttyagent"'
263       substituteInPlace src/util/virpci.c \
264          --replace '/lib/modules' '${if isLinux then "/run/booted-system/kernel-modules" else ""}/lib/modules'
266       patchShebangs .
267     ''
268     + (lib.concatStringsSep "\n" (lib.mapAttrsToList patchBuilder overrides));
270   mesonAutoFeatures = "disabled";
272   mesonFlags =
273     let
274       cfg = option: val: "-D${option}=${val}";
275       feat = option: enable: cfg option (if enable then "enabled" else "disabled");
276       driver = name: feat "driver_${name}";
277       storage = name: feat "storage_${name}";
278     in
279     [
280       "--sysconfdir=/var/lib"
281       (cfg "install_prefix" (placeholder "out"))
282       (cfg "localstatedir" "/var")
283       (cfg "runstatedir" (if isDarwin then "/var/run" else "/run"))
284       (cfg "sshconfdir" "/etc/ssh/ssh_config.d")
286       (cfg "init_script" (if isDarwin then "none" else "systemd"))
287       (cfg "qemu_datadir" (lib.optionalString isDarwin "${qemu}/share/qemu"))
289       (feat "apparmor" isLinux)
290       (feat "attr" isLinux)
291       (feat "audit" isLinux)
292       (feat "bash_completion" true)
293       (feat "blkid" isLinux)
294       (feat "capng" isLinux)
295       (feat "curl" true)
296       (feat "docs" true)
297       (feat "expensive_tests" true)
298       (feat "firewalld" isLinux)
299       (feat "firewalld_zone" isLinux)
300       (feat "fuse" isLinux)
301       (feat "glusterfs" enableGlusterfs)
302       (feat "host_validate" true)
303       (feat "libiscsi" enableIscsi)
304       (feat "libnl" isLinux)
305       (feat "libpcap" true)
306       (feat "libssh2" true)
307       (feat "login_shell" isLinux)
308       (feat "nss" (isLinux && !stdenv.hostPlatform.isMusl))
309       (feat "numactl" isLinux)
310       (feat "numad" isLinux)
311       (feat "pciaccess" isLinux)
312       (feat "polkit" isLinux)
313       (feat "readline" true)
314       (feat "secdriver_apparmor" isLinux)
315       (feat "ssh_proxy" isLinux)
316       (feat "tests" true)
317       (feat "udev" isLinux)
318       (feat "json_c" true)
320       (driver "ch" isLinux)
321       (driver "esx" true)
322       (driver "interface" isLinux)
323       (driver "libvirtd" true)
324       (driver "libxl" enableXen)
325       (driver "lxc" isLinux)
326       (driver "network" true)
327       (driver "openvz" isLinux)
328       (driver "qemu" true)
329       (driver "remote" true)
330       (driver "secrets" true)
331       (driver "test" true)
332       (driver "vbox" true)
333       (driver "vmware" true)
335       (storage "dir" true)
336       (storage "disk" isLinux)
337       (storage "fs" isLinux)
338       (storage "gluster" enableGlusterfs)
339       (storage "iscsi" enableIscsi)
340       (storage "iscsi_direct" enableIscsi)
341       (storage "lvm" isLinux)
342       (storage "mpath" isLinux)
343       (storage "rbd" enableCeph)
344       (storage "scsi" true)
345       (storage "vstorage" isLinux)
346       (storage "zfs" enableZfs)
347     ];
349   doCheck = true;
351   postInstall = ''
352     substituteInPlace $out/bin/virt-xml-validate \
353       --replace xmllint ${libxml2}/bin/xmllint
355     substituteInPlace $out/libexec/libvirt-guests.sh \
356       --replace 'ON_BOOT="start"'       'ON_BOOT=''${ON_BOOT:-start}' \
357       --replace 'ON_SHUTDOWN="suspend"' 'ON_SHUTDOWN=''${ON_SHUTDOWN:-suspend}' \
358       --replace 'PARALLEL_SHUTDOWN=0'   'PARALLEL_SHUTDOWN=''${PARALLEL_SHUTDOWN:-0}' \
359       --replace "$out/bin"              '${gettext}/bin' \
360       --replace 'lock/subsys'           'lock' \
361       --replace 'gettext.sh'            'gettext.sh
362     # Added in nixpkgs:
363     gettext() { "${gettext}/bin/gettext" "$@"; }
364     '
365   '' + lib.optionalString isLinux ''
366     for f in $out/lib/systemd/system/*.service ; do
367       substituteInPlace $f --replace /bin/kill ${coreutils}/bin/kill
368     done
369     rm $out/lib/systemd/system/{virtlockd,virtlogd}.*
370     wrapProgram $out/sbin/libvirtd \
371       --prefix PATH : /run/libvirt/nix-emulators:${binPath}
372   '';
374   passthru.updateScript = writeScript "update-libvirt" ''
375     #!/usr/bin/env nix-shell
376     #!nix-shell -i bash -p curl jq common-updater-scripts
378     set -eu -o pipefail
380     libvirtVersion=$(curl https://gitlab.com/api/v4/projects/192693/repository/tags | jq -r '.[].name|select(. | contains("rc") | not)' | head -n1 | sed "s/v//g")
381     sysvirtVersion=$(curl https://gitlab.com/api/v4/projects/192677/repository/tags | jq -r '.[].name|select(. | contains("rc") | not)' | head -n1 | sed "s/v//g")
382     update-source-version ${pname} "$libvirtVersion"
383     update-source-version python3Packages.${pname} "$libvirtVersion"
384     update-source-version perlPackages.SysVirt "$sysvirtVersion" --file="pkgs/top-level/perl-packages.nix"
385   '';
387   passthru.tests.libvirtd = nixosTests.libvirtd;
389   meta = with lib; {
390     description = "Toolkit to interact with the virtualization capabilities of recent versions of Linux and other OSes";
391     homepage = "https://libvirt.org/";
392     changelog = "https://gitlab.com/libvirt/libvirt/-/raw/v${version}/NEWS.rst";
393     license = licenses.lgpl2Plus;
394     platforms = platforms.unix;
395     maintainers = with maintainers; [ fpletz globin lovesegfault ];
396   };