Created
November 18, 2023 22:03
-
-
Save risicle/3aea6fbd26c99d2e7a1010882bb5886d to your computer and use it in GitHub Desktop.
`jq` script to convert a netplan config file (converted from yaml) to nixos-importable json. Does not support all netplan features obviously.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
systemd: { | |
network: { | |
netdevs: ( | |
.network.ethernets | with_entries(.value |= { | |
netdevConfig: {Name: ."set-name"}, | |
matchConfig: {MACAddress: .match.macaddress} | |
}) | |
) | |
} | |
}, | |
networking: { | |
bonds: ( | |
.network.bonds | with_entries(.value |= { | |
interfaces: .interfaces, | |
driverOptions: { | |
mode: .parameters.mode | tostring, | |
downdelay: .parameters."down-delay" | tostring, | |
lacp_rate: .parameters."lacp-rate" | tostring, | |
miimon: .parameters."mii-monitor-interval" | tostring, | |
xmit_hash_policy: .parameters."transmit-hash-policy" | tostring, | |
updelay: .parameters."up-delay" | tostring | |
} | |
}) | |
), | |
vlans: ( | |
.network.vlans | with_entries(.value |= {id: .id, interface: .link}) | |
), | |
interfaces: ( | |
(.network.ethernets + .network.bonds + .network.vlans) | ( | |
with_entries(select(.value.addresses) | .value |= { | |
ipv4: { | |
addresses: [ | |
.addresses[] | split("/") | { | |
address: .[0], | |
prefixLength: .[1] | tonumber | |
} | |
] | |
} | |
}) | |
) * ( | |
with_entries(select(.value.gateway4) | .value |= { | |
ipv4: { | |
routes: [{address: "0.0.0.0", prefixLength: 0, via: .gateway4}] | |
} | |
}) | |
) * ( | |
with_entries(select(.value.routes) | .value |= { | |
ipv4: { | |
routes: [ | |
.routes[] | ( | |
{ | |
address: (if | |
.to == "default" | |
then | |
"0.0.0.0" | |
else | |
.to | |
end | split("/")[0]), | |
prefixLength: (if | |
.to == "default" | |
then | |
0 | |
else | |
(if | |
.to | contains("/") | |
then | |
.to | split("/")[1] | tonumber | |
else | |
0 | |
end) | |
end), | |
} | |
+ (if .via then {via: .via} else {} end) | |
+ (if .type then {type: .type} else {} end) | |
) | |
] | |
} | |
}) | |
) * ( | |
with_entries(select(.value.mtu) | .value |= {mtu: .mtu}) | |
) * ( | |
with_entries(select(.value.macaddress) | .value |= {macAddress: .macaddress}) | |
) | |
), | |
nameservers: .network[] | objects | .[] | select(.nameservers.addresses) | .nameservers.addresses | flatten | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment