2020-05-03 07:42:53 -07:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
2021-09-13 04:40:47 -07:00
|
|
|
options = {
|
|
|
|
nix-bitcoin.nodeinfo = {
|
|
|
|
enable = mkEnableOption "nodeinfo";
|
2022-07-04 03:04:42 -07:00
|
|
|
|
2021-09-13 04:40:47 -07:00
|
|
|
program = mkOption {
|
|
|
|
readOnly = true;
|
|
|
|
default = script;
|
2021-12-07 19:07:27 -08:00
|
|
|
defaultText = "(See source)";
|
2021-09-13 04:40:47 -07:00
|
|
|
};
|
2022-07-04 03:04:42 -07:00
|
|
|
|
|
|
|
services = mkOption {
|
|
|
|
internal = true;
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
defaultText = "(See source)";
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc ''
|
2022-07-04 03:04:42 -07:00
|
|
|
Nodeinfo service definitions.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-10-22 10:37:57 -07:00
|
|
|
lib = mkOption {
|
2022-07-04 03:04:42 -07:00
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
|
|
|
default = nodeinfoLib;
|
|
|
|
defaultText = "(See source)";
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc ''
|
2022-07-04 03:04:42 -07:00
|
|
|
Helper functions for defining nodeinfo services.
|
|
|
|
'';
|
|
|
|
};
|
2021-09-13 04:40:47 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-14 04:24:26 -08:00
|
|
|
cfg = config.nix-bitcoin.nodeinfo;
|
2021-10-01 02:51:57 -07:00
|
|
|
nbLib = config.nix-bitcoin.lib;
|
2021-01-14 04:24:26 -08:00
|
|
|
|
2020-05-03 07:42:53 -07:00
|
|
|
script = pkgs.writeScriptBin "nodeinfo" ''
|
2021-01-14 04:24:26 -08:00
|
|
|
#!${pkgs.python3}/bin/python
|
|
|
|
|
|
|
|
import json
|
|
|
|
import subprocess
|
2021-11-02 20:25:55 -07:00
|
|
|
import sys
|
2021-01-14 04:24:26 -08:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
def success(*args):
|
|
|
|
return subprocess.call(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
|
|
|
|
|
|
|
|
def is_active(unit):
|
|
|
|
return success("systemctl", "is-active", "--quiet", unit)
|
|
|
|
|
|
|
|
def is_enabled(unit):
|
|
|
|
return success("systemctl", "is-enabled", "--quiet", unit)
|
|
|
|
|
|
|
|
def cmd(*args):
|
|
|
|
return subprocess.run(args, stdout=subprocess.PIPE).stdout.decode('utf-8')
|
|
|
|
|
|
|
|
def shell(*args):
|
|
|
|
return cmd("bash", "-c", *args).strip()
|
|
|
|
|
|
|
|
infos = OrderedDict()
|
|
|
|
operator = "${config.nix-bitcoin.operator.name}"
|
|
|
|
|
|
|
|
def set_onion_address(info, name, port):
|
|
|
|
path = f"/var/lib/onion-addresses/{operator}/{name}"
|
|
|
|
try:
|
|
|
|
with open(path, "r") as f:
|
|
|
|
onion_address = f.read().strip()
|
|
|
|
except OSError:
|
|
|
|
print(f"error reading file {path}", file=sys.stderr)
|
|
|
|
return
|
|
|
|
info["onion_address"] = f"{onion_address}:{port}"
|
|
|
|
|
2023-01-20 04:45:10 -08:00
|
|
|
def add_service(service, make_info, systemd_service = None):
|
|
|
|
systemd_service = systemd_service or service
|
|
|
|
if not is_active(systemd_service):
|
|
|
|
infos[service] = f"'{systemd_service}.service' is not running"
|
2021-01-14 04:24:26 -08:00
|
|
|
else:
|
|
|
|
info = OrderedDict()
|
|
|
|
exec(make_info, globals(), locals())
|
|
|
|
infos[service] = info
|
|
|
|
|
|
|
|
if is_enabled("onion-adresses") and not is_active("onion-adresses"):
|
|
|
|
print("error: service 'onion-adresses' is not running")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
${concatStrings infos}
|
|
|
|
|
|
|
|
print(json.dumps(infos, indent=2))
|
2020-05-03 07:42:53 -07:00
|
|
|
'';
|
2021-01-14 04:24:26 -08:00
|
|
|
|
2022-07-04 03:04:42 -07:00
|
|
|
infos = map (serviceName:
|
|
|
|
let serviceCfg = config.services.${serviceName};
|
|
|
|
in optionalString serviceCfg.enable (cfg.services.${serviceName} serviceName serviceCfg)
|
|
|
|
) (builtins.attrNames cfg.services);
|
2021-01-14 04:24:26 -08:00
|
|
|
|
2022-07-04 03:04:42 -07:00
|
|
|
nodeinfoLib = rec {
|
2023-01-20 04:45:10 -08:00
|
|
|
mkInfo = extraCode: name: cfg:
|
|
|
|
mkInfoLong {
|
|
|
|
inherit extraCode name cfg;
|
|
|
|
};
|
|
|
|
|
|
|
|
mkInfoLong = { extraCode ? "", name, cfg, systemdServiceName ? name }: ''
|
2021-01-14 04:24:26 -08:00
|
|
|
add_service("${name}", """
|
2021-10-01 02:51:57 -07:00
|
|
|
info["local_address"] = "${nbLib.addressWithPort cfg.address cfg.port}"
|
2021-01-14 04:24:26 -08:00
|
|
|
'' + mkIfOnionPort name (onionPort: ''
|
|
|
|
set_onion_address(info, "${name}", ${onionPort})
|
|
|
|
'') + extraCode + ''
|
|
|
|
|
2023-01-20 04:45:10 -08:00
|
|
|
""", "${systemdServiceName}")
|
2021-01-14 04:24:26 -08:00
|
|
|
'';
|
|
|
|
|
2022-07-04 03:04:42 -07:00
|
|
|
mkIfOnionPort = name: fn:
|
|
|
|
if onionServices ? ${name} then
|
|
|
|
fn (toString (builtins.elemAt onionServices.${name}.map 0).port)
|
|
|
|
else
|
|
|
|
"";
|
|
|
|
};
|
2021-01-14 04:24:26 -08:00
|
|
|
|
2021-08-15 02:28:41 -07:00
|
|
|
inherit (config.services.tor.relay) onionServices;
|
2020-05-03 07:42:53 -07:00
|
|
|
in {
|
2021-09-13 04:40:47 -07:00
|
|
|
inherit options;
|
2020-05-03 07:42:53 -07:00
|
|
|
|
|
|
|
config = {
|
2021-01-14 04:24:26 -08:00
|
|
|
environment.systemPackages = optional cfg.enable script;
|
2022-07-04 03:04:42 -07:00
|
|
|
|
|
|
|
nix-bitcoin.nodeinfo.services = with nodeinfoLib; {
|
|
|
|
bitcoind = mkInfo "";
|
|
|
|
clightning = mkInfo ''
|
|
|
|
info["nodeid"] = shell("lightning-cli getinfo | jq -r '.id'")
|
|
|
|
if 'onion_address' in info:
|
|
|
|
info["id"] = f"{info['nodeid']}@{info['onion_address']}"
|
|
|
|
'';
|
2023-03-09 12:43:09 -08:00
|
|
|
lnd = name: cfg: mkInfo ''
|
|
|
|
info["rest_address"] = "${nbLib.addressWithPort cfg.restAddress cfg.restPort}"
|
2022-07-04 03:04:42 -07:00
|
|
|
info["nodeid"] = shell("lncli getinfo | jq -r '.identity_pubkey'")
|
2023-03-09 12:43:09 -08:00
|
|
|
'' name cfg;
|
2022-07-04 03:04:42 -07:00
|
|
|
clightning-rest = mkInfo "";
|
|
|
|
electrs = mkInfo "";
|
2022-07-04 03:15:44 -07:00
|
|
|
fulcrum = mkInfo "";
|
2022-07-04 03:04:42 -07:00
|
|
|
spark-wallet = mkInfo "";
|
|
|
|
btcpayserver = mkInfo "";
|
|
|
|
liquidd = mkInfo "";
|
|
|
|
joinmarket-ob-watcher = mkInfo "";
|
|
|
|
rtl = mkInfo "";
|
|
|
|
# Only add sshd when it has an onion service
|
|
|
|
sshd = name: cfg: mkIfOnionPort "sshd" (onionPort: ''
|
|
|
|
add_service("sshd", """set_onion_address(info, "sshd", ${onionPort})""")
|
|
|
|
'');
|
|
|
|
};
|
2020-05-03 07:42:53 -07:00
|
|
|
};
|
|
|
|
}
|