{ inputs, lib, pkgs, homeActivationPackage ? null, ... }:

{
  # Pre-activate standalone home-manager if provided
  system.activationScripts.home-manager-activate = lib.mkIf (homeActivationPackage != null) {
    text = ''
      USER_HOME="/home/nomarchy"
      if [ ! -d "$USER_HOME" ]; then
        mkdir -p "$USER_HOME"
        chown nomarchy:users "$USER_HOME"
      fi
      # Run activation as the nomarchy user
      ${pkgs.sudo}/bin/sudo -u nomarchy ${homeActivationPackage}/activate
    '';
    deps = [ "users" ];
  };

  # Shared VM configuration
  virtualisation.vmVariant = {
    virtualisation.graphics = true;
    virtualisation.qemu.options = [ "-device" "virtio-vga" ];
  };

  # Improve VM resolution and guest experience
  services.spice-vdagentd.enable = true;
  services.qemuGuest.enable = true;
  services.xserver.videoDrivers = [ "qxl" "virtio" "modesetting" ];

  # Provide the flake to the VM for local rebuilding
  environment.etc."nomarchy".source = lib.mkDefault inputs.self;

  # Dummy hardware config for VM
  fileSystems."/" = lib.mkDefault { device = "/dev/disk/by-label/nixos"; };
  boot.loader.grub.device = lib.mkDefault "/dev/vda";
  boot.initrd.availableKernelModules = [ "virtio_pci" "virtio_blk" "virtio_gpu" "virtio_net" "virtio_mmio" ];
  
  # Force early KMS for Plymouth
  boot.initrd.kernelModules = [ "virtio_gpu" ];
}
