|
75 | 75 | source /xnode-config/env |
76 | 76 | ''; |
77 | 77 |
|
| 78 | + # https://github.com/nix-community/nixos-images/blob/main/nix/kexec-installer/restore_routes.py |
| 79 | + networking.firewall.enable = false; |
| 80 | + networking.useNetworkd = true; |
| 81 | + systemd.network.enable = true; |
78 | 82 | systemd.services.apply-network-config = { |
79 | 83 | wantedBy = [ "multi-user.target" ]; |
80 | 84 | description = "Apply run time provided network config."; |
81 | | - wants = [ "network-online.target" ]; |
82 | | - after = [ "network-online.target" ]; |
| 85 | + wants = [ "network-pre.target" ]; |
| 86 | + before = [ "network-pre.target" ]; |
83 | 87 | serviceConfig = { |
84 | 88 | Type = "oneshot"; |
85 | 89 | User = "root"; |
|
94 | 98 | # Extract environmental variables |
95 | 99 | source /xnode-config/env |
96 | 100 |
|
| 101 | + output="/etc/systemd/network" |
97 | 102 | if [[ $NETWORK_CONFIG ]]; then |
98 | 103 | interfaces=$(echo "$NETWORK_CONFIG" | jq -c '.address.[]') |
99 | 104 | routes=$(echo "$NETWORK_CONFIG" | jq -c '.route.[]') |
100 | 105 | for iface in $interfaces; do |
101 | 106 | mac=$(echo "$iface" | jq -r '.address') |
102 | | - og_name=$(echo "$iface" | jq -r '.ifname') |
103 | | - name=$(grep -l "$mac" /sys/class/net/*/address | sed 's|/sys/class/net/\(.*\)/address|\1|') |
| 107 | + name=$(echo "$iface" | jq -r '.ifname') |
| 108 | + systemd="''${output}/00-''${mac}.network" |
| 109 | +
|
| 110 | + cat << EOF > "$systemd" |
| 111 | + [Match] |
| 112 | + MACAddress = $mac |
| 113 | +
|
| 114 | + [Network] |
| 115 | + DHCP = yes |
| 116 | + LLDP = yes |
| 117 | + IPv6AcceptRA = yes |
| 118 | + MulticastDNS = yes |
| 119 | + EOF |
104 | 120 |
|
105 | 121 | addresses=$(echo "$iface" | jq -c '.addr_info[]') |
106 | 122 | for address in $addresses; do |
|
111 | 127 | continue |
112 | 128 | fi |
113 | 129 |
|
114 | | - config="$(echo $address | jq -r '.local')/$(echo $address | jq -r '.prefixlen')" |
115 | | - ip address add $config dev $name |
116 | | - done |
| 130 | + ip="$(echo $address | jq -r '.local')/$(echo $address | jq -r '.prefixlen')" |
117 | 131 |
|
118 | | - ip link set $name up |
| 132 | + cat << EOF >> "$systemd" |
| 133 | + Address = $ip |
| 134 | + EOF |
| 135 | + done |
119 | 136 |
|
120 | 137 | for route in $routes; do |
121 | 138 | protocol=$(echo "$route" | jq -r '.protocol') |
122 | 139 | dev=$(echo "$route" | jq -r '.dev') |
123 | 140 |
|
124 | | - if [ "$protocol" != "static" ] || [ "$dev" != "$og_name" ]; then |
| 141 | + if [ "$protocol" != "static" ] || [ "$dev" != "$name" ]; then |
125 | 142 | continue |
126 | 143 | fi |
127 | 144 |
|
128 | | - args="" |
| 145 | + onlink="no" |
129 | 146 | flags=$(echo "$route" | jq -r '.flags') |
130 | 147 | if [[ $flags == *"onlink"* ]]; then |
131 | | - args="$args onlink" |
| 148 | + onlink="yes" |
132 | 149 | fi |
133 | 150 |
|
134 | 151 | destination=$(echo $route | jq -r '.dst') |
| 152 | + if [ "$destination" == "default" ]; then |
| 153 | + destination="0.0.0.0/0" |
| 154 | + fi |
135 | 155 | gateway=$(echo $route | jq -r '.gateway') |
136 | | - ip route add $destination via $gateway $args dev $name |
| 156 | +
|
| 157 | + cat << EOF >> "$systemd" |
| 158 | +
|
| 159 | + [Route] |
| 160 | + Destination = $destination |
| 161 | + Gateway = $gateway |
| 162 | + GatewayOnLink = $onlink |
| 163 | + EOF |
137 | 164 | done |
138 | 165 | done |
139 | 166 | fi |
|
0 commit comments