{ pkgs ? import {}, system ? builtins.currentSystem, nixvirt, }: let inherit (nixvirt) lib; vm = import ./lib/vm.nix { inherit pkgs nixvirt; }; osdb = import ./db.nix { inherit pkgs system; }; volumeFn = { name, size }: nixvirt.lib.volume.writeXML { name = name + ".qcow2"; capacity = { count = size; unit = "GB"; }; target = { format.type = "qcow2"; }; }; mkVM = { name, uuid, arch, isos, }: rec { base = { inherit name uuid arch; volume.index = 0; volume.bus = "sata"; volume.file = "/var/lib/libvirt/images/${name}.qcow2"; }; idxToDev = i: builtins.substring i 1 "abcdefg"; volume = volumeFn { size = 16; inherit name; }; beforeInstall = isos.beforeInstall; # Required attribute. afterInstall = if builtins.hasAttr "afterInstall" isos then isos.afterInstall else []; beforeBoot = if builtins.hasAttr "beforeBoot" isos then isos.beforeBoot else []; afterBoot = if builtins.hasAttr "afterBoot" isos then isos.afterBoot else []; beforeInstallDrv = lib.domain.writeXML (vm.genericVM (base // { cdroms = map (x: { inherit (x) index file; bus = "sata"; dev = "sd" + idxToDev x.index; }) (beforeInstall); })); afterInstallDrv = lib.domain.writeXML (vm.genericVM (base // { cdroms = map (x: { inherit (x) index file; bus = "sata"; dev = "sd" + idxToDev x.index; }) (afterInstall); })); beforeBootDrv = lib.domain.writeXML (vm.genericVM (base // { cdroms = map (x: { inherit (x) index file; bus = "sata"; dev = "sd" + idxToDev x.index; }) (beforeBoot); })); afterBootDrv = lib.domain.writeXML (vm.genericVM (base // { cdroms = map (x: { inherit (x) index file; bus = "sata"; dev = "sd" + idxToDev x.index; }) (afterBoot); })); }; in { inherit osdb; mkFlake = config: let vm = mkVM { inherit (config) name uuid; beforeInstall = [ { index = 1; file = "${osdb.${config.os.name}.${config.os.version}.${config.os.edition}.${config.os.arch}.installer}/out.iso"; } { index = 2; file = "${osdb.${config.os.name}.${config.os.version}.${config.os.edition}.${config.os.arch}.unattend}/out.iso"; } ]; }; in { packages.volume = vm.volume; packages.beforeInstall = vm.beforeInstallDrv; packages.afterInstall = vm.afterInstallDrv; packages.beforeBoot = vm.beforeBootDrv; packages.afterBoot = vm.afterBootDrv; }; }