2009-10-12 17:27:57 +00:00
|
|
|
|
{ config, pkgs, ... }:
|
2009-03-06 12:27:38 +00:00
|
|
|
|
|
2009-10-12 17:27:57 +00:00
|
|
|
|
with pkgs.lib;
|
2007-02-12 16:00:55 +00:00
|
|
|
|
|
2009-10-12 17:27:57 +00:00
|
|
|
|
let
|
2009-05-28 16:03:48 +00:00
|
|
|
|
|
2009-07-16 17:18:54 +00:00
|
|
|
|
cfg = config.networking;
|
2012-08-29 20:15:04 +00:00
|
|
|
|
hasVirtuals = any (i: i.virtual) cfg.interfaces;
|
2009-07-16 17:18:54 +00:00
|
|
|
|
|
2011-09-14 18:20:50 +00:00
|
|
|
|
in
|
2009-07-16 17:18:54 +00:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
###### interface
|
2009-05-28 16:03:48 +00:00
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
networking.hostName = mkOption {
|
|
|
|
|
default = "nixos";
|
2009-07-16 17:18:54 +00:00
|
|
|
|
description = ''
|
2009-05-28 16:03:48 +00:00
|
|
|
|
The name of the machine. Leave it empty if you want to obtain
|
|
|
|
|
it from a DHCP server (if using DHCP).
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2011-02-19 17:21:29 +00:00
|
|
|
|
networking.enableIPv6 = mkOption {
|
|
|
|
|
default = true;
|
2009-07-16 17:18:54 +00:00
|
|
|
|
description = ''
|
2011-02-19 17:21:29 +00:00
|
|
|
|
Whether to enable support for IPv6.
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.defaultGateway = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
example = "131.211.84.1";
|
2009-07-16 17:18:54 +00:00
|
|
|
|
description = ''
|
2009-05-28 16:03:48 +00:00
|
|
|
|
The default gateway. It can be left empty if it is auto-detected through DHCP.
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.nameservers = mkOption {
|
|
|
|
|
default = [];
|
|
|
|
|
example = ["130.161.158.4" "130.161.33.17"];
|
2009-07-16 17:18:54 +00:00
|
|
|
|
description = ''
|
2009-05-28 16:03:48 +00:00
|
|
|
|
The list of nameservers. It can be left empty if it is auto-detected through DHCP.
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.domain = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
example = "home";
|
2009-07-16 17:18:54 +00:00
|
|
|
|
description = ''
|
2009-05-28 16:03:48 +00:00
|
|
|
|
The domain. It can be left empty if it is auto-detected through DHCP.
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.localCommands = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
example = "text=anything; echo You can put $text here.";
|
2009-07-16 17:18:54 +00:00
|
|
|
|
description = ''
|
2009-05-28 16:03:48 +00:00
|
|
|
|
Shell commands to be executed at the end of the
|
|
|
|
|
<literal>network-interfaces</literal> Upstart job. Note that if
|
|
|
|
|
you are using DHCP to obtain the network configuration,
|
|
|
|
|
interfaces may not be fully configured yet.
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.interfaces = mkOption {
|
|
|
|
|
default = [];
|
|
|
|
|
example = [
|
|
|
|
|
{ name = "eth0";
|
|
|
|
|
ipAddress = "131.211.84.78";
|
|
|
|
|
subnetMask = "255.255.255.128";
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
description = ''
|
|
|
|
|
The configuration for each network interface. If
|
|
|
|
|
<option>networking.useDHCP</option> is true, then every
|
|
|
|
|
interface not listed here will be configured using DHCP.
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
type = types.list types.optionSet;
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
|
example = "eth0";
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
Name of the interface.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ipAddress = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
example = "10.0.0.1";
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
IP address of the interface. Leave empty to configure the
|
|
|
|
|
interface using DHCP.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
subnetMask = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
example = "255.255.255.0";
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
Subnet mask of the interface. Leave empty to use the
|
|
|
|
|
default subnet mask.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2010-11-24 22:58:48 +00:00
|
|
|
|
macAddress = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
example = "00:11:22:33:44:55";
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
MAC address of the interface. Leave empty to use the default.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-29 20:15:04 +00:00
|
|
|
|
virtual = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
|
|
|
|
Whether this interface is virtual and should be created by tunctl.
|
|
|
|
|
This is mainly useful for creating bridges between a host a virtual
|
|
|
|
|
network such as VPN or a virtual machine.
|
|
|
|
|
|
|
|
|
|
Defaults to tap device, unless interface contains "tun" in its name.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualOwner = mkOption {
|
|
|
|
|
default = "root";
|
|
|
|
|
type = types.uniq types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
In case of a virtual device, the user who owns it.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-29 20:17:40 +00:00
|
|
|
|
proxyARP = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
|
|
|
|
Turn on proxy_arp for this device (and proxy_ndp for ipv6).
|
|
|
|
|
This is mainly useful for creating pseudo-bridges between a real
|
|
|
|
|
interface and a virtual network such as VPN or a virtual machine for
|
|
|
|
|
interfaces that don't support real bridging (most wlan interfaces).
|
|
|
|
|
As ARP proxying acts slightly above the link-layer, below-ip traffic
|
|
|
|
|
isn't bridged, so things like DHCP won't work. The advantage above
|
|
|
|
|
using NAT lies in the fact that no IP addresses are shared, so all
|
|
|
|
|
hosts are reachable/routeable.
|
|
|
|
|
|
|
|
|
|
WARNING: turns on ip-routing, so if you have multiple interfaces, you
|
|
|
|
|
should think of the consequence and setup firewall rules to limit this.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2009-07-16 17:18:54 +00:00
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2010-05-21 14:12:03 +00:00
|
|
|
|
networking.ifaces = mkOption {
|
|
|
|
|
default = listToAttrs
|
|
|
|
|
(map (iface: { name = iface.name; value = iface; }) config.networking.interfaces);
|
|
|
|
|
internal = true;
|
|
|
|
|
description = ''
|
|
|
|
|
The network interfaces in <option>networking.interfaces</option>
|
|
|
|
|
as an attribute set keyed on the interface name.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2011-03-15 15:13:48 +00:00
|
|
|
|
networking.bridges = mkOption {
|
|
|
|
|
default = { };
|
|
|
|
|
example =
|
|
|
|
|
{ br0.interfaces = [ "eth0" "eth1" ];
|
|
|
|
|
br1.interfaces = [ "eth2" "wlan0" ];
|
|
|
|
|
};
|
|
|
|
|
description =
|
|
|
|
|
''
|
|
|
|
|
This option allows you to define Ethernet bridge devices
|
|
|
|
|
that connect physical networks together. The value of this
|
|
|
|
|
option is an attribute set. Each attribute specifies a
|
|
|
|
|
bridge, with the attribute name specifying the name of the
|
|
|
|
|
bridge's network interface.
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
type = types.attrsOf types.optionSet;
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
interfaces = mkOption {
|
|
|
|
|
example = [ "eth0" "eth1" ];
|
|
|
|
|
type = types.listOf types.string;
|
|
|
|
|
description =
|
|
|
|
|
"The physical network interfaces connected by the bridge.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2011-03-15 15:13:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-20 14:29:21 +00:00
|
|
|
|
networking.useDHCP = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
merge = mergeEnableOption;
|
|
|
|
|
description = ''
|
|
|
|
|
Whether to use DHCP to obtain an IP adress and other
|
|
|
|
|
configuration for all network interfaces that are not manually
|
|
|
|
|
configured.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2009-05-28 16:03:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-07-16 17:18:54 +00:00
|
|
|
|
###### implementation
|
2009-05-28 16:03:48 +00:00
|
|
|
|
|
2009-07-16 17:18:54 +00:00
|
|
|
|
config = {
|
2009-03-06 12:27:38 +00:00
|
|
|
|
|
2012-08-29 20:15:04 +00:00
|
|
|
|
boot.kernelModules = optional cfg.enableIPv6 "ipv6" ++ optional hasVirtuals "tun";
|
2011-02-19 17:21:29 +00:00
|
|
|
|
|
2009-09-29 15:43:52 +00:00
|
|
|
|
environment.systemPackages =
|
|
|
|
|
[ pkgs.host
|
|
|
|
|
pkgs.iproute
|
2010-06-04 14:00:56 +00:00
|
|
|
|
pkgs.iputils
|
2009-09-29 15:43:52 +00:00
|
|
|
|
pkgs.nettools
|
|
|
|
|
pkgs.wirelesstools
|
2010-04-21 11:37:52 +00:00
|
|
|
|
pkgs.rfkill
|
2012-02-20 00:00:50 +00:00
|
|
|
|
pkgs.openresolv
|
2011-09-14 18:20:50 +00:00
|
|
|
|
]
|
2011-03-24 16:23:28 +00:00
|
|
|
|
++ optional (cfg.bridges != {}) pkgs.bridge_utils
|
2012-08-29 20:15:04 +00:00
|
|
|
|
++ optional hasVirtuals pkgs.tunctl
|
2011-03-24 16:23:28 +00:00
|
|
|
|
++ optional cfg.enableIPv6 pkgs.ndisc6;
|
2010-06-02 21:10:48 +00:00
|
|
|
|
|
|
|
|
|
security.setuidPrograms = [ "ping" "ping6" ];
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2012-08-15 19:38:52 +00:00
|
|
|
|
jobs."network-interfaces" =
|
|
|
|
|
{ description = "Static Network Interfaces";
|
2007-11-23 17:12:37 +00:00
|
|
|
|
|
2012-08-15 19:38:52 +00:00
|
|
|
|
after = [ "systemd-udev-settle.service" ];
|
|
|
|
|
before = [ "network.target" ];
|
|
|
|
|
wantedBy = [ "network.target" ];
|
2007-02-12 16:00:55 +00:00
|
|
|
|
|
2011-03-11 13:57:48 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
2011-03-09 12:28:44 +00:00
|
|
|
|
|
2009-07-16 17:18:54 +00:00
|
|
|
|
preStart =
|
|
|
|
|
''
|
2011-04-01 15:05:42 +00:00
|
|
|
|
set +e # continue in case of errors
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2012-02-20 00:00:50 +00:00
|
|
|
|
# Set the static DNS configuration, if given.
|
|
|
|
|
cat | ${pkgs.openresolv}/sbin/resolvconf -a static <<EOF
|
|
|
|
|
${optionalString (cfg.nameservers != [] && cfg.domain != "") ''
|
|
|
|
|
domain ${cfg.domain}
|
|
|
|
|
''}
|
|
|
|
|
${flip concatMapStrings cfg.nameservers (ns: ''
|
|
|
|
|
nameserver ${ns}
|
|
|
|
|
'')}
|
|
|
|
|
EOF
|
2009-07-16 17:18:54 +00:00
|
|
|
|
|
|
|
|
|
# Set the default gateway.
|
2011-03-11 14:50:11 +00:00
|
|
|
|
${optionalString (cfg.defaultGateway != "") ''
|
2012-10-10 21:55:42 +00:00
|
|
|
|
ip route add default via "${cfg.defaultGateway}"
|
2011-03-11 14:50:11 +00:00
|
|
|
|
''}
|
2009-07-16 17:18:54 +00:00
|
|
|
|
|
2012-10-10 21:55:42 +00:00
|
|
|
|
# Turn on forwarding if any interface has enabled proxy_arp.
|
2012-08-29 20:17:40 +00:00
|
|
|
|
${optionalString (any (i: i.proxyARP) cfg.interfaces) ''
|
|
|
|
|
echo 1 > /proc/sys/net/ipv4/ip_forward
|
|
|
|
|
''}
|
|
|
|
|
|
2009-07-16 17:18:54 +00:00
|
|
|
|
# Run any user-specified commands.
|
2011-04-01 15:05:42 +00:00
|
|
|
|
${pkgs.stdenv.shell} ${pkgs.writeText "local-net-cmds" cfg.localCommands}
|
2009-07-16 17:18:54 +00:00
|
|
|
|
'';
|
2010-09-13 15:41:38 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-10-10 21:55:42 +00:00
|
|
|
|
boot.systemd.services =
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
# For each interface <foo>, create a job ‘<foo>-cfg.service"
|
|
|
|
|
# that performs static configuration. It has a "wants"
|
|
|
|
|
# dependency on ‘<foo>.service’, which is supposed to create
|
|
|
|
|
# the interface and need not exist (i.e. for hardware
|
|
|
|
|
# interfaces). It has a binds-to dependency on the actual
|
|
|
|
|
# network device, so it only gets started after the interface
|
|
|
|
|
# has appeared, and it's stopped when the interface
|
|
|
|
|
# disappears.
|
|
|
|
|
configureInterface = i: nameValuePair "${i.name}-cfg"
|
|
|
|
|
{ description = "Configuration of ${i.name}";
|
|
|
|
|
wantedBy = [ "network.target" ];
|
|
|
|
|
wants = [ "${i.name}.service" ];
|
|
|
|
|
bindsTo = [ "sys-subsystem-net-devices-${i.name}.device" ];
|
|
|
|
|
after = [ "${i.name}.service" "sys-subsystem-net-devices-${i.name}.device" ];
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
script =
|
|
|
|
|
''
|
|
|
|
|
echo "bringing up interface..."
|
|
|
|
|
ip link set "${i.name}" up
|
|
|
|
|
''
|
|
|
|
|
+ optionalString (i.macAddress != "")
|
|
|
|
|
''
|
|
|
|
|
echo "setting MAC address to ${i.macAddress}..."
|
|
|
|
|
ip link set "${i.name}" address "${i.macAddress}"
|
|
|
|
|
''
|
|
|
|
|
+ optionalString (i.ipAddress != "")
|
|
|
|
|
''
|
|
|
|
|
echo "configuring interface..."
|
|
|
|
|
ip addr flush dev "${i.name}"
|
|
|
|
|
ip addr add "${i.ipAddress}""${optionalString (i.subnetMask != "") ("/" + i.subnetMask)}" \
|
|
|
|
|
dev "${i.name}"
|
|
|
|
|
${config.system.build.systemd}/bin/systemctl start ip-up.target
|
|
|
|
|
''
|
|
|
|
|
+ optionalString i.proxyARP
|
|
|
|
|
''
|
|
|
|
|
echo 1 > /proc/sys/net/ipv4/conf/${i.name}/proxy_arp
|
|
|
|
|
''
|
|
|
|
|
+ optionalString (i.proxyARP && cfg.enableIPv6)
|
|
|
|
|
''
|
|
|
|
|
echo 1 > /proc/sys/net/ipv6/conf/${i.name}/proxy_ndp
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createTunDevice = i: nameValuePair "${i.name}"
|
|
|
|
|
{ description = "Virtual Network Interface ${i.name}";
|
|
|
|
|
wantedBy = [ "network.target" ];
|
|
|
|
|
serviceConfig =
|
|
|
|
|
{ Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
ExecStart = "${pkgs.tunctl}/bin/tunctl -t '${i.name}' -u '${i.virtualOwner}'";
|
|
|
|
|
ExecStop = "${pkgs.tunctl}/bin/tunctl -d '${i.name}'";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createBridgeDevice = n: v:
|
|
|
|
|
let
|
|
|
|
|
deps = map (i: "sys-subsystem-net-devices-${i}.device") v.interfaces;
|
|
|
|
|
in
|
|
|
|
|
{ description = "Bridge Interface ${n}";
|
|
|
|
|
wantedBy = [ "network.target" ];
|
|
|
|
|
wants = map (i: "${i}.service") v.interfaces;
|
|
|
|
|
bindsTo = deps;
|
|
|
|
|
after = deps;
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.bridge_utils pkgs.iproute ];
|
|
|
|
|
script =
|
|
|
|
|
''
|
|
|
|
|
brctl addbr "${n}"
|
|
|
|
|
|
|
|
|
|
# Set bridge's hello time to 0 to avoid startup delays.
|
|
|
|
|
brctl setfd "${n}" 0
|
|
|
|
|
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
brctl addif "${n}" "${i}"
|
|
|
|
|
ip addr flush dev "${i}"
|
|
|
|
|
'')}
|
|
|
|
|
|
|
|
|
|
# !!! Should delete (brctl delif) any interfaces that
|
|
|
|
|
# no longer belong to the bridge.
|
|
|
|
|
'';
|
|
|
|
|
postStop =
|
|
|
|
|
''
|
|
|
|
|
ip link set "${n}" down
|
|
|
|
|
brctl delbr "${n}"
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
in listToAttrs (
|
|
|
|
|
map configureInterface cfg.interfaces ++
|
|
|
|
|
map createTunDevice (filter (i: i.virtual) cfg.interfaces))
|
|
|
|
|
// mapAttrs createBridgeDevice cfg.bridges;
|
|
|
|
|
|
2010-09-13 15:41:38 +00:00
|
|
|
|
# Set the host name in the activation script. Don't clear it if
|
|
|
|
|
# it's not configured in the NixOS configuration, since it may
|
|
|
|
|
# have been set by dhclient in the meantime.
|
|
|
|
|
system.activationScripts.hostname =
|
2010-09-14 11:58:55 +00:00
|
|
|
|
optionalString (config.networking.hostName != "") ''
|
2010-09-13 15:41:38 +00:00
|
|
|
|
hostname "${config.networking.hostName}"
|
2010-09-14 11:58:55 +00:00
|
|
|
|
'';
|
2009-07-16 17:18:54 +00:00
|
|
|
|
|
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2006-11-20 17:06:44 +00:00
|
|
|
|
}
|