summary refs log tree commit diff
path: root/modules/tasks/network-interfaces.nix
blob: fab61b79c987a8bba137c863bc4ca60acc9cefa1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{ config, pkgs, ... }:

with pkgs.lib;

let

  cfg = config.networking;

in 

{

  ###### interface

  options = {

    networking.hostName = mkOption {
      default = "nixos";
      description = ''
        The name of the machine.  Leave it empty if you want to obtain
        it from a DHCP server (if using DHCP).
      '';
    };

    networking.enableIPv6 = mkOption {
      default = true;
      description = ''
        Whether to enable support for IPv6.
      '';
    };

    networking.defaultGateway = mkOption {
      default = "";
      example = "131.211.84.1";
      description = ''
        The default gateway.  It can be left empty if it is auto-detected through DHCP.
      '';
    };

    networking.nameservers = mkOption {
      default = [];
      example = ["130.161.158.4" "130.161.33.17"];
      description = ''
        The list of nameservers.  It can be left empty if it is auto-detected through DHCP.
      '';
    };

    networking.domain = mkOption {
      default = "";
      example = "home";
      description = ''
        The domain.  It can be left empty if it is auto-detected through DHCP.
      '';
    };

    networking.localCommands = mkOption {
      default = "";
      example = "text=anything; echo You can put $text here.";
      description = ''
        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.
      '';
    };

    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.
          '';
        };

        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.
          '';
        };

      };
      
    };

    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.
      '';
    };
    
    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.";
        };

      };
      
    };

  };


  ###### implementation

  config = {

    boot.kernelModules = optional cfg.enableIPv6 "ipv6";

    environment.systemPackages =
      [ pkgs.host
        pkgs.iproute
        pkgs.iputils
        pkgs.nettools
        pkgs.wirelesstools
        pkgs.rfkill
      ] 
      ++ optional (cfg.bridges != {}) pkgs.bridge_utils
      ++ optional cfg.enableIPv6 pkgs.ndisc6;

    security.setuidPrograms = [ "ping" "ping6" ];
    
    jobs.networkInterfaces = 
      { name = "network-interfaces";

        startOn = "stopped udevtrigger";

        path = [ pkgs.iproute ];

        preStart =
          ''
            set +e # continue in case of errors
          
            ${flip concatMapStrings cfg.interfaces (i:
              optionalString (i.macAddress != "")
                ''
                  echo "Setting MAC address of ${i.name} to ${i.macAddress}..."
                  ip link set "${i.name}" address "${i.macAddress}"
                '')
            }

            for i in $(cd /sys/class/net && ls -d *); do
                echo "Bringing up network device $i..."
                ip link set "$i" up
            done

            # Create bridge devices.
            ${concatStrings (attrValues (flip mapAttrs cfg.bridges (n: v: ''
                echo "Creating bridge ${n}..."
                ${pkgs.bridge_utils}/sbin/brctl addbr "${n}"
                
                ${flip concatMapStrings v.interfaces (i: ''
                  ${pkgs.bridge_utils}/sbin/brctl addif "${n}" "${i}"
                  ip addr flush dev "${i}"
                '')}

                # For some reason enslaving an interface to a bridge
                # causes traffic to be blocked for a few seconds, long
                # enough for IPv6 router solicitations to get lost.
                # So increase the number of attemts.
                ${optionalString cfg.enableIPv6 ''
                  echo 5 > /proc/sys/net/ipv6/conf/${n}/router_solicitations
                ''}
                
                # !!! Should delete (brctl delif) any interfaces that
                # no longer belong to the bridge.
            '')))}
            
            # Configure the manually specified interfaces.
            ${flip concatMapStrings cfg.interfaces (i:
              optionalString (i.ipAddress != "")
                ''
                  echo "Configuring interface ${i.name}..."
                  ip addr add "${i.ipAddress}""${optionalString (i.subnetMask != "") ("/" + i.subnetMask)}" \
                    dev "${i.name}"
                '')
            }

            # 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.
            ${optionalString (cfg.defaultGateway != "") ''
                ip route add default via "${cfg.defaultGateway}"
            ''}

            # Run any user-specified commands.
            ${pkgs.stdenv.shell} ${pkgs.writeText "local-net-cmds" cfg.localCommands}

            ${optionalString (cfg.interfaces != [] || cfg.localCommands != "") ''
              # Emit the ip-up event (e.g. to start ntpd).
              initctl emit -n ip-up
            ''}
          '';
      };

    # 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 =
      optionalString (config.networking.hostName != "") ''
        hostname "${config.networking.hostName}"
      '';

  };
  
}