{ pkgs, inputs, ... }:
pkgs.writeShellApplication {
  name = "start-vm";
  runtimeInputs = [
    pkgs.spice-gtk
    pkgs.uutils-findutils
  ];
  text = ''
    host="''${1:-}"
    clean="''${2:-no}"
    if [[ "$clean" == "clean" ]]; then
      find . -type f -name '*.qcow2' -delete
    fi
    run_vm="$(nix build --no-link --print-out-paths '${inputs.self}#nixosConfigurations."'"$host"'".config.system.build.vm' --show-trace)"

    # shellcheck disable=SC2211
    # shellcheck disable=SC2086
    $run_vm/bin/* &
    pid=$!
    trap 'kill $pid' EXIT
    sleep 2
    spicy --title "$host" --uri=spice+unix:///tmp/spice.sock
  '';
}
