Manage virtualbox /etc/vbox/networks.conf?

I’m using vagrant to bring up a virtualbox vm that uses hostonly networking. I get the error:

The IP address configured for the host-only network is not within the
allowed ranges. Please update the address used to be within the allowed
ranges and run the command again.

  Address: 192.168.95.101
  Ranges: 192.168.56.0/21

Valid ranges can be modified in the /etc/vbox/networks.conf file. For
more information including valid format see:

  https://www.virtualbox.org/manual/ch06.html#network_hostonly

The question is if there is already any options to manage this file. I searched and looked at nixpkgs/virtualbox-host.nix at 114bfa7d5c4e618c1eafe79cc42975389307048e · NixOS/nixpkgs · GitHub (which I’m still too newb to fully grok) and don’t see anything. Is there an easy way to extend the existing code or to just use an alternative function to manage this file in my configuration.nix?

1 Like

I’ve hacked together a solution with activationScripts

  system.activationScripts.symlinks = {
    text = ''
      source ${config.system.build.setEnvironment} # get base packages into this activationScript so e.g. which, curl etc. work

      file_manage() {
        actual_file="$1"
        local desired="$2"

        desired_file=$(mktemp)
        echo "$desired" > "$desired_file"
      

        diff "$desired_file" "$actual_file" #2>/dev/null 1>/dev/null
        exit_code=$?
        [[ $exit_code -eq 0 ]] && { 
          echo "****** OK: contents of $actual_file" 
        } || { 
          echo "$desired" > "$actual_file"
          echo "****** CHANGED: contents of $actual_file"
        }

      }

      mkdir -p /etc/vbox/ 
      file_manage "/etc/vbox/networks.conf" "* 192.168.0.0/16"

Great job on the activation script! However, files in /etc should be trivially manageable using the environment.etc option.

Interesting. This looks like it requires there to be a file managed in /nix/store some where that is then linked as the source to somewhere in etc. I’m not sure how that would be done.

I assume that the proper way to do this would be to extend the existing virtualisation.virtualbox.host to have a function?!?!? that allows you to declaratively manage this new networks.conf files.

I think it is just the poor example in the docs confusing you. The module system does the “manage the file in Nix store” part for you. Essentially, it should be enough to do:

{
  environment.etc."vbox/networks.conf".text = ''
    * 192.168.0.0/16
  '';
}

in your NixOS config. You are right that this addition should probably be contributed to the virtualbox module in NixOS.

This makes perfect sense now seeing the additional example. I tested this works!