2020-07-07 17:22:17 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.services.lightning-loop = {
|
2021-12-14 21:51:23 +03:00
|
|
|
enable = mkEnableOption "Lightning Loop, a non-custodial off/on chain bridge";
|
2020-10-29 23:20:36 +03:00
|
|
|
rpcAddress = mkOption {
|
|
|
|
type = types.str;
|
2023-07-29 19:58:55 +03:00
|
|
|
default = "127.0.0.1";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "Address to listen for gRPC connections.";
|
2020-10-29 23:20:36 +03:00
|
|
|
};
|
|
|
|
rpcPort = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 11010;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "Port to listen for gRPC connections.";
|
2020-10-29 23:20:36 +03:00
|
|
|
};
|
|
|
|
restAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = cfg.rpcAddress;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "Address to listen for REST connections.";
|
2020-10-29 23:20:36 +03:00
|
|
|
};
|
|
|
|
restPort = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8081;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "Port to listen for REST connections.";
|
2020-10-29 23:20:36 +03:00
|
|
|
};
|
2020-07-07 17:22:17 +03:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2020-11-10 00:09:09 +03:00
|
|
|
default = config.nix-bitcoin.pkgs.lightning-loop;
|
2021-12-08 06:07:28 +03:00
|
|
|
defaultText = "config.nix-bitcoin.pkgs.lightning-loop";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "The package providing lightning-loop binaries.";
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
2020-09-15 11:46:15 +03:00
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/lightning-loop";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "The data directory for lightning-loop.";
|
2020-09-15 11:46:15 +03:00
|
|
|
};
|
2020-07-07 17:22:17 +03:00
|
|
|
proxy = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2021-11-28 23:24:49 +03:00
|
|
|
default = if cfg.tor.proxy then config.nix-bitcoin.torClientAddressWithPort else null;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "`host:port` of SOCKS5 proxy for connnecting to the loop server.";
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
2022-07-07 17:08:28 +03:00
|
|
|
certificate = {
|
|
|
|
extraIPs = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "60.100.0.1" ];
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2022-07-07 17:08:28 +03:00
|
|
|
Extra `subjectAltName` IPs added to the certificate.
|
2022-12-18 15:13:47 +03:00
|
|
|
This works the same as loop option {option}`tlsextraip`.
|
2022-07-07 17:08:28 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraDomains = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "example.com" ];
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2022-07-07 17:08:28 +03:00
|
|
|
Extra `subjectAltName` domain names added to the certificate.
|
2022-12-18 15:13:47 +03:00
|
|
|
This works the same as loop option {option}`tlsextradomain`.
|
2022-07-07 17:08:28 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2020-09-15 11:46:15 +03:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
2020-07-07 17:22:17 +03:00
|
|
|
default = "";
|
2020-09-15 11:46:15 +03:00
|
|
|
example = ''
|
|
|
|
debuglevel=trace
|
|
|
|
'';
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2022-07-07 17:08:26 +03:00
|
|
|
Extra lines appended to the configuration file.
|
|
|
|
See here for all available options:
|
|
|
|
https://github.com/lightninglabs/loop/blob/11ab596080e9d36f1df43edbeba0702b25aa7457/loopd/config.go#L119
|
|
|
|
'';
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
|
|
|
cli = mkOption {
|
2022-11-09 00:45:19 +03:00
|
|
|
default = pkgs.writers.writeBashBin "loop" ''
|
2020-10-29 23:20:37 +03:00
|
|
|
${cfg.package}/bin/loop \
|
2021-10-01 12:51:57 +03:00
|
|
|
--rpcserver ${nbLib.addressWithPort cfg.rpcAddress cfg.rpcPort} \
|
2020-10-16 16:46:01 +03:00
|
|
|
--macaroonpath '${cfg.dataDir}/${network}/loop.macaroon' \
|
|
|
|
--tlscertpath '${secretsDir}/loop-cert' "$@"
|
2020-07-07 17:22:17 +03:00
|
|
|
'';
|
2021-12-08 06:07:28 +03:00
|
|
|
defaultText = "(See source)";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "Binary to connect with the lightning-loop instance.";
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
2021-11-28 23:24:49 +03:00
|
|
|
tor = nbLib.tor;
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
|
|
|
|
2021-09-13 14:40:47 +03:00
|
|
|
cfg = config.services.lightning-loop;
|
|
|
|
nbLib = config.nix-bitcoin.lib;
|
|
|
|
secretsDir = config.nix-bitcoin.secretsDir;
|
|
|
|
|
|
|
|
lnd = config.services.lnd;
|
|
|
|
|
|
|
|
network = config.services.bitcoind.network;
|
|
|
|
configFile = builtins.toFile "loop.conf" ''
|
|
|
|
datadir=${cfg.dataDir}
|
|
|
|
network=${network}
|
2021-10-01 12:51:57 +03:00
|
|
|
rpclisten=${cfg.rpcAddress}:${toString cfg.rpcPort}
|
2021-09-13 14:40:47 +03:00
|
|
|
restlisten=${cfg.restAddress}:${toString cfg.restPort}
|
|
|
|
logdir=${cfg.dataDir}/logs
|
|
|
|
tlscertpath=${secretsDir}/loop-cert
|
|
|
|
tlskeypath=${secretsDir}/loop-key
|
|
|
|
|
2021-10-01 12:51:57 +03:00
|
|
|
lnd.host=${nbLib.addressWithPort lnd.rpcAddress lnd.rpcPort}
|
2021-09-13 14:40:47 +03:00
|
|
|
lnd.macaroonpath=${lnd.networkDir}/admin.macaroon
|
|
|
|
lnd.tlspath=${lnd.certPath}
|
|
|
|
|
|
|
|
${optionalString (cfg.proxy != null) "server.proxy=${cfg.proxy}"}
|
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
2020-07-07 17:22:17 +03:00
|
|
|
config = mkIf cfg.enable {
|
2020-10-18 15:49:20 +03:00
|
|
|
services.lnd.enable = true;
|
2020-07-07 17:22:17 +03:00
|
|
|
|
|
|
|
environment.systemPackages = [ cfg.package (hiPrio cfg.cli) ];
|
|
|
|
|
2020-09-15 11:46:15 +03:00
|
|
|
systemd.tmpfiles.rules = [
|
2021-09-08 18:01:12 +03:00
|
|
|
"d '${cfg.dataDir}' 0770 ${lnd.user} ${lnd.group} - -"
|
2020-09-15 11:46:15 +03:00
|
|
|
];
|
|
|
|
|
2023-07-29 19:58:55 +03:00
|
|
|
services.lightning-loop.certificate.extraIPs = mkIf (cfg.rpcAddress != "127.0.0.1") [ "${cfg.rpcAddress}" ];
|
2022-07-07 17:08:28 +03:00
|
|
|
|
2020-07-07 17:22:17 +03:00
|
|
|
systemd.services.lightning-loop = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
requires = [ "lnd.service" ];
|
2023-10-03 14:00:23 +03:00
|
|
|
after = [ "lnd.service" "nix-bitcoin-secrets.target" ];
|
2021-02-04 00:44:41 +03:00
|
|
|
serviceConfig = nbLib.defaultHardening // {
|
2020-09-15 11:46:15 +03:00
|
|
|
ExecStart = "${cfg.package}/bin/loopd --configfile=${configFile}";
|
2021-09-08 18:01:12 +03:00
|
|
|
User = lnd.user;
|
2020-07-07 17:22:17 +03:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
2022-05-07 21:34:21 +03:00
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
2021-11-28 23:24:49 +03:00
|
|
|
} // nbLib.allowedIPAddresses cfg.tor.enforce;
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
2020-09-15 11:46:15 +03:00
|
|
|
|
|
|
|
nix-bitcoin.secrets = {
|
2021-09-08 18:01:12 +03:00
|
|
|
loop-key.user = lnd.user;
|
|
|
|
loop-cert.user = lnd.user;
|
2020-09-15 11:46:15 +03:00
|
|
|
};
|
2021-09-08 18:01:18 +03:00
|
|
|
nix-bitcoin.generateSecretsCmds.lightning-loop = ''
|
2022-07-07 17:08:28 +03:00
|
|
|
makeCert loop '${nbLib.mkCertExtraAltNames cfg.certificate}'
|
2021-09-08 18:01:18 +03:00
|
|
|
'';
|
2020-07-07 17:22:17 +03:00
|
|
|
};
|
|
|
|
}
|