I’m trying to write a NixOS module that is able to generate IP addresses and subnets within a defined IPv4 address space. I might give it a.b.c.d/N, and have it generate IP addresses corresponding to M bits’ worth of subnets and the remaining 32-N-M bits’ worth of host addresses.
For example, to clarify, given an address space of 192.168.0.0/23, and a 3-bit subnet allocation, I want a function that I could ask for host number 5 on subnet 3, and it would be able to tell me the IP address is 192.168.0.197 (or host 2 on subnet 4 would be 192.168.1.2 and so on).
Doing this in a language with bitwise operators would be pretty straightforward, but I’m unsure if there’s an obvious way to approach this in nix. Any hints?