I want to autostart a vagrant/virtualbox machine when I access a port on localhost. The vagrant machine is a development environment for web development.
Until now I have
{ pkgs, ... }: {
virtualisation.virtualbox.host.enable = true;
users.extraGroups.vboxusers.members = [ "lucc" ];
environment.systemPackages = [ pkgs.vagrant ];
systemd.user.sockets.vagrant.listenStreams = [ "8880" ];
systemd.user.services.vagrant = {
path = with pkgs; [ vagrant virtualbox curl ];
script = "vagrant up --provision --no-tty --no-color";
serviceConfig.WorkingDirectory = "/bla/bla/bla";
};
}
With the first three lines I can manage the vagrant machine manually from the shell so this is good.
The user socket also correctly triggers the service when I access http://localhost:8880, so I think this is also good.
The problem seems to be the vagrant.service, when I start it it complains that it can not find virtualbox:
...
Dec 17 11:11:21 localhost vagrant-start[574619]: ==> default: Forwarding ports...
Dec 17 11:11:21 localhost vagrant-start[574619]: default: 80 (guest) => 8880 (host) (adapter 1)
Dec 17 11:11:21 localhost vagrant-start[574619]: default: 22 (guest) => 2222 (host) (adapter 1)
Dec 17 11:11:22 localhost vagrant-start[574619]: ==> default: Booting VM...
Dec 17 11:11:25 localhost vagrant-start[574619]: There was an error while executing `VBoxManage`, a CLI used by Vagrant
Dec 17 11:11:25 localhost vagrant-start[574619]: for controlling VirtualBox. The command and stderr is shown below.
Dec 17 11:11:25 localhost vagrant-start[574619]: Command: ["startvm", "2242e924-0f52-4644-8c92-73d3cff567f8", "--type", "headless"]
Dec 17 11:11:25 localhost vagrant-start[574619]: Stderr: VBoxManage: error: The virtual machine 'vm-infra_default_1665474807647_70636' has terminated unexpectedly during startup with exit code 1 (0x1)
Dec 17 11:11:25 localhost vagrant-start[574619]: VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
Dec 17 11:11:25 localhost systemd[1718]: vagrant.service: Main process exited, code=exited, status=1/FAILURE
But when I execute the start script from the systemd unit in my shell it runs jist fine.
How can I make vagrant + virtualbox work in a systemd unit? Or alternatively how can I autostart my vagrant + virtualbox machine on a NixOS system as soon as I access the port localhost:8880?