Actually having internet access in NixOS test

So I’m doing a little NixOS test and I’d like it to have internet connection (I know this is not a good practice but this is just for testing my way around). I’ve read here that you just have to disable the nix sandbox. So I put

nix.useSandbox = false;

However, this is not doing it, my simple command

      [status, out] = machine.execute("ping -c1 google.com")
      print(out)

Is giving me

machine # ping: google.com: Name or service not known

Am I missing something?


As an aside, It may be wrong to hijack my own question like this but I feel it’s wronger to open a new one just for this: How would I use shell_interact() when launching the test from a flake?

I run my test from a flake output as nix build .#nixosTests.test -L but then a shell_interact() gives me

machine: Terminal is ready (there is no prompt):

Any hint for this one is welcome since I’m still stuck and have no idea what to try next. Here’s a very small code sample that illustrates my problem:

{ pkgs,  ... }:

pkgs.nixosTest {
  name = "internetTest";

  machine = { pkgs, ... }: {
    nix.useSandbox = false;
  };

  testScript = ''
      machine.wait_for_unit("default.target")

      machine.succeed("ping -c1 142.250.184.174") # the ip I got from "ping google.com", in case it's something related to DNS
    '';
}

Actually, writing this last one I solved it. Turns out I had to put nix.useSandbox = false in my own config, not in the VM. Sorry for the spam

You can also do nix-build --option sandbox false to avoid a system-wide change.

1 Like

@haf how did your config end up differing from this? It’s unclear to me where to put the nix.useSandbox = false setting.

I’ve long ditched that configuration, but IIRC the sandbox is actually a configuration of your host nix, not the virtual machine launched for the test. So nix.useSandbox = false should go to your system-wide nix configuration, that is:

  • /etc/nixos/configuration.nix for NixOS
  • nix.conf otherwise (though I think the option is just sandbox there, but I only use NixOS)