From b8db72c4706dbac269f397f14739b8f5ea38a5bc Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Tue, 28 Apr 2026 21:39:19 +0200 Subject: [PATCH] bootstrap: Rewrite in Nu --- flake.nix | 1 + os/host/dev/seed/bootstrap/addresses.sql | 16 ++++++++------- os/host/dev/seed/bootstrap/default.nix | 21 ++++++++++---------- os/host/dev/seed/bootstrap/zone.jq | 22 --------------------- os/host/dev/seed/bootstrap/zone.nu | 25 ++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 39 deletions(-) delete mode 100644 os/host/dev/seed/bootstrap/zone.jq create mode 100755 os/host/dev/seed/bootstrap/zone.nu diff --git a/flake.nix b/flake.nix index 050eecb..c4a0f81 100644 --- a/flake.nix +++ b/flake.nix @@ -164,6 +164,7 @@ self.checks.${system}.pre-commit.enabledPackages ++ (with pkgs; [ dnscontrol + nushell ]); }; diff --git a/os/host/dev/seed/bootstrap/addresses.sql b/os/host/dev/seed/bootstrap/addresses.sql index d641137..7713953 100644 --- a/os/host/dev/seed/bootstrap/addresses.sql +++ b/os/host/dev/seed/bootstrap/addresses.sql @@ -1,12 +1,14 @@ -select json_object(a1.node, json_group_array (a2.value)) +select + node, + substr(value, 0, instr(value, ':')) as host, + substr(value, instr(value, ':') + 1) as port from - addresses a1 - inner join addresses a2 using (node) + addresses where - a1.banned = false and - a1.type != 'onion' and - unixepoch() - (a1.last_success / 1000) < (60 * 60 * 24 * 7) + banned = false and + type == 'dns' and + unixepoch() - (last_success / 1000) < (60 * 60 * 24 * 7) -group by a1.node +order by node \ No newline at end of file diff --git a/os/host/dev/seed/bootstrap/default.nix b/os/host/dev/seed/bootstrap/default.nix index 649bb6f..f1c87f6 100644 --- a/os/host/dev/seed/bootstrap/default.nix +++ b/os/host/dev/seed/bootstrap/default.nix @@ -8,22 +8,23 @@ services.radicle-bootstrap = { serviceConfig.Type = "oneshot"; script = '' - cat "${./addresses.sql}" | sqlite3 "/var/lib/radicle/node/node.db" | \ - jq --slurp add | \ - jq \ - --raw-output \ - --arg serviceName "_radicle-node.tcp" \ - --from-file "${./zone.jq}" \ - > "/etc/knot/dns-sd.zone" + nu ${./zone.nu} \ + --db "/var/lib/radicle/node/node.db" \ + --query ${./addresses.sql} \ + --zone "/etc/knot/dns-sd.zone" knotc -b zone-reload "bootstrap.radicle.network" ''; + after = ["systemd-resolved.service"]; wants = ["knot.service"]; - requires = ["knot.service"]; + requires = [ + "knot.service" + "systemd-resolved.service" + ]; path = with pkgs; [ - jq - sqlite knot-dns + nushell + config.systemd.package ]; }; timers.radicle-bootstrap = { diff --git a/os/host/dev/seed/bootstrap/zone.jq b/os/host/dev/seed/bootstrap/zone.jq deleted file mode 100644 index 6363bd7..0000000 --- a/os/host/dev/seed/bootstrap/zone.jq +++ /dev/null @@ -1,22 +0,0 @@ -to_entries -| .[] -| .key as $nid -| "_radicle-node._tcp" as $serviceName -| "\($nid).\($serviceName)" as $name -| [ - "\($serviceName) PTR \($name)", - - "\($name) TXT \"nid=\($nid)\"", - "\($name) TXT \"version=1\"", - "\($name) TXT \"network=main\"", - - (.value - | unique - | to_entries[] - | .key as $weight - | (.value | capture("(?[^:]+):(?[0-9]+)")) - | "\($nid).\($serviceName) SRV 0 \($weight) \(.port) \(.host)."), - - "" -] -| .[] diff --git a/os/host/dev/seed/bootstrap/zone.nu b/os/host/dev/seed/bootstrap/zone.nu new file mode 100755 index 0000000..a1324f9 --- /dev/null +++ b/os/host/dev/seed/bootstrap/zone.nu @@ -0,0 +1,25 @@ +#! /usr/bin/env nu + +def main [ + --db: path # Path of node database to query + --query: path # Path of query to run against database + --zone: path # Path of the zone file to (over)write +] { + open $db | + query db $"(open $query)" | + where {|row| + (^resolvectl query --cname=false $row.host | complete).exit_code == 0 + } | + each {|row| + let serviceName = "_radicle-node._tcp" + let name = $"($row.node).($serviceName)" + $" +($serviceName) PTR ($name) +($name) TXT \"nid=($row.node)\" +($name) TXT \"version=1\" +($name) TXT \"network=main\" +($name) SRV 0 1 ($row.port) ($row.host)." + } | + str join "\n" | + save -f $zone +}