2009-10-12 19:27:57 +02:00
|
|
|
{ config, pkgs, ... }:
|
2009-03-06 13:27:38 +01:00
|
|
|
|
2009-10-12 19:27:57 +02:00
|
|
|
with pkgs.lib;
|
2007-02-12 17:00:55 +01:00
|
|
|
|
2009-10-12 19:27:57 +02:00
|
|
|
let
|
2009-05-28 18:03:48 +02:00
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
cfg = config.networking;
|
|
|
|
|
2011-09-14 20:20:50 +02:00
|
|
|
in
|
2009-07-16 19:18:54 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2009-05-28 18:03:48 +02:00
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
networking.hostName = mkOption {
|
|
|
|
default = "nixos";
|
2009-07-16 19:18:54 +02:00
|
|
|
description = ''
|
2009-05-28 18:03:48 +02: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 19:18:54 +02:00
|
|
|
'';
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
2011-02-19 18:21:29 +01:00
|
|
|
networking.enableIPv6 = mkOption {
|
|
|
|
default = true;
|
2009-07-16 19:18:54 +02:00
|
|
|
description = ''
|
2011-02-19 18:21:29 +01:00
|
|
|
Whether to enable support for IPv6.
|
2009-07-16 19:18:54 +02:00
|
|
|
'';
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
networking.defaultGateway = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "131.211.84.1";
|
2009-07-16 19:18:54 +02:00
|
|
|
description = ''
|
2009-05-28 18:03:48 +02:00
|
|
|
The default gateway. It can be left empty if it is auto-detected through DHCP.
|
2009-07-16 19:18:54 +02:00
|
|
|
'';
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
networking.nameservers = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = ["130.161.158.4" "130.161.33.17"];
|
2009-07-16 19:18:54 +02:00
|
|
|
description = ''
|
2009-05-28 18:03:48 +02:00
|
|
|
The list of nameservers. It can be left empty if it is auto-detected through DHCP.
|
2009-07-16 19:18:54 +02:00
|
|
|
'';
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
networking.domain = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "home";
|
2009-07-16 19:18:54 +02:00
|
|
|
description = ''
|
2009-05-28 18:03:48 +02:00
|
|
|
The domain. It can be left empty if it is auto-detected through DHCP.
|
2009-07-16 19:18:54 +02:00
|
|
|
'';
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
networking.localCommands = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "text=anything; echo You can put $text here.";
|
2009-07-16 19:18:54 +02:00
|
|
|
description = ''
|
2009-05-28 18:03:48 +02: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 19:18:54 +02: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 23:58:48 +01: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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
2010-05-21 16:12:03 +02: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 20:20:50 +02:00
|
|
|
|
2011-03-15 16:13:48 +01: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 20:20:50 +02:00
|
|
|
|
2011-03-15 16:13:48 +01:00
|
|
|
};
|
|
|
|
|
2009-05-28 18:03:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
###### implementation
|
2009-05-28 18:03:48 +02:00
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
config = {
|
2009-03-06 13:27:38 +01:00
|
|
|
|
2011-02-19 18:21:29 +01:00
|
|
|
boot.kernelModules = optional cfg.enableIPv6 "ipv6";
|
|
|
|
|
2009-09-29 17:43:52 +02:00
|
|
|
environment.systemPackages =
|
|
|
|
[ pkgs.host
|
|
|
|
pkgs.iproute
|
2010-06-04 16:00:56 +02:00
|
|
|
pkgs.iputils
|
2009-09-29 17:43:52 +02:00
|
|
|
pkgs.nettools
|
|
|
|
pkgs.wirelesstools
|
2010-04-21 13:37:52 +02:00
|
|
|
pkgs.rfkill
|
2011-09-14 20:20:50 +02:00
|
|
|
]
|
2011-03-24 17:23:28 +01:00
|
|
|
++ optional (cfg.bridges != {}) pkgs.bridge_utils
|
|
|
|
++ optional cfg.enableIPv6 pkgs.ndisc6;
|
2010-06-02 23:10:48 +02:00
|
|
|
|
|
|
|
security.setuidPrograms = [ "ping" "ping6" ];
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
|
|
jobs.networkInterfaces =
|
2009-07-16 19:18:54 +02:00
|
|
|
{ name = "network-interfaces";
|
2007-11-23 18:12:37 +01:00
|
|
|
|
2010-07-06 13:03:23 +02:00
|
|
|
startOn = "stopped udevtrigger";
|
2007-02-12 17:00:55 +01:00
|
|
|
|
2011-03-11 14:57:48 +01:00
|
|
|
path = [ pkgs.iproute ];
|
2011-03-09 13:28:44 +01:00
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
preStart =
|
|
|
|
''
|
2011-04-01 17:05:42 +02:00
|
|
|
set +e # continue in case of errors
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-03-11 15:50:11 +01:00
|
|
|
${flip concatMapStrings cfg.interfaces (i:
|
|
|
|
optionalString (i.macAddress != "")
|
2010-11-24 23:58:48 +01:00
|
|
|
''
|
2011-03-11 15:50:11 +01:00
|
|
|
echo "Setting MAC address of ${i.name} to ${i.macAddress}..."
|
2011-04-01 17:05:42 +02:00
|
|
|
ip link set "${i.name}" address "${i.macAddress}"
|
2011-03-11 15:50:11 +01:00
|
|
|
'')
|
2010-11-24 23:58:48 +01:00
|
|
|
}
|
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
for i in $(cd /sys/class/net && ls -d *); do
|
|
|
|
echo "Bringing up network device $i..."
|
2011-04-01 17:05:42 +02:00
|
|
|
ip link set "$i" up
|
2009-07-16 19:18:54 +02:00
|
|
|
done
|
|
|
|
|
2011-06-20 20:12:47 +02:00
|
|
|
# Create bridge devices.
|
|
|
|
${concatStrings (attrValues (flip mapAttrs cfg.bridges (n: v: ''
|
|
|
|
echo "Creating bridge ${n}..."
|
|
|
|
${pkgs.bridge_utils}/sbin/brctl addbr "${n}"
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-09-26 11:41:40 +02:00
|
|
|
# Set bridge's hello time to 0 to avoid startup delays.
|
|
|
|
${pkgs.bridge_utils}/sbin/brctl setfd "${n}" 0
|
|
|
|
|
2011-06-20 20:12:47 +02:00
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
${pkgs.bridge_utils}/sbin/brctl addif "${n}" "${i}"
|
|
|
|
ip addr flush dev "${i}"
|
|
|
|
'')}
|
|
|
|
|
|
|
|
# !!! Should delete (brctl delif) any interfaces that
|
|
|
|
# no longer belong to the bridge.
|
|
|
|
'')))}
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-07-16 19:18:54 +02:00
|
|
|
# Configure the manually specified interfaces.
|
2011-03-11 15:50:11 +01:00
|
|
|
${flip concatMapStrings cfg.interfaces (i:
|
|
|
|
optionalString (i.ipAddress != "")
|
2009-07-16 19:18:54 +02:00
|
|
|
''
|
|
|
|
echo "Configuring interface ${i.name}..."
|
2011-03-11 15:50:11 +01:00
|
|
|
ip addr add "${i.ipAddress}""${optionalString (i.subnetMask != "") ("/" + i.subnetMask)}" \
|
2011-04-01 17:05:42 +02:00
|
|
|
dev "${i.name}"
|
2011-03-11 15:50:11 +01:00
|
|
|
'')
|
|
|
|
}
|
2009-07-16 19:18:54 +02:00
|
|
|
|
|
|
|
# Set the nameservers.
|
|
|
|
if test -n "${toString cfg.nameservers}"; then
|
|
|
|
rm -f /etc/resolv.conf
|
|
|
|
if test -n "${cfg.domain}"; then
|
|
|
|
echo "domain ${cfg.domain}" >> /etc/resolv.conf
|
|
|
|
fi
|
|
|
|
for i in ${toString cfg.nameservers}; do
|
|
|
|
echo "nameserver $i" >> /etc/resolv.conf
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set the default gateway.
|
2011-03-11 15:50:11 +01:00
|
|
|
${optionalString (cfg.defaultGateway != "") ''
|
2011-04-01 17:05:42 +02:00
|
|
|
ip route add default via "${cfg.defaultGateway}"
|
2011-03-11 15:50:11 +01:00
|
|
|
''}
|
2009-07-16 19:18:54 +02:00
|
|
|
|
|
|
|
# Run any user-specified commands.
|
2011-04-01 17:05:42 +02:00
|
|
|
${pkgs.stdenv.shell} ${pkgs.writeText "local-net-cmds" cfg.localCommands}
|
2009-11-06 22:38:40 +01:00
|
|
|
|
2010-01-15 12:20:57 +01:00
|
|
|
${optionalString (cfg.interfaces != [] || cfg.localCommands != "") ''
|
2010-01-12 12:08:27 +01:00
|
|
|
# Emit the ip-up event (e.g. to start ntpd).
|
2009-11-06 22:38:40 +01:00
|
|
|
initctl emit -n ip-up
|
|
|
|
''}
|
2009-07-16 19:18:54 +02:00
|
|
|
'';
|
2010-09-13 17:41:38 +02: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 13:58:55 +02:00
|
|
|
optionalString (config.networking.hostName != "") ''
|
2010-09-13 17:41:38 +02:00
|
|
|
hostname "${config.networking.hostName}"
|
2010-09-14 13:58:55 +02:00
|
|
|
'';
|
2009-07-16 19:18:54 +02:00
|
|
|
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2006-11-20 18:06:44 +01:00
|
|
|
}
|