Qemu/kvm copy & paste

So I am very new to NixOS as of today and testing it out in a VM in KVM on my Fedora host.
I am trying to get the copy/paste feature working in the VM but, so far, I have been unsuccessful. I have added the following packages to my config:

pkgs.spice
pkgs.spice-vdagent

And added the following services to my config:

services.spice-vdagentd.enable = true;
services.qemuGuest.enable = true;

Am I missing something?
If need be, I can post my complete config.

Also, for my understanding, after I make these changes and run sudo nixos-rebuild switch is there a need to reboot the machine to get the new config to work?
Thanks in advance from a complete noobie.

1 Like

You figured anything out here?:wink:

It’s quite tricky to set up SPICE correctly. First you need to add some options to QEMU:

  virtualisation.qemu.options = [
    "-vga qxl"
    "-spice port=5924,disable-ticketing=on"
    "-device virtio-serial -chardev spicevmc,id=vdagent,debug=0,name=vdagent"
    "-device virtserialport,chardev=vdagent,name=com.redhat.spice.0"
  ];

Then you have to enable the guest SPICE system-wide daemon

services.spice-vdagentd.enable = true

and, crucially, you need to make sure the per-session agent is started as part of your graphical session. For the latter, if you’re running a DE that implements the XDG autostart spec you’re done, otherwise you need to run spice-vdagent manually (after the X server, in your login session).

Finally, you start the VM as usual but no window will open, instead to connect to the VM you need a SPICE client, like spicy (pkgs.spice-gtk) and specify localhost:5924 (port specified in the qemu.options) as host.

After all of this you get:

  • automatic display resizing that follows the window size
  • shared clipboard
  • client mouse mode
1 Like

It doesn’t exist

https://search.nixos.org/options?query=virtualisation.qemu.options

When I use “sudo nixos-rebuild switch”, It gives me:

error: The option "virtualisation.qemu" does not exist.

Post your config.

(Post must be at least 20 characters.)

You need to import the qemu-vm.nix module:

  imports = [ <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix> ];

After adding imports = [ <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix> ]; , I got thrown into emergency mode, when rebooting I get

mounting nix-store on /nix/.ro-store...
[    1.560235] 9pnet_virtio: no channels available for device nix-store
mount: mounting nix-store on /mnt-root/nix/.ro-store failed No such file or directory

configuration.nix:

{ config, lib, pkgs, ... }:

let
  unstableTarball =
    fetchTarball
      https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz;
in
{
  imports = [
    ./hardware-configuration.nix
    <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
  ];

  nixpkgs.config = {
    packageOverrides = pkgs: {
      unstable = import unstableTarball {
        config = config.nixpkgs.config;
      };
    };
  };

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  console = {
    font = "Lat2-Terminus16";
    useXkbConfig = true; # use xkb.options in tty.
  };

  services.xserver.enable = true;

  # Xmonad
  services.xserver.windowManager.xmonad = {
    enable = true;
    enableContribAndExtras = true;
  };

  # Disable default
  programs.nano.enable = false;
  services.xserver.excludePackages = with pkgs; [xterm];

  # Shell
  programs.zsh.enable = true;
  users.defaultUserShell = pkgs.zsh;

  # Configure keymap in X11
  services.xserver = {
    xkb.layout = "us";
    autoRepeatDelay = 200;
    autoRepeatInterval = 20;
  };

  users.users.test = {
    isNormalUser = true;
    extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
    packages = with pkgs; [
    ];
  };

  environment.systemPackages = with pkgs; [
    neovim
    wget
    zsh
    git
    gnumake
    python3
    unzip
    lua
    gcc
    nodePackages.pyright
    alacritty
    dmenu
    librewolf
    xsel
    xclip
  ];

  services.openssh.enable = true;
  system.stateVersion = "23.11"; # Did you read the comment?

}

That suggests adding virtualisation.mountHostNixStore = false;.

I added it, now I get:

mounting shared on /tmp/shared...
[    1.408774] 9pnet_virtio: no channels available for device shared
mount: mounting shared on /mnt-root/tmp/shared failed: No such file or directory

Huh. Looks like the qemu-vm.nix module just sets up a bunch of shared directories even though the documented default for virtualisation.sharedDirectories is { }.

Try virtualisation.sharedDirectories = lib.mkForce { };?

It works now when I use spice-vdagent, thanks.
But the automatic display resizing doesn’t work unfortunately, and I’m stuck with a 1024 x 768 resolution.