Does anybody have working automatic-resizing in VirtualBox?

For context, I am trying to get this to work on a Windows host. I think this shouldn’t make a difference but don’t know enough about VirtualBox to know.

What I want is that I can resize the VirtualBox window and the desktop size of OS running inside the VM needs to adapt automatically.

  1. This doesn’t work with the usual
    virtualisation.virtualbox.guest.enable = true;
  2. It doesn’t work with the VirtualBox appliance for download here.
  3. Manually installing the guest additions by mounting the .iso does not work on NixOS. I don’t understand NixOS yet but I think it both wants to change things which it is not allowed to change and cannot find some binaries that it expects in default paths.

If anybody has this working I would appreciate some pointers.

1 Like

There is a bug in the VBoxClient 6.1.6 which has been fixed in 6.1.7 IIRC, though the latter is not yet in nixpkgs.

I’m waiting for the update as well, but don’t have the time to do the PR work on my own currently.

When I’m in the office I might be able to hand out a link to the VBox issue tracker.

3 Likes

Okay, the upstream issue: https://www.virtualbox.org/ticket/19496

@smn you just become unlucky that VirtualBox deprecated VBoxVGA driver in VirtualBox version 6.0.3 (I think).

With new version you need to use VMSVGA driver, unfortunately NixOS needs a fix to work with it, and here it is: nixos/virtualbox-guest: support VMSVGA graphics adapter by bachp · Pull Request #86473 · NixOS/nixpkgs · GitHub but it is hanging there, I guess it needs attention from someone who can merge it.

As a workaround you can just type sudo VBoxClient --vmsvga after starting up. I’m sure there’s a way to include the fix in configuration.nix but I didn’t spent time looking into it myself.

2 Likes

Thanks @takeda!

Since I am fairly new to NixOS: How do I apply the fix locally?

So here’s what I did:

  # TODO: remove me after https://github.com/NixOS/nixpkgs/pull/86473 is applied
  services.xserver.videoDrivers = lib.mkForce [ "vmware" "virtualbox" "modesetting" ];
  systemd.services.virtualbox-resize = {
    description = "VirtualBox Guest Screen Resizing";

    wantedBy = [ "multi-user.target" ];
    requires = [ "dev-vboxguest.device" ];
    after = [ "dev-vboxguest.device" ];

    unitConfig.ConditionVirtualization = "oracle";

    serviceConfig.ExecStart = "@${config.boot.kernelPackages.virtualboxGuestAdditions}/bin/VBoxClient -fv --vmsvga";
  };

You might also modify top of your configuration.nix file and change:

{ config, pkgs, ... }:

into:

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

The first line changes preference to use vmware driver, which is what VMSVGA is using, this will also allow you to enable 3D acceleration option in VirtualBox and make rendering smoother.

The second one is creating a service that would run VBoxClient --vmsvga this will enable screen resizing. Note though that the patch that is waiting (nixos/virtualbox-guest: support VMSVGA graphics adapter by bachp · Pull Request #86473 · NixOS/nixpkgs · GitHub) is using xrandr to change screen size, once it is merged those two might conflict with each other and you should remove this from your config.

I like this method, because it seems to work better on my machine, it also resizes the login screen. It looks like it needs to run as root though (maybe there is a way to make it run as unprivileged user). From what I read other OSes also run this as a service so perhaps this is correct approach.

4 Likes

Excellent. This works. In case anybody else is wondering: Yes, this goes in your configuration.nix and yes, the adding of the lib is required.

I’m (apparently) experiencing the same issue on MacOS, so this does indeed appear to be applicable to multiple host systems.

tadeka’s workaround does not appear to work for me, unfortunately

Actually, it does work, but only when also selecting VMSVEGA as the graphics controller in the Virtualbox settings panel of the Virtual Machine.

2 Likes

@vanschelven that’s correct, you need to use VMSVGA. The issue is that VirtualBox deprecated the old driver for Linux and wants VMSVGA to be used. This “patch” enables NixOS to use it. Anyway, the original fix had been merged, so probably in NixOS 20.09 you should remove this hack.

2 Likes

@takeda thank you for your snippet!

I’ve tried it on NixOS 23.05, and it does not seem to work for me

[mingaleg@minganix:~]$ sudo systemctl status virtualbox-resize
○ virtualbox-resize.service - VirtualBox Guest Screen Resizing
     Loaded: loaded (/etc/systemd/system/virtualbox-resize.service; enabled; preset: enabled)
     Active: inactive (dead) since Thu 2023-06-29 20:26:27 BST; 11min ago
   Duration: 42ms
    Process: 561 ExecStart=/nix/store/m3f0cn2gmc92w4zmn5hhmfz00ra15wxx-VirtualBox-GuestAdditions-7.0.8-6.1.35/bin/VBoxClient -fv --vmsvga (code=exited, status=0/SUCCESS)
   Main PID: 561 (code=exited, status=0/SUCCESS)
         IP: 0B in, 0B out
        CPU: 5ms


Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695695 main     OS Release: 6.1.35
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695698 main     OS Version: #1-NixOS SMP PREEMPT_DYNAMIC Wed Jun 21 14:01:03 UTC 2023
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695700 main     Executable: /nix/store/m3f0cn2gmc92w4zmn5hhmfz00ra15wxx-VirtualBox-GuestAdditions-7.0.8-6.1.35/bin/VBoxClient
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695700 main     Process ID: 561
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695701 main     Package type: LINUX_64BITS_GENERIC
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695706 main      VBoxClient 7.0.8 r156879 started. Verbose level = 1. Wayland environment detected: no
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.695709 main      Service: SVGA X11 display
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.698404 main      Initializing service ...
Jun 29 20:26:27 minganix VBoxClient[561]: 19:26:27.698881 main      Service is not availabe, skipping
Jun 29 20:26:27 minganix systemd[1]: virtualbox-resize.service: Deactivated successfully.

If I run the command VBoxClient -fv --vmsvga manualy in X11 enviroment, however, it works just fine; and it also does not work if I run it from TTY instead (same log messages). So, I suspect, the service should somehow be running with the current X session in mind, but I’m not sure how the service definition should be updated to facilitate that, any help would be appreciated.

I’ve only now realized this thread is 3 years old… Sorry for the necro-posting.

But yeah, I still face the same issue with only these configs set:

{
  virtualisation.virtualbox.guest = {
    enable = true;
    x11 = true;
  };
}

Should this had been already resolved, am I doing something wrong? My configuration is configuration.nix · GitHub.

I’ve also tried to run my config without proposed

{
  services.xserver.videoDrivers =...
  systemd.services.virtualbox-resize = ...
}

sections, with just

{
  virtualisation.virtualbox.guest = {
    enable = true;
    x11 = true;
  };
}

but I do not see any VBoxClient functionalities working (screen resizing, clipboard sharing, maybe something else I’ve not checked yet) with just that config as well.

I was able to make this work by making vboxclient systemd service to run in user namespace and depending (via WantedBy) on graphical-session.target. Here is the configuration which worked for me (with also a service for clipboard sharing):

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

{
  services.xserver.videoDrivers = lib.mkForce [ "vmware" "virtualbox" "modesetting" ];
  systemd.user.services = let
    vbox-client = desc: flags: {
      description = "VirtualBox Guest: ${desc}";

      wantedBy = [ "graphical-session.target" ];
      requires = [ "dev-vboxguest.device" ];
      after = [ "dev-vboxguest.device" ];

      unitConfig.ConditionVirtualization = "oracle";

      serviceConfig.ExecStart = "${config.boot.kernelPackages.virtualboxGuestAdditions}/bin/VBoxClient -fv ${flags}";
      };
  in {
    virtualbox-resize = vbox-client "Resize" "--vmsvga";
    virtualbox-clipboard = vbox-client "Clipboard" "--clipboard";
  };

  virtualisation.virtualbox.guest = {
    enable = true;
    x11 = true;
  };
}

Thank you @takeda for the original snippet!

4 Likes

just to confirm I downloaded the nix pre-built ISO image for virtualbox from here:

and then i added mingaleg’s config to the bottom of the one that comes preconfigured
so my file is like this:



{ config, pkgs, lib, ... }:
{
  imports = [ <nixpkgs/nixos/modules/installer/virtualbox-demo.nix> ];

  # Let demo build as a trusted user.
# nix.settings.trusted-users = [ "demo" ];

# Mount a VirtualBox shared folder.
# This is configurable in the VirtualBox menu at
# Machine / Settings / Shared Folders.
# fileSystems."/mnt" = {
#   fsType = "vboxsf";
#   device = "nameofdevicetomount";
#   options = [ "rw" ];
# };

# By default, the NixOS VirtualBox demo image includes SDDM and Plasma.
# If you prefer another desktop manager or display manager, you may want
# to disable the default.
# services.xserver.desktopManager.plasma5.enable = lib.mkForce false;
# services.xserver.displayManager.sddm.enable = lib.mkForce false;

# Enable GDM/GNOME by uncommenting above two lines and two lines below.
# services.xserver.displayManager.gdm.enable = true;
# services.xserver.desktopManager.gnome.enable = true;

# Set your time zone.
# time.timeZone = "Europe/Amsterdam";

# List packages installed in system profile. To search, run:
# \$ nix search wget
# environment.systemPackages = with pkgs; [
#   wget vim
# ];

# Enable the OpenSSH daemon.
# services.openssh.enable = true;

  services.xserver.videoDrivers = lib.mkForce [ "vmware" "virtualbox" "modesetting" ];
  systemd.user.services = let
    vbox-client = desc: flags: {
      description = "VirtualBox Guest: ${desc}";

      wantedBy = [ "graphical-session.target" ];
      requires = [ "dev-vboxguest.device" ];
      after = [ "dev-vboxguest.device" ];

      unitConfig.ConditionVirtualization = "oracle";

      serviceConfig.ExecStart = "${config.boot.kernelPackages.virtualboxGuestAdditions}/bin/VBoxClient -fv ${flags}";
      };
  in {
    virtualbox-resize = vbox-client "Resize" "--vmsvga";
    virtualbox-clipboard = vbox-client "Clipboard" "--clipboard";
  };

  virtualisation.virtualbox.guest = {
    enable = true;
    x11 = true;
  };

}


and the window is now resizing correctly within virtualbox, also able to play a youtube video and the sound is being passed through correctly from nix-os guest to windows host :star_struck: