diff --git a/lib/config/custom-build-vm.nix b/lib/config/custom-build-vm.nix index b7d9198..a52f631 100644 --- a/lib/config/custom-build-vm.nix +++ b/lib/config/custom-build-vm.nix @@ -1,46 +1,66 @@ -{ config, lib, pkgs, ... }: -let - createVmRunner = - vmVariant: - pkgs.writeShellApplication { - name = "run-${config.system.name}-vm"; - text = '' - ${vmVariant.system.build.vm}/bin/run-${config.system.name}-vm - ''; +{ config, lib, options, pkgs, ... }: { + options = {}; + + config = { + # Replace native `qemu` with `remote-viewer` + system.build = + { + vm = + let + packageName = "custom-nixos-vm"; + + mergedSystem = + with options.system; + lib.mergeDefinitions + build.loc + build.type + (lib.lists.forEach + ( + builtins.filter + (item: + !(lib.path.hasPrefix ./. (/. + item.file))) + build.definitionsWithLocations) + (item: { inherit (item) file value; })); + in + lib.mkForce ( + if (mergedSystem.mergedValue.vm.name == packageName) + then + mergedSystem.mergedValue.vm + else + let + wrapped = pkgs.writeShellApplication { + name = "run-${config.system.name}-vm"; + text = '' + ${mergedSystem.mergedValue.vm}/bin/run-${config.system.name}-vm + ''; + }; + in + # Rename package to `nixos-vm` + pkgs.symlinkJoin { + name = packageName; + paths = [ wrapped ]; + }); }; -in - { - config = { - # Replace native `qemu` with `remote-viewer` - system.build = - with { - inherit (config.virtualisation) - vmVariantWithBootLoader - ; - }; - { - vmWithBootLoader = createVmRunner vmVariantWithBootLoader; - }; - virtualisation = - let vmConfig = { - boot.loader.efi.efiSysMountPoint = lib.mkForce "/boot"; + virtualisation = + let vmConfig = { + boot.loader.efi.efiSysMountPoint = lib.mkForce "/boot"; - virtualisation = { - qemu.options = [ - "-display sdl" - ]; + virtualisation = { + qemu.options = [ + "-display sdl" + ]; - sharedDirectories = { - hostKeys = { - source = "/etc/ssh"; - target = "/etc/ssh"; - }; + sharedDirectories = { + hostKeys = { + source = "/etc/ssh"; + target = "/etc/ssh"; }; }; }; - in { - vmVariantWithBootLoader = vmConfig; - }; - }; - } + }; + in { + vmVariantWithBootLoader = vmConfig; + }; + }; +}