Networking: Difference between revisions
imported>Hypnosis2839 →VLAN's: rename section, also usePredictableInterfaceNames was meant for backwards compat |
imported>Hypnosis2839 clean up intro and minor formatting changes |
||
| Line 1: | Line 1: | ||
Networking config always goes in your system configuration. | |||
== Configuration == | == Configuration == | ||
| Line 62: | Line 62: | ||
== Prefix delegation with fixed DUID == | == Prefix delegation with fixed DUID == | ||
Sometimes the hosting provider manages | Sometimes the hosting provider manages IPv6 networks via a so-called ''DUID'' or ''clientid''. This snippet is required to make the network routable: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 108: | Line 108: | ||
{{ic|enp2s1}} is a normal network interface at {{ic|192.168.1.2}} with no VLAN information. | {{ic|enp2s1}} is a normal network interface at {{ic|192.168.1.2}} with no VLAN information. | ||
{{ic|enp2s0}} is the virtual LAN trunk | {{ic|enp2s0}} is the virtual LAN trunk with two tagged VLANs, {{ic|vlan100}} and {{ic|vlan101}}. | ||
{{ic|vlan100}} is in the {{ic|10.1.1.X}} network and {{ic|vlan101}} is in the {{ic|10.10.10.X}} network. | {{ic|vlan100}} is in the {{ic|10.1.1.X}} network and {{ic|vlan101}} is in the {{ic|10.10.10.X}} network. | ||
Revision as of 15:15, 16 July 2023
Networking config always goes in your system configuration.
Configuration
Static IP for network adapter
The following example configures a static IPv6 address and a default gateway for the interface ens3
networking = {
interfaces = {
ens3.ipv6.addresses = [{
address = "2a01:4f8:1c1b:16d0::";
prefixLength = 64;
}];
};
defaultGateway6 = {
address = "fe80::1";
interface = "ens3";
};
};
Hosts file
To edit /etc/hosts just add something like this to your configuration.nix:
networking.extraHosts = ''
127.0.0.2 other-localhost
10.0.0.1 server
'';
Port forwarding
In this example we're going to forward the port 80 via NAT from our external network interface ens3 to the host 10.100.0.3 on our internal interface wg0.
networking = {
firewall = {
enable = true;
allowedTCPPorts = [ 80 ];
extraCommands = "iptables -t nat -A POSTROUTING -d 10.100.0.3 -p tcp -m tcp --dport 80 -j MASQUERADE";
};
nat = {
enable = true;
internalInterfaces = [ "wg0" ];
externalInterface = "ens3";
forwardPorts = [
{
sourcePort = 80;
proto = "tcp";
destination = "10.100.0.3:80";
}
];
};
};
IPv6
Prefix delegation with fixed DUID
Sometimes the hosting provider manages IPv6 networks via a so-called DUID or clientid. This snippet is required to make the network routable:
{ config, pkgs, ... }:
let
# Get this from your hosting provider
clientid = "00:11:22:33:44:55:66:77:88:99";
interface = "enp2s0";
subnet = "56";
network = "2001:bbb:3333:1111::/${subnet}";
own_ip = "2001:bbb:3333:1111::1/${subnet}";
in {
# ... snip ...
networking.enableIPv6 = true;
networking.useDHCP = true;
networking.dhcpcd.persistent = true;
networking.dhcpcd.extraConfig = ''
clientid "${clientid}"
noipv6rs
interface ${interface}
ia_pd 1/${network} ${interface}
static ip6_address=${own_ip}
'';
environment.etc."dhcpcd.duid".text = clientid;
}
Source: gleber gist for online.net IPv6 config in NixOS
Note: Recent versions of dhcpcd move the duid file to /var/db/dcpcd/duid. For that to work, you have to replace the above environment.etc line with something like:
systemd.services.dhcpcd.preStart = ''
cp ${pkgs.writeText "duid" "<ID>"} /var/db/dhcpcd/duid
'';
VLANs
Refer to networking.vlans in the manual.
Below is a complete networking example showing two interfaces, one with VLAN trunk tagging and one without.
enp2s1 is a normal network interface at 192.168.1.2 with no VLAN information.
enp2s0 is the virtual LAN trunk with two tagged VLANs, vlan100 and vlan101.
vlan100 is in the 10.1.1.X network and vlan101 is in the 10.10.10.X network.
The hostID should be unique among your machines, as mentioned in the manual.
Complete networking section example:
networking = {
hostId = "deadb33f";
hostName = "nixos";
domain = "example.com";
dhcpcd.enable = false;
interfaces.enp2s1.ipv4.addresses = [{
address = "192.168.1.2";
prefixLength = 28;
}];
vlans = {
vlan100 = { id=100; interface="enp2s0"; };
vlan101 = { id=101; interface="enp2s0"; };
};
interfaces.vlan100.ipv4.addresses = [{
address = "10.1.1.2";
prefixLength = 24;
}];
interfaces.vlan101.ipv4.addresses = [{
address = "10.10.10.3";
prefixLength = 24;
}];
defaultGateway = "192.168.1.1";
nameservers = [ "1.1.1.1" "8.8.8.8" ];
};