diff --git a/nixos/modules/virtualisation/libvirtd.nix b/nixos/modules/virtualisation/libvirtd.nix index 7c95405ed318..4dff564c269e 100644 --- a/nixos/modules/virtualisation/libvirtd.nix +++ b/nixos/modules/virtualisation/libvirtd.nix @@ -129,6 +129,60 @@ let }; }; }; + + hooksModule = types.submodule { + options = { + daemon = mkOption { + type = types.attrsOf types.path; + default = { }; + description = lib.mdDoc '' + Hooks that will be placed under /var/lib/libvirt/hooks/daemon.d/ + and called for daemon start/shutdown/SIGHUP events. + Please see https://libvirt.org/hooks.html for documentation. + ''; + }; + + qemu = mkOption { + type = types.attrsOf types.path; + default = { }; + description = lib.mdDoc '' + Hooks that will be placed under /var/lib/libvirt/hooks/qemu.d/ + and called for qemu domains begin/end/migrate events. + Please see https://libvirt.org/hooks.html for documentation. + ''; + }; + + lxc = mkOption { + type = types.attrsOf types.path; + default = { }; + description = lib.mdDoc '' + Hooks that will be placed under /var/lib/libvirt/hooks/lxc.d/ + and called for lxc domains begin/end events. + Please see https://libvirt.org/hooks.html for documentation. + ''; + }; + + libxl = mkOption { + type = types.attrsOf types.path; + default = { }; + description = lib.mdDoc '' + Hooks that will be placed under /var/lib/libvirt/hooks/libxl.d/ + and called for libxl-handled xen domains begin/end events. + Please see https://libvirt.org/hooks.html for documentation. + ''; + }; + + network = mkOption { + type = types.attrsOf types.path; + default = { }; + description = lib.mdDoc '' + Hooks that will be placed under /var/lib/libvirt/hooks/lxc.d/ + and called for networks begin/end events. + Please see https://libvirt.org/hooks.html for documentation. + ''; + }; + }; + }; in { @@ -246,6 +300,14 @@ in QEMU related options. ''; }; + + hooks = mkOption { + type = hooksModule; + default = { }; + description = lib.mdDoc '' + Hooks related options. + ''; + }; }; @@ -335,6 +397,15 @@ in ln -s --force ${ovmfpackage}/FV/AAVMF_VARS.fd /run/${dirName}/nix-ovmf/ ln -s --force ${ovmfpackage}/FV/OVMF_VARS.fd /run/${dirName}/nix-ovmf/ '')} + + # Symlink hooks to /var/lib/libvirt + ${concatStringsSep "\n" (map (driver: + '' + mkdir -p /var/lib/${dirName}/hooks/${driver}.d + rm -rf /var/lib/${dirName}/hooks/${driver}.d/* + ${concatStringsSep "\n" (mapAttrsToList (name: value: + "ln -s --force ${value} /var/lib/${dirName}/hooks/${driver}.d/${name}") cfg.hooks.${driver})} + '') (attrNames cfg.hooks))} ''; serviceConfig = { diff --git a/nixos/tests/libvirtd.nix b/nixos/tests/libvirtd.nix index b6e604075997..41d06cc9643f 100644 --- a/nixos/tests/libvirtd.nix +++ b/nixos/tests/libvirtd.nix @@ -11,6 +11,9 @@ import ./make-test-python.nix ({ pkgs, ... }: { memorySize = 2048; libvirtd.enable = true; + libvirtd.hooks.qemu.is_working = "${pkgs.writeShellScript "testHook.sh" '' + touch /tmp/qemu_hook_is_working + ''}"; }; boot.supportedFilesystems = [ "zfs" ]; networking.hostId = "deadbeef"; # needed for zfs @@ -57,5 +60,9 @@ import ./make-test-python.nix ({ pkgs, ... }: { virthost.shutdown() virthost.wait_for_unit("multi-user.target") virthost.wait_until_succeeds("ping -c 1 nixos") + + with subtest("test if hooks are linked and run"): + virthost.succeed("ls /var/lib/libvirt/hooks/qemu.d/is_working") + virthost.succeed("ls /tmp/qemu_hook_is_working") ''; })