Skip to content

Instantly share code, notes, and snippets.

@risicle
Created November 18, 2023 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save risicle/3aea6fbd26c99d2e7a1010882bb5886d to your computer and use it in GitHub Desktop.
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.
{
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