git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #145183 from veehaitch/networkd-DHCPServerStaticLease

nixos/networkd: add `dhcpServerStaticLeaseConfig` option

commit6e69e537ffabe80e4a90da44cab002ef20392a52 authorAndreas Rammhold <andreas@rammhold.de> dateThu 9 Dec 2021, 12:57 (5 years ago) parentcbf9da7ce81231
3 files changed+126
nixos/modules/system/boot/networkd.nix+44 nixos/tests/all-tests.nix+1 nixos/tests/systemd-networkd-dhcpserver-static-leases.nix+81
nixos/modules/system/boot/networkd.nix+44
@@ -823,6 +823,16 @@ let
823823 (assertValueOneOf "OnLink" boolValues)
824824 ];
825825
826+ sectionDHCPServerStaticLease = checkUnitConfig "DHCPServerStaticLease" [
827+ (assertOnlyFields [
828+ "MACAddress"
829+ "Address"
830+ ])
831+ (assertHasField "MACAddress")
832+ (assertHasField "Address")
833+ (assertMacAddress "MACAddress")
834+ ];
835+
826836 };
827837 };
828838
@@ -1163,6 +1173,25 @@ let
11631173 };
11641174 };
11651175
1176+ dhcpServerStaticLeaseOptions = {
1177+ options = {
1178+ dhcpServerStaticLeaseConfig = mkOption {
1179+ default = {};
1180+ example = { MACAddress = "65:43:4a:5b:d8:5f"; Address = "192.168.1.42"; };
1181+ type = types.addCheck (types.attrsOf unitOption) check.network.sectionDHCPServerStaticLease;
1182+ description = ''
1183+ Each attribute in this set specifies an option in the
1184+ <literal>[DHCPServerStaticLease]</literal> section of the unit. See
1185+ <citerefentry><refentrytitle>systemd.network</refentrytitle>
1186+ <manvolnum>5</manvolnum></citerefentry> for details.
1187+
1188+ Make sure to configure the corresponding client interface to use
1189+ <literal>ClientIdentifier=mac</literal>.
1190+ '';
1191+ };
1192+ };
1193+ };
1194+
11661195 networkOptions = commonNetworkOptions // {
11671196
11681197 linkConfig = mkOption {
@@ -1275,6 +1304,17 @@ let
12751304 '';
12761305 };
12771306
1307+ dhcpServerStaticLeases = mkOption {
1308+ default = [];
1309+ example = [ { MACAddress = "65:43:4a:5b:d8:5f"; Address = "192.168.1.42"; } ];
1310+ type = with types; listOf (submodule dhcpServerStaticLeaseOptions);
1311+ description = ''
1312+ A list of DHCPServerStaticLease sections to be added to the unit. See
1313+ <citerefentry><refentrytitle>systemd.network</refentrytitle>
1314+ <manvolnum>5</manvolnum></citerefentry> for details.
1315+ '';
1316+ };
1317+
12781318 ipv6Prefixes = mkOption {
12791319 default = [];
12801320 example = [ { AddressAutoconfiguration = true; OnLink = true; } ];
@@ -1646,6 +1686,10 @@ let
16461686 [IPv6Prefix]
16471687 ${attrsToSection x.ipv6PrefixConfig}
16481688 '')
1689+ + flip concatMapStrings def.dhcpServerStaticLeases (x: ''
1690+ [DHCPServerStaticLease]
1691+ ${attrsToSection x.dhcpServerStaticLeaseConfig}
1692+ '')
16491693 + def.extraConfig;
16501694 };
16511695
nixos/tests/all-tests.nix+1
@@ -454,6 +454,7 @@ in
454454 systemd-journal = handleTest ./systemd-journal.nix {};
455455 systemd-networkd = handleTest ./systemd-networkd.nix {};
456456 systemd-networkd-dhcpserver = handleTest ./systemd-networkd-dhcpserver.nix {};
457+ systemd-networkd-dhcpserver-static-leases = handleTest ./systemd-networkd-dhcpserver-static-leases.nix {};
457458 systemd-networkd-ipv6-prefix-delegation = handleTest ./systemd-networkd-ipv6-prefix-delegation.nix {};
458459 systemd-networkd-vrf = handleTest ./systemd-networkd-vrf.nix {};
459460 systemd-nspawn = handleTest ./systemd-nspawn.nix {};
nixos/tests/systemd-networkd-dhcpserver-static-leases.nixnew file+81
@@ -0,0 +1,81 @@
1+# In contrast to systemd-networkd-dhcpserver, this test configures
2+# the router with a static DHCP lease for the client's MAC address.
3+import ./make-test-python.nix ({ lib, ... }: {
4+ name = "systemd-networkd-dhcpserver-static-leases";
5+ meta = with lib.maintainers; {
6+ maintainers = [ veehaitch tomfitzhenry ];
7+ };
8+ nodes = {
9+ router = {
10+ virtualisation.vlans = [ 1 ];
11+ systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
12+ networking = {
13+ useNetworkd = true;
14+ useDHCP = false;
15+ firewall.enable = false;
16+ };
17+ systemd.network = {
18+ networks = {
19+ # systemd-networkd will load the first network unit file
20+ # that matches, ordered lexiographically by filename.
21+ # /etc/systemd/network/{40-eth1,99-main}.network already
22+ # exists. This network unit must be loaded for the test,
23+ # however, hence why this network is named such.
24+ "01-eth1" = {
25+ name = "eth1";
26+ networkConfig = {
27+ DHCPServer = true;
28+ Address = "10.0.0.1/24";
29+ };
30+ dhcpServerStaticLeases = [{
31+ dhcpServerStaticLeaseConfig = {
32+ MACAddress = "02:de:ad:be:ef:01";
33+ Address = "10.0.0.10";
34+ };
35+ }];
36+ };
37+ };
38+ };
39+ };
40+
41+ client = {
42+ virtualisation.vlans = [ 1 ];
43+ systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
44+ networking = {
45+ useNetworkd = true;
46+ useDHCP = false;
47+ firewall.enable = false;
48+ interfaces.eth1 = {
49+ useDHCP = true;
50+ macAddress = "02:de:ad:be:ef:01";
51+ };
52+ };
53+
54+ # This setting is important to have the router assign the
55+ # configured lease based on the client's MAC address. Also see:
56+ # https://github.com/systemd/systemd/issues/21368#issuecomment-982193546
57+ systemd.network.networks."40-eth1".dhcpV4Config.ClientIdentifier = "mac";
58+ };
59+ };
60+ testScript = ''
61+ start_all()
62+
63+ with subtest("check router network configuration"):
64+ router.wait_for_unit("systemd-networkd-wait-online.service")
65+ eth1_status = router.succeed("networkctl status eth1")
66+ assert "Network File: /etc/systemd/network/01-eth1.network" in eth1_status, \
67+ "The router interface eth1 is not using the expected network file"
68+ assert "10.0.0.1" in eth1_status, "Did not find expected router IPv4"
69+
70+ with subtest("check client network configuration"):
71+ client.wait_for_unit("systemd-networkd-wait-online.service")
72+ eth1_status = client.succeed("networkctl status eth1")
73+ assert "Network File: /etc/systemd/network/40-eth1.network" in eth1_status, \
74+ "The client interface eth1 is not using the expected network file"
75+ assert "10.0.0.10" in eth1_status, "Did not find expected client IPv4"
76+
77+ with subtest("router and client can reach each other"):
78+ client.wait_until_succeeds("ping -c 5 10.0.0.1")
79+ router.wait_until_succeeds("ping -c 5 10.0.0.10")
80+ '';
81+})