2021-11-08 14:43:14 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.services.rtl = {
|
2021-12-14 21:51:23 +03:00
|
|
|
enable = mkEnableOption "Ride The Lightning, a web interface for lnd and clightning ";
|
2021-11-08 14:43:14 +03:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
description = "HTTP server address.";
|
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 3000;
|
|
|
|
description = "HTTP server port.";
|
|
|
|
};
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/rtl";
|
|
|
|
description = "The data directory for RTL.";
|
|
|
|
};
|
|
|
|
nodes = {
|
|
|
|
clightning = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2021-11-26 17:13:30 +03:00
|
|
|
description = "Enable the clightning node interface.";
|
2021-11-08 14:43:14 +03:00
|
|
|
};
|
|
|
|
lnd = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2021-11-26 17:13:30 +03:00
|
|
|
description = "Enable the lnd node interface.";
|
2021-11-08 14:43:14 +03:00
|
|
|
};
|
|
|
|
reverseOrder = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Reverse the order of nodes shown in the UI.
|
|
|
|
By default, clightning is shown before lnd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
loop = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether to enable swaps with lightning-loop.";
|
|
|
|
};
|
|
|
|
nightTheme = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Enable the Night UI Theme.";
|
|
|
|
};
|
2021-11-26 17:13:37 +03:00
|
|
|
extraCurrency = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
example = "USD";
|
|
|
|
description = ''
|
|
|
|
Currency code (ISO 4217) of the extra currency used for displaying balances.
|
|
|
|
When set, this option enables online currency rate fetching.
|
|
|
|
Warning: Rate fetching requires outgoing clearnet connections, so option
|
|
|
|
`tor.enforce` is automatically disabled.
|
|
|
|
'';
|
|
|
|
};
|
2021-11-26 17:13:30 +03:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "rtl";
|
|
|
|
description = "The user as which to run RTL.";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = cfg.user;
|
|
|
|
description = "The group as which to run RTL.";
|
|
|
|
};
|
2021-11-28 23:24:49 +03:00
|
|
|
tor.enforce = nbLib.tor.enforce;
|
2021-11-08 14:43:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
cfg = config.services.rtl;
|
|
|
|
nbLib = config.nix-bitcoin.lib;
|
2021-12-14 21:51:22 +03:00
|
|
|
nbPkgs = config.nix-bitcoin.pkgs;
|
2021-11-08 14:43:14 +03:00
|
|
|
secretsDir = config.nix-bitcoin.secretsDir;
|
|
|
|
|
|
|
|
node = { isLnd, index }: ''
|
|
|
|
{
|
|
|
|
"index": ${toString index},
|
|
|
|
"lnNode": "Node",
|
|
|
|
"lnImplementation": "${if isLnd then "LND" else "CLT"}",
|
|
|
|
"Authentication": {
|
|
|
|
${optionalString (isLnd && cfg.loop)
|
|
|
|
''"swapMacaroonPath": "${lightning-loop.dataDir}/${bitcoind.network}",''
|
|
|
|
}
|
|
|
|
"macaroonPath": "${if isLnd
|
|
|
|
then "${cfg.dataDir}/macaroons"
|
2022-05-05 22:56:16 +03:00
|
|
|
else "${clightning-rest.dataDir}/certs"
|
2021-11-08 14:43:14 +03:00
|
|
|
}"
|
|
|
|
},
|
|
|
|
"Settings": {
|
|
|
|
"userPersona": "OPERATOR",
|
|
|
|
"themeMode": "${if cfg.nightTheme then "NIGHT" else "DAY"}",
|
|
|
|
"themeColor": "PURPLE",
|
|
|
|
${optionalString isLnd
|
|
|
|
''"channelBackupPath": "${cfg.dataDir}/backup/lnd",''
|
|
|
|
}
|
|
|
|
"logLevel": "INFO",
|
2021-11-26 17:13:37 +03:00
|
|
|
"fiatConversion": ${if cfg.extraCurrency == null then "false" else "true"},
|
|
|
|
${optionalString (cfg.extraCurrency != null)
|
|
|
|
''"currencyUnit": "${cfg.extraCurrency}",''
|
|
|
|
}
|
2021-11-08 14:43:14 +03:00
|
|
|
${optionalString (isLnd && cfg.loop)
|
|
|
|
''"swapServerUrl": "https://${nbLib.addressWithPort lightning-loop.restAddress lightning-loop.restPort}",''
|
|
|
|
}
|
|
|
|
"lnServerUrl": "https://${
|
|
|
|
if isLnd
|
|
|
|
then nbLib.addressWithPort lnd.restAddress lnd.restPort
|
2022-05-05 22:56:16 +03:00
|
|
|
else nbLib.addressWithPort clightning-rest.address clightning-rest.port
|
2021-11-08 14:43:14 +03:00
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
nodes' = optional cfg.nodes.clightning (node { isLnd = false; index = 1; }) ++
|
|
|
|
optional cfg.nodes.lnd (node { isLnd = true; index = 2; });
|
|
|
|
|
|
|
|
nodes = if cfg.nodes.reverseOrder then reverseList nodes' else nodes';
|
|
|
|
|
|
|
|
configFile = builtins.toFile "config" ''
|
|
|
|
{
|
|
|
|
"multiPass": "@multiPass@",
|
|
|
|
"host": "${cfg.address}",
|
|
|
|
"port": "${toString cfg.port}",
|
|
|
|
"SSO": {
|
|
|
|
"rtlSSO": 0
|
|
|
|
},
|
|
|
|
"nodes": [
|
|
|
|
${builtins.concatStringsSep ",\n" nodes}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
inherit (config.services)
|
|
|
|
bitcoind
|
|
|
|
lnd
|
2022-05-05 22:56:16 +03:00
|
|
|
clightning-rest
|
2021-11-08 14:43:14 +03:00
|
|
|
lightning-loop;
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.nodes.clightning || cfg.nodes.lnd;
|
|
|
|
message = ''
|
|
|
|
RTL: At least one of `nodes.lnd` or `nodes.clightning` must be `true`.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.lnd.enable = mkIf cfg.nodes.lnd true;
|
|
|
|
services.lightning-loop.enable = mkIf cfg.loop true;
|
2022-05-05 22:56:16 +03:00
|
|
|
services.clightning-rest.enable = mkIf cfg.nodes.clightning true;
|
2021-11-08 14:43:14 +03:00
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -"
|
|
|
|
];
|
|
|
|
|
2021-11-26 17:13:37 +03:00
|
|
|
services.rtl.tor.enforce = mkIf (cfg.extraCurrency != null) false;
|
|
|
|
|
2021-11-08 14:43:14 +03:00
|
|
|
systemd.services.rtl = rec {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-05-05 22:56:16 +03:00
|
|
|
requires = optional cfg.nodes.clightning "clightning-rest.service" ++
|
2021-11-08 14:43:14 +03:00
|
|
|
optional cfg.nodes.lnd "lnd.service";
|
|
|
|
after = requires;
|
|
|
|
environment.RTL_CONFIG_PATH = cfg.dataDir;
|
|
|
|
serviceConfig = nbLib.defaultHardening // {
|
|
|
|
ExecStartPre = [
|
|
|
|
(nbLib.script "rtl-setup-config" ''
|
|
|
|
<${configFile} sed "s|@multiPass@|$(cat ${secretsDir}/rtl-password)|" \
|
|
|
|
> '${cfg.dataDir}/RTL-Config.json'
|
|
|
|
'')
|
|
|
|
] ++ optional cfg.nodes.lnd
|
|
|
|
(nbLib.rootScript "rtl-copy-macaroon" ''
|
|
|
|
install -D -o ${cfg.user} -g ${cfg.group} ${lnd.networkDir}/admin.macaroon \
|
|
|
|
'${cfg.dataDir}/macaroons/admin.macaroon'
|
|
|
|
'');
|
2021-12-14 21:51:22 +03:00
|
|
|
ExecStart = "${nbPkgs.rtl}/bin/rtl";
|
2021-11-08 14:43:14 +03:00
|
|
|
# Show "rtl" instead of "node" in the journal
|
|
|
|
SyslogIdentifier = "rtl";
|
|
|
|
User = cfg.user;
|
|
|
|
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
|
2021-11-08 14:43:14 +03:00
|
|
|
// nbLib.nodejs;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.${cfg.user} = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = cfg.group;
|
|
|
|
extraGroups =
|
2022-05-05 22:56:16 +03:00
|
|
|
# Reads cert and macaroon from the clightning-rest datadir
|
|
|
|
optional cfg.nodes.clightning clightning-rest.group ++
|
2021-11-08 14:43:14 +03:00
|
|
|
optional cfg.loop lnd.group;
|
|
|
|
};
|
|
|
|
users.groups.${cfg.group} = {};
|
|
|
|
|
|
|
|
nix-bitcoin.secrets.rtl-password.user = cfg.user;
|
|
|
|
nix-bitcoin.generateSecretsCmds.rtl = ''
|
|
|
|
makePasswordSecret rtl-password
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|