How to set DNS servers using NetworkManager

I’m trying to set up my NixOS machine to use Cloudflare’s DNS rather than my ISP’s (AT&T) as it corrupts files that maven fetches.

I’m kind of at a loss as to how to set this up. I’m using Pantheon as my desktop environment which provides an option to override DNS options for a specific connection. However, this is only temporary as I’ll find myself having to redo this configuration every so often.

How can I make it so that NetworkManager always uses a specified set of IPv4 and IPv6 addresses for DNS resolution?

I’ve tried setting networking.nameservers, but it seems to do nothing.

Here’s my current networking config:

networking = {
  networkmanager = {
    enable = true;
    wifi.backend = "iwd";

  hostName = "armstronglewis";
  useDHCP = false;

 # The global useDHCP flag is deprecated, therefore explicitly set to false here.
 # Per-interface useDHCP will be mandatory in the future, so this generated config
 # replicates the default behaviour.
  interfaces.enp0s31f6.useDHCP = true;
  interfaces.wlp0s20f3.useDHCP = true;

I think what you’re looking for is networking.networkmanager.insertNameservers option. It will put given ones to the first place so they’d be used. There is also “appendNameservers” for adding to the end of the list.

1 Like