summaryrefslogtreecommitdiffstats
path: root/nixos/modules/tasks/network-interfaces-scripted.nix
blob: 7b96e1efc025efabc36a457e7e362e5ef6220279 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
{
  config,
  lib,
  pkgs,
  utils,
  ...
}:

with utils;
with lib;

let

  cfg = config.networking;
  interfaces = attrValues cfg.interfaces;

  slaves =
    concatMap (i: i.interfaces) (attrValues cfg.bonds)
    ++ concatMap (i: i.interfaces) (attrValues cfg.bridges)
    ++ concatMap (i: attrNames (filterAttrs (_: config: config.type != "internal") i.interfaces)) (
      attrValues cfg.vswitches
    )
    ++ concatMap (i: [ i.interface ]) (attrValues cfg.macvlans)
    ++ concatMap (i: [ i.interface ]) (attrValues cfg.ipvlans)
    ++ concatMap (i: [ i.interface ]) (attrValues cfg.vlans);

  # We must escape interfaces due to the systemd interpretation
  subsystemDevice = interface: "sys-subsystem-net-devices-${escapeSystemdPath interface}.device";

  interfaceIps = i: i.ipv4.addresses ++ optionals cfg.enableIPv6 i.ipv6.addresses;

  destroyBond = i: ''
    while true; do
      UPDATED=1
      SLAVES=$(ip link | grep 'master ${i}' | awk -F: '{print $2}')
      for I in $SLAVES; do
        UPDATED=0
        ip link set dev "$I" nomaster
      done
      [ "$UPDATED" -eq "1" ] && break
    done
    ip link set dev "${i}" down 2>/dev/null || true
    ip link del dev "${i}" 2>/dev/null || true
  '';

  formatIpArgs =
    args:
    lib.pipe args [
      (lib.filterAttrs (n: v: v != null))
      (lib.mapAttrsToList (n: v: "${n} ${toString v}"))
      (lib.concatStringsSep " ")
    ];

  # Converts an IPv4 address literal to a list of bits
  parseAddr.ipv4 =
    addr:
    let
      pad = b: lib.replicate (8 - builtins.length b) 0 ++ b;
      toBin = n: pad (lib.toBaseDigits 2 (lib.toInt n));
    in
    lib.concatMap toBin (builtins.splitVersion addr);

  # Converts an IPv6 address literal to a list of bits
  parseAddr.ipv6 =
    addr:
    let
      pad = b: lib.replicate (16 - builtins.length b) 0 ++ b;
      fromHex = n: (builtins.fromTOML "n = 0x${n}").n;
      toBin = n: pad (lib.toBaseDigits 2 (fromHex n));
      normal = (lib.network.ipv6.fromString addr).address;
    in
    lib.concatMap toBin (lib.splitString ":" normal);

  # Checks if `addr` is part of the `net` subnet
  inSubnet =
    v: net: addr:
    let
      prefix = lib.take net.prefixLength (parseAddr.${v} net.address);
      match = lib.zipListsWith (a: b: a == b) prefix (parseAddr.${v} addr);
    in
    lib.all lib.id match;

  # Checks if the netmask of all addresses on interface `iface` includes
  # the IP address of `gateway`
  #
  # Note: this is used to check whether networking.defaultGateway relies on
  # the given interface, either explicitly, via the `interface` (optional),
  # or explicitly, by using an address in a subnet of this interface.
  #
  # Configuration of the default gateway is then performed as part of that
  # interface setup in `configureAddrs`, below.
  isGateway =
    v: gateway: iface:
    lib.any lib.id (
      [ (iface.name == gateway.interface) ]
      ++ map (net: inSubnet v net gateway.address) iface.${v}.addresses
    );

  # Checks if `gateway` uses an address from `iface` as default source
  #
  # Note: this is needed to delay the configuration of the gateway and default
  # source until the right interfaces and address have been set up, otherwise
  # the commands will fail.
  hasSource =
    v: gateway: iface:
    builtins.elem gateway.source (map (i: i.address) iface.${v}.addresses);

  # Interfaces corresponding to the default gateways
  gateway4Iface = builtins.filter (isGateway "ipv4" cfg.defaultGateway) interfaces;
  gateway6Iface = builtins.filter (isGateway "ipv6" cfg.defaultGateway6) interfaces;

  # Interfaces corresponding to the default source addresses
  #
  # Note: the use of `head` here is safe because these expressions
  # are evaluated only when `needsSourceIface`, see `configureAddrs` below.
  source4Iface = builtins.head (builtins.filter (hasSource "ipv4" cfg.defaultGateway) interfaces);
  source6Iface = builtins.head (builtins.filter (hasSource "ipv6" cfg.defaultGateway6) interfaces);

  # warn that these attributes are deprecated (2017-2-2)
  # Should be removed in the release after next
  bondDeprecation = rec {
    deprecated = [
      "lacp_rate"
      "miimon"
      "mode"
      "xmit_hash_policy"
    ];
    filterDeprecated =
      bond: (filterAttrs (attrName: attr: elem attrName deprecated && attr != null) bond);
  };

  bondWarnings =
    let
      oneBondWarnings =
        bondName: bond: mapAttrsToList (bondText bondName) (bondDeprecation.filterDeprecated bond);
      bondText =
        bondName: optName: _:
        "${bondName}.${optName} is deprecated, use ${bondName}.driverOptions";
    in
    {
      warnings = flatten (mapAttrsToList oneBondWarnings cfg.bonds);
    };

  normalConfig = {
    systemd.network.links =
      let
        createNetworkLink =
          i:
          nameValuePair "40-${i.name}" {
            matchConfig.OriginalName = i.name;
            linkConfig =
              optionalAttrs (i.macAddress != null) {
                MACAddress = i.macAddress;
              }
              // optionalAttrs (i.mtu != null) {
                MTUBytes = toString i.mtu;
              };
          };
      in
      listToAttrs (map createNetworkLink interfaces);
    systemd.services =
      let

        deviceDependency =
          dev:
          # Use systemd service if we manage device creation, else
          # trust udev when not in a container
          if (dev == null || dev == "lo") then
            [ ]
          else if
            (hasAttr dev (filterAttrs (k: v: v.virtual) cfg.interfaces))
            || (hasAttr dev cfg.bridges)
            || (hasAttr dev cfg.bonds)
            || (hasAttr dev cfg.macvlans)
            || (hasAttr dev cfg.ipvlans)
            || (hasAttr dev cfg.sits)
            || (hasAttr dev cfg.ipips)
            || (hasAttr dev cfg.vlans)
            || (hasAttr dev cfg.greTunnels)
            || (hasAttr dev cfg.vswitches)
          then
            [ "${dev}-netdev.service" ]
          else
            optional (!config.boot.isContainer) (subsystemDevice dev);

        # For each interface <foo>, creates a network-addresses-<foo>.service
        # job that performs static address configuration.
        #
        # It has a Wants dependency on <foo>-netdev.service, which creates
        # create the interface, or on a device unit (for hardware interfaces).
        # It also has a BindsTo dependency on the device unit: so, it only gets
        # started after the interface has appeared and it's stopped when the
        # interface disappears.
        #
        # Unless in a container, the job is not made part of network.target, so
        # if an interface is not found (e.g. a USB interface not plugged in) it
        # will not hang the boot sequence.
        #
        # If the interface is the default gateway, the job will also set the
        # default gateway and delay network-online.target.
        configureAddrs =
          i:
          let
            ips = interfaceIps i;
            isDefaultGateway4 = cfg.defaultGateway != null && builtins.elem i gateway4Iface;
            isDefaultGateway6 = cfg.defaultGateway6 != null && builtins.elem i gateway6Iface;
            needsSourceIface4 =
              isDefaultGateway4 && cfg.defaultGateway.source != null && i.name != source4Iface.name;
            needsSourceIface6 =
              isDefaultGateway6 && cfg.defaultGateway6.source != null && i.name != source6Iface.name;

            configureGateway =
              version: gateway:
              optionalString (gateway.address != "") ''
                echo -n "setting ${i.name} as default IPv${version} gateway... "
                ${optionalString (gateway.interface != null) ''
                  ip -${version} route replace ${gateway.address} proto static ${
                    formatIpArgs {
                      metric = gateway.metric;
                      dev = gateway.interface;
                    }
                  }
                ''}
                ip -${version} route replace default proto static ${
                  formatIpArgs {
                    metric = gateway.metric;
                    via = gateway.address;
                    window = cfg.defaultGatewayWindowSize;
                    dev = gateway.interface;
                    src = gateway.source;
                  }
                }
                echo "done"
              '';
          in
          nameValuePair "network-addresses-${i.name}" {
            description = "Address configuration of ${i.name}";

            wantedBy =
              deviceDependency i.name
              ++ optional config.boot.isContainer "network.target"
              ++ optional (isDefaultGateway4 || isDefaultGateway6) "network-online.target";
            bindsTo = deviceDependency i.name;
            partOf = [ "networking-scripted.target" ];
            after = [
              "network-pre.target"
            ]
            ++ optional needsSourceIface4 "network-addresses-${source4Iface.name}.service"
            ++ optional needsSourceIface6 "network-addresses-${source6Iface.name}.service"
            ++ deviceDependency i.name;
            serviceConfig.Type = "oneshot";
            serviceConfig.RemainAfterExit = true;
            # Restart rather than stop+start this unit to prevent the
            # network from dying during switch-to-configuration.
            stopIfChanged = false;
            path = [ pkgs.iproute2 ];
            script = ''
              state="/run/nixos/network/addresses/${i.name}"
              mkdir -p "$(dirname "$state")"

              ip link set dev "${i.name}" up

              ${flip concatMapStrings ips (
                ip:
                let
                  cidr = "${ip.address}/${toString ip.prefixLength}";
                in
                ''
                  echo "${cidr}" >> $state
                  echo -n "adding address ${cidr}... "
                  if out=$(ip addr replace "${cidr}" dev "${i.name}" nodad 2>&1); then
                    echo "done"
                  else
                    echo "'ip addr replace \"${cidr}\" dev \"${i.name}\"' nodad failed: $out"
                    exit 1
                  fi
                ''
              )}

              state="/run/nixos/network/routes/${i.name}"
              mkdir -p "$(dirname "$state")"

              ${flip concatMapStrings (i.ipv4.routes ++ i.ipv6.routes) (
                route:
                let
                  cidr = "${route.address}/${toString route.prefixLength}";
                  via = optionalString (route.via != null) ''via "${route.via}"'';
                  options = concatStrings (mapAttrsToList (name: val: "${name} ${val} ") route.options);
                  type = toString route.type;
                in
                ''
                  echo "${cidr}" >> $state
                  echo -n "adding route ${cidr}... "
                  if out=$(ip route add ${type} "${cidr}" ${options} ${via} dev "${i.name}" proto static 2>&1); then
                    echo "done"
                  elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
                    echo "'ip route add ${type} \"${cidr}\" ${options} ${via} dev \"${i.name}\"' failed: $out"
                    exit 1
                  fi
                ''
              )}

              # Set the default gateway
              ${optionalString isDefaultGateway4 (configureGateway "4" cfg.defaultGateway)}
              ${optionalString isDefaultGateway6 (configureGateway "6" cfg.defaultGateway6)}
            '';
            preStop = ''
              state="/run/nixos/network/routes/${i.name}"
              if [ -e "$state" ]; then
                while read -r cidr; do
                  echo -n "deleting route $cidr... "
                  ip route del "$cidr" dev "${i.name}" >/dev/null 2>&1 && echo "done" || echo "failed"
                done < "$state"
                rm -f "$state"
              fi

              state="/run/nixos/network/addresses/${i.name}"
              if [ -e "$state" ]; then
                while read -r cidr; do
                  echo -n "deleting address $cidr... "
                  ip addr del "$cidr" dev "${i.name}" >/dev/null 2>&1 && echo "done" || echo "failed"
                done < "$state"
                rm -f "$state"
              fi
            '';
          };

        createTunDevice =
          i:
          nameValuePair "${i.name}-netdev" {
            description = "Virtual Network Interface ${i.name}";
            bindsTo = optional (!config.boot.isContainer) "dev-net-tun.device";
            partOf = [ "networking-scripted.target" ];
            after = optional (!config.boot.isContainer) "dev-net-tun.device" ++ [ "network-pre.target" ];
            wantedBy = [
              "network.target"
              (subsystemDevice i.name)
            ];
            before = [ "network.target" ];
            path = [ pkgs.iproute2 ];
            serviceConfig = {
              Type = "oneshot";
              RemainAfterExit = true;
            };
            script = ''
              ip tuntap add dev "${i.name}" mode "${i.virtualType}" ${
                lib.optionalString (i.virtualOwner != null) ''user "${i.virtualOwner}"''
              }
            '';
            postStop = ''
              ip link del dev ${i.name} || true
            '';
          };

        createBridgeDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = concatLists (map deviceDependency v.interfaces);
            in
            {
              description = "Bridge Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps ++ optional v.rstp "mstpd.service";
              partOf = [
                "network.target"
                "networking-scripted.target"
              ]
              ++ optional v.rstp "mstpd.service";
              after = [
                "network-pre.target"
              ]
              ++ deps
              ++ optional v.rstp "mstpd.service"
              ++ map (i: "network-addresses-${i}.service") v.interfaces;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                echo "Removing old bridge ${n}..."
                ip link show dev "${n}" >/dev/null 2>&1 && ip link del dev "${n}"

                echo "Adding bridge ${n}..."
                ip link add name "${n}" type bridge

                # Enslave child interfaces
                ${flip concatMapStrings v.interfaces (i: ''
                  ip link set dev "${i}" master "${n}"
                  ip link set dev "${i}" up
                '')}
                # Save list of enslaved interfaces
                echo "${
                  flip concatMapStrings v.interfaces (i: ''
                    ${i}
                  '')
                }" > /run/${n}.interfaces

                ${optionalString config.virtualisation.libvirtd.enable ''
                  # Enslave dynamically added interfaces which may be lost on nixos-rebuild
                  #
                  # if `libvirtd.service` is not running, do not use `virsh` which would try activate it via 'libvirtd.socket' and thus start it out-of-order.
                  # `libvirtd.service` will set up bridge interfaces when it will start normally.
                  #
                  if /run/current-system/systemd/bin/systemctl --quiet is-active 'libvirtd.service'; then
                    for uri in qemu:///system lxc:///; do
                      for dom in $(${pkgs.libvirt}/bin/virsh -c $uri list --name); do
                        ${pkgs.libvirt}/bin/virsh -c $uri dumpxml "$dom" | \
                        ${pkgs.xmlstarlet}/bin/xmlstarlet sel -t -m "//domain/devices/interface[@type='bridge'][source/@bridge='${n}'][target/@dev]" -v "concat('ip link set dev ',target/@dev,' master ',source/@bridge,';')" | \
                        ${pkgs.bash}/bin/bash
                      done
                    done
                  fi
                ''}

                # Enable stp on the interface
                ${optionalString v.rstp ''
                  echo 2 >/sys/class/net/${n}/bridge/stp_state
                ''}

                ip link set dev "${n}" up
              '';
              postStop = ''
                ip link set dev "${n}" down || true
                ip link del dev "${n}" || true
                rm -f /run/${n}.interfaces
              '';
              reload = ''
                # shellcheck disable=SC2013
                # Un-enslave child interfaces (old list of interfaces)
                for interface in $(cat /run/${n}.interfaces); do
                  ip link set dev "$interface" nomaster up
                done

                # Enslave child interfaces (new list of interfaces)
                ${flip concatMapStrings v.interfaces (i: ''
                  ip link set dev "${i}" master "${n}"
                  ip link set dev "${i}" up
                '')}
                # Save list of enslaved interfaces
                echo "${
                  flip concatMapStrings v.interfaces (i: ''
                    ${i}
                  '')
                }" > /run/${n}.interfaces

                # (Un-)set stp on the bridge
                echo ${if v.rstp then "2" else "0"} > /sys/class/net/${n}/bridge/stp_state
              '';
              reloadIfChanged = true;
            }
          );

        createVswitchDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = concatLists (
                map deviceDependency (attrNames (filterAttrs (_: config: config.type != "internal") v.interfaces))
              );
              internalConfigs = map (i: "network-addresses-${i}.service") (
                attrNames (filterAttrs (_: config: config.type == "internal") v.interfaces)
              );
              ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
            in
            {
              description = "Open vSwitch Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ]
              ++ internalConfigs;
              before = [ "network.target" ] ++ internalConfigs;
              partOf = [
                "network.target"
                "networking-scripted.target"
              ]; # shutdown the bridge when network is shutdown
              bindsTo = [ "ovs-vswitchd.service" ]; # requires ovs-vswitchd to be alive at all times
              after = [
                "network-pre.target"
                "ovs-vswitchd.service"
              ]
              ++ deps; # start switch after physical interfaces and vswitch daemon
              wants = deps; # if one or more interface fails, the switch should continue to run
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [
                pkgs.iproute2
                config.virtualisation.vswitch.package
              ];
              preStart = ''
                echo "Resetting Open vSwitch ${n}..."
                ovs-vsctl --if-exists del-br ${n} -- add-br ${n} \
                          -- set bridge ${n} protocols=${concatStringsSep "," v.supportedOpenFlowVersions}
              '';
              script = ''
                echo "Configuring Open vSwitch ${n}..."
                ovs-vsctl ${
                  concatStrings (
                    mapAttrsToList (
                      name: config:
                      " -- add-port ${n} ${name}" + optionalString (config.vlan != null) " tag=${toString config.vlan}"
                    ) v.interfaces
                  )
                } \
                  ${
                    concatStrings (
                      mapAttrsToList (
                        name: config: optionalString (config.type != null) " -- set interface ${name} type=${config.type}"
                      ) v.interfaces
                    )
                  } \
                  ${concatMapStrings (x: " -- set-controller ${n} " + x) v.controllers} \
                  ${concatMapStrings (x: " -- " + x) (splitString "\n" v.extraOvsctlCmds)}


                echo "Adding OpenFlow rules for Open vSwitch ${n}..."
                ovs-ofctl --protocols=${v.openFlowVersion} add-flows ${n} ${ofRules}
              '';
              postStop = ''
                echo "Cleaning Open vSwitch ${n}"
                echo "Shutting down internal ${n} interface"
                ip link set dev ${n} down || true
                echo "Deleting flows for ${n}"
                ovs-ofctl --protocols=${v.openFlowVersion} del-flows ${n} || true
                echo "Deleting Open vSwitch ${n}"
                ovs-vsctl --if-exists del-br ${n} || true
              '';
            }
          );

        createBondDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = concatLists (map deviceDependency v.interfaces);
            in
            {
              description = "Bond Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [ "networking-scripted.target" ];
              after = [ "network-pre.target" ] ++ deps ++ map (i: "network-addresses-${i}.service") v.interfaces;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [
                pkgs.iproute2
                pkgs.gawk
              ];
              script = ''
                echo "Destroying old bond ${n}..."
                ${destroyBond n}

                echo "Creating new bond ${n}..."
                ip link add name "${n}" type bond \
                ${
                  let
                    opts = (mapAttrs (const toString) (bondDeprecation.filterDeprecated v)) // v.driverOptions;
                  in
                  concatStringsSep "\n" (mapAttrsToList (set: val: "  ${set} ${val} \\") opts)
                }

                # !!! There must be a better way to wait for the interface
                while [ ! -d "/sys/class/net/${n}" ]; do sleep 0.1; done;

                # Bring up the bond and enslave the specified interfaces
                ip link set dev "${n}" up
                ${flip concatMapStrings v.interfaces (i: ''
                  ip link set dev "${i}" down
                  ip link set dev "${i}" master "${n}"
                '')}
              '';
              postStop = destroyBond n;
            }
          );

        createMacvlanDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = deviceDependency v.interface;
            in
            {
              description = "MACVLAN Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [ "networking-scripted.target" ];
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
                ip link add link "${v.interface}" name "${n}" type macvlan \
                  ${optionalString (v.mode != null) "mode ${v.mode}"}
                ip link set dev "${n}" up
              '';
              postStop = ''
                ip link delete dev "${n}" || true
              '';
            }
          );

        createIpvlanDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = deviceDependency v.interface;
            in
            {
              description = "IPVLAN Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [ "networking-scripted.target" ];
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
                ip link add link "${v.interface}" name "${n}" type ipvlan \
                  ${optionalString (v.mode != null) "mode ${v.mode}"} \
                  ${optionalString (v.flags != null) "${v.flags}"}
                ip link set dev "${n}" up
              '';
              postStop = ''
                ip link delete dev "${n}" || true
              '';
            }
          );

        createFouEncapsulation =
          n: v:
          nameValuePair "${n}-fou-encap" (
            let
              # if we have a device to bind to we can wait for its addresses to be
              # configured, otherwise external sequencing is required.
              deps = optionals (v.local != null && v.local.dev != null) (
                deviceDependency v.local.dev ++ [ "network-addresses-${v.local.dev}.service" ]
              );
              fouSpec = "port ${toString v.port} ${
                if v.protocol != null then "ipproto ${toString v.protocol}" else "gue"
              } ${
                optionalString (v.local != null)
                  "local ${escapeShellArg v.local.address} ${
                    optionalString (v.local.dev != null) "dev ${escapeShellArg v.local.dev}"
                  }"
              }";
            in
            {
              description = "FOU endpoint ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [ "networking-scripted.target" ];
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # always remove previous incarnation since show can't filter
                ip fou del ${fouSpec} >/dev/null 2>&1 || true
                ip fou add ${fouSpec}
              '';
              postStop = ''
                ip fou del ${fouSpec} || true
              '';
            }
          );

        createSitDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = deviceDependency v.dev;
            in
            {
              description = "IPv6 in IPv4 Tunnel Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
                ip link add name "${n}" type sit ${
                  formatIpArgs {
                    inherit (v)
                      remote
                      local
                      ttl
                      dev
                      ;
                    encap = if v.encapsulation.type == "6in4" then null else v.encapsulation.type;
                    encap-dport = v.encapsulation.port;
                    encap-sport = v.encapsulation.sourcePort;
                  }
                }
                ip link set dev "${n}" up
              '';
              postStop = ''
                ip link delete dev "${n}" || true
              '';
            }
          );

        createIpipDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = deviceDependency v.dev;
            in
            {
              description = "IP in IP Tunnel Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [ "networking-scripted.target" ];
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
                ip tunnel add name "${n}" ${
                  formatIpArgs {
                    inherit (v)
                      remote
                      local
                      ttl
                      dev
                      ;
                    mode =
                      {
                        "4in6" = "ipip6";
                        "ipip" = "ipip";
                      }
                      .${v.encapsulation.type};
                    encaplimit = if v.encapsulation.type == "ipip" then null else v.encapsulation.limit;
                  }
                }
                ip link set dev "${n}" up
              '';
              postStop = ''
                ip link delete dev "${n}" || true
              '';
            }
          );

        createGreDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = deviceDependency v.dev;
              ttlarg = if lib.hasPrefix "ip6" v.type then "hoplimit" else "ttl";
            in
            {
              description = "GRE Tunnel Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [ "networking-scripted.target" ];
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
                ip link add name "${n}" type ${v.type} \
                  ${optionalString (v.remote != null) "remote \"${v.remote}\""} \
                  ${optionalString (v.local != null) "local \"${v.local}\""} \
                  ${optionalString (v.ttl != null) "${ttlarg} ${toString v.ttl}"} \
                  ${optionalString (v.dev != null) "dev \"${v.dev}\""}
                ip link set dev "${n}" up
              '';
              postStop = ''
                ip link delete dev "${n}" || true
              '';
            }
          );

        createVlanDevice =
          n: v:
          nameValuePair "${n}-netdev" (
            let
              deps = deviceDependency v.interface;
            in
            {
              description = "VLAN Interface ${n}";
              wantedBy = [
                "network.target"
                (subsystemDevice n)
              ];
              bindsTo = deps;
              partOf = [
                "network.target"
                "networking-scripted.target"
              ];
              after = [ "network-pre.target" ] ++ deps;
              before = [ "network.target" ];
              serviceConfig.Type = "oneshot";
              serviceConfig.RemainAfterExit = true;
              path = [ pkgs.iproute2 ];
              script = ''
                # Remove Dead Interfaces
                ip link show dev "${n}" >/dev/null 2>&1 && ip link delete dev "${n}"
                ip link add link "${v.interface}" name "${n}" type vlan id "${toString v.id}"

                # We try to bring up the logical VLAN interface. If the master
                # interface the logical interface is dependent upon is not up yet we will
                # fail to immediately bring up the logical interface. The resulting logical
                # interface will brought up later when the master interface is up.
                ip link set dev "${n}" up || true
              '';
              postStop = ''
                ip link delete dev "${n}" || true
              '';
            }
          );

      in
      listToAttrs (
        map configureAddrs interfaces ++ map createTunDevice (filter (i: i.virtual) interfaces)
      )
      // mapAttrs' createBridgeDevice cfg.bridges
      // mapAttrs' createVswitchDevice cfg.vswitches
      // mapAttrs' createBondDevice cfg.bonds
      // mapAttrs' createMacvlanDevice cfg.macvlans
      // mapAttrs' createIpvlanDevice cfg.ipvlans
      // mapAttrs' createFouEncapsulation cfg.fooOverUDP
      // mapAttrs' createSitDevice cfg.sits
      // mapAttrs' createIpipDevice cfg.ipips
      // mapAttrs' createGreDevice cfg.greTunnels
      // mapAttrs' createVlanDevice cfg.vlans
      // {
        network-local-commands = {
          after = [ "network-pre.target" ];
          wantedBy = [ "network.target" ];
        };
      };

    # Note: the scripted networking backend consistent of many
    # independent services that are linked to the network.target.
    # Since there is no daemon (e.g systemd-networkd) that is
    # started as part of the system and pulls in network.target.
    # Thus, to start these services we link network.target directly
    # to multi-user.target, this has the same result.
    systemd.targets.network.wantedBy = [ "multi-user.target" ];

    # This target serves no purpose during the boot, but can be
    # used to quickly reset the network configuration by running
    # systemctl restart networking-scripted.target
    systemd.targets.networking-scripted = {
      description = "NixOS scripted networking setup";
    };

    services.udev.extraRules = lib.concatStringsSep "\n" (
      [ ''KERNEL=="tun", TAG+="systemd"'' ]
      # This creates a udev rule to start each service with a WantedBy
      # dependency on a device unit. It's needed because if the service
      # unit is loaded in stage 2 but its device was already up by
      # stage 1, systemd will not automatically start it.
      ++ lib.forEach (lib.attrNames cfg.interfaces) (
        iface:
        ''ACTION=="add", SUBSYSTEM=="net", KERNEL=="${iface}", ''
        + ''ENV{SYSTEMD_WANTS}="network-addresses-${iface}.service"''
      )
    );

  };

in

{

  meta.maintainers = with lib.maintainers; [ rnhmjoj ];

  config = mkMerge [
    bondWarnings
    (mkIf (!cfg.useNetworkd) normalConfig)
    {
      # Ensure slave interfaces are brought up
      networking.interfaces = genAttrs slaves (i: { });
    }
  ];
}