Files
infra/lib/zerotier-interface.nix
2025-12-01 14:44:10 +07:00

30 lines
702 B
Nix

nwid:
{ pkgs, ... }:
let
ztInterfaceScript = pkgs.writeShellScript "zt-interface" ''
#!/usr/bin/env bash
network="$1"
index=${"2:-0"}
network="0x$network"
# zerotier hash function
network=$(( (($network ^ ($network >> (3*8))) + $index ) % (1<<(5*8)) ))
# encode the result in base32
while [ $network -gt 0 ]; do
index=$(( $network % (1<<5) + 24 ))
[ $index -lt 50 ] && index=$(( $index + 73 ))
result="$(printf "\\%03o" $index)$result"
network=$(( $network >> 5 ))
done
echo "zt$(printf %b "$result")"
'';
result = pkgs.runCommand "zt-interface-output" { } ''
${ztInterfaceScript} > $out
'';
in
builtins.readFile result