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.
This doesn’t work with the usual virtualisation.virtualbox.guest.enable = true;
It doesn’t work with the VirtualBox appliance for download here.
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.
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.
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.
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.
@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.
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.
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):
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
@mingaleg 's solution works great! THANK YOU for this!!!
Isn’t it supposed to work out of the box (as per other comments that this is fixed already)? I am wondering if we did something else wrong or missed, and in case this workaround is not necessary?