Skip to content

Instantly share code, notes, and snippets.

@risicle
Created May 2, 2021 21:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save risicle/4aaad898902c821f535a98901d599615 to your computer and use it in GitHub Desktop.
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
{
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
}
}
@asdf8dfafjk
Copy link

How should I run this?

plan.np:

network:
  version: 2
  renderer: networkd
  wifis:
    wlp2s0b1:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.0.21/24]
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]
      access-points:
        "network_ssid_name":
            password: "**********"
      routes:
        - to: default
          via: 192.168.0.1

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).

@risicle
Copy link
Author

risicle commented Oct 8, 2022

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.

@risicle
Copy link
Author

risicle commented Oct 8, 2022

@risicle
Copy link
Author

risicle commented Oct 8, 2022

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.

@asdf8dfafjk
Copy link

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