Skip to content

Instantly share code, notes, and snippets.

@johanot
Created January 7, 2019 15:33
Show Gist options
  • Select an option

  • Save johanot/7646d199950073d8f02e05893bacadb9 to your computer and use it in GitHub Desktop.

Select an option

Save johanot/7646d199950073d8f02e05893bacadb9 to your computer and use it in GitHub Desktop.
{
reservations =
let
program = pkgs.writeText "cidr-filter.py" ''
import ipaddress
import json
import sys
with open(sys.argv[1]) as f:
data = json.load(f)
net = ipaddress.ip_network(sys.argv[2])
out = []
for val in data:
host = ipaddress.ip_network(val["ip"])
if net.overlaps(host):
out.append(val)
print(json.dumps(out))
'';
filterCidr = with pkgs; runCommand "cidr-filter" { buildInputs = [ python3 python3Packages.ipaddress ]; } ''
mkdir -p $out
python ${toString program} "${writeText "input.json" (builtins.toJSON (lib.attrValues reservations))}" "${subnet}" >$out/out.json
'';
in
with builtins; map hostToKea (fromJSON (readFile "${filterCidr}/out.json"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment