Created
May 2, 2021 21:18
-
-
Save risicle/4aaad898902c821f535a98901d599615 to your computer and use it in GitHub Desktop.
jq expression to turn netplan yaml configs into nixos network configurations, supporting bonding and vlans
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.mtu) | .value |= {mtu: .mtu}) | |
) * ( | |
with_entries(select(.value.macaddress) | .value |= {macAddress: .macaddress}) | |
) | |
), | |
nameservers: .network[] | objects | .[] | select(.nameservers.addresses) | .nameservers.addresses | flatten | |
} | |
} |
Does it throw the same error when piping the json version of the plan? Indeed, it needs to be converted to json before feeding it to jq.
FWIW the place I use this is here https://github.com/risicle/nixos-infect-phoenixnap-bmc/blob/c997715589a70a0d974a3cb8f2001aa4fa59000e/nixos-infect#L146
Also note I've never used this with wifi so it will definitely need some work - note how it only cares about .network.ethernets
, .network.bonds
& .network.vlans
.
No not the same error when piping json but an error nevertheless.
So it looks like this is customized for your personal use cases, got it!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How should I run this?
plan.np:
Command:
cat plan.np | jq -f <(curl https://gist.githubusercontent.com/risicle/4aaad898902c821f535a98901d599615/raw/7326b35ddecb0e5747909d7d56b1941e6adccb38/netplan-to-nixos.jq)
Output: parse error: Invalid literal at line 1, column 8
Also throws error after piping
plan.np.json
(json version of the plan).