{
  description = "QVM - Quick development VMs for AI-assisted workflows";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs =
    {
      self,
      nixpkgs,
      flake-utils,
    }:
    {
      nixosModules.default =
        {
          config,
          lib,
          pkgs,
          ...
        }:
        let
          cfg = config.programs.qvm;
        in
        {
          options.programs.qvm = {
            memory = lib.mkOption {
              type = lib.types.str;
              default = "8G";
              description = "Amount of memory to allocate to the VM (e.g., '8G', '16G')";
            };

            cpus = lib.mkOption {
              type = lib.types.int;
              default = 4;
              description = "Number of CPU cores to allocate to the VM";
            };
          };

          config = {
            environment.systemPackages = [ self.packages.${pkgs.system}.qvm ];

            environment.etc."xdg/qvm/qvm.conf".text = ''
              QVM_MEMORY="${cfg.memory}"
              QVM_CPUS="${toString cfg.cpus}"
            '';
          };
        };
    }
    // flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = import nixpkgs { inherit system; };

        qvm = pkgs.buildGoModule {
          pname = "qvm";
          version = "0.1.0";

          # NOTE: In a flake, only git-tracked files are included by default.
          # The Go source files must be committed to git for this build to work.
          # For development, use: go build ./cmd/qvm
          src = ./.;

          vendorHash = "sha256-d6Z32nPDawwFqhKfVw/QwHUuDuMuTdQdHApmxcXzFng=";

          subPackages = [ "cmd/qvm" ];

          nativeBuildInputs = with pkgs; [
            makeWrapper
          ];

          postInstall = ''
            # Install default VM flake template
            if [ -d "$src/flake/default-vm" ]; then
              mkdir -p $out/share/qvm
              cp -r $src/flake/default-vm $out/share/qvm/default-vm
            fi

            # Wrap binary with PATH containing required dependencies
            wrapProgram $out/bin/qvm \
              --prefix PATH : ${
                pkgs.lib.makeBinPath [
                  pkgs.qemu
                  pkgs.openssh
                  pkgs.jq
                  pkgs.nix
                  pkgs.netcat-gnu
                  pkgs.sshpass
                ]
              }
          '';

          meta = with pkgs.lib; {
            description = "Quick development VMs for AI-assisted workflows";
            homepage = "https://github.com/josh/qvm";
            license = licenses.mit;
            maintainers = [ ];
            platforms = platforms.linux;
            mainProgram = "qvm";
          };
        };

      in
      {
        packages = {
          inherit qvm;
          default = qvm;
        };

        apps = {
          qvm = {
            type = "app";
            program = "${qvm}/bin/qvm";
          };
          default = {
            type = "app";
            program = "${qvm}/bin/qvm";
          };
        };

        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            # Runtime dependencies for development
            qemu
            openssh
            jq
            nix
            netcat-gnu
            sshpass

            # Development tools
            go
            gopls
            gotools
          ];

          shellHook = ''
            echo "QVM development environment (Go)"
            echo "Build: go build ./cmd/qvm"
            echo "Run: ./qvm status"
          '';
        };
      }
    );
}
