2021-11-08 11:43:14 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.services.rtl = {
|
2022-05-18 11:33:45 +00:00
|
|
|
enable = mkEnableOption "Ride The Lightning, a web interface for lnd and clightning";
|
2021-11-08 11:43:14 +00:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "HTTP server address.";
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 3000;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "HTTP server port.";
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/rtl";
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "The data directory for RTL.";
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
|
|
|
nodes = {
|
2022-05-14 13:21:36 +00:00
|
|
|
clightning = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "Enable the clightning node interface.";
|
2022-05-14 13:21:36 +00:00
|
|
|
};
|
2022-05-14 13:21:37 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
Settings.userPersona = "MERCHANT";
|
|
|
|
Settings.logLevel = "DEBUG";
|
|
|
|
};
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2022-05-14 13:21:37 +00:00
|
|
|
Extra clightning node configuration.
|
|
|
|
See here for all available options:
|
|
|
|
https://github.com/Ride-The-Lightning/RTL/blob/master/.github/docs/Application_configurations.md
|
|
|
|
'';
|
|
|
|
};
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
2022-05-14 13:21:36 +00:00
|
|
|
lnd = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "Enable the lnd node interface.";
|
2022-05-14 13:21:36 +00:00
|
|
|
};
|
|
|
|
loop = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "Enable swaps with lightning-loop.";
|
2022-05-14 13:21:36 +00:00
|
|
|
};
|
2022-05-14 13:21:37 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
Settings.userPersona = "MERCHANT";
|
|
|
|
Settings.logLevel = "DEBUG";
|
|
|
|
};
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2022-05-14 13:21:37 +00:00
|
|
|
Extra lnd node configuration.
|
|
|
|
See here for all available options:
|
|
|
|
https://github.com/Ride-The-Lightning/RTL/blob/master/.github/docs/Application_configurations.md
|
|
|
|
'';
|
|
|
|
};
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
|
|
|
reverseOrder = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2021-11-08 11:43:14 +00:00
|
|
|
Reverse the order of nodes shown in the UI.
|
|
|
|
By default, clightning is shown before lnd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
nightTheme = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "Enable the Night UI Theme.";
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
2021-11-26 14:13:37 +00:00
|
|
|
extraCurrency = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
example = "USD";
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2021-11-26 14:13:37 +00:00
|
|
|
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
|
2022-12-18 12:13:47 +00:00
|
|
|
{option}`tor.enforce` is automatically disabled.
|
2021-11-26 14:13:37 +00:00
|
|
|
'';
|
|
|
|
};
|
2021-11-26 14:13:30 +00:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "rtl";
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "The user as which to run RTL.";
|
2021-11-26 14:13:30 +00:00
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = cfg.user;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "The group as which to run RTL.";
|
2021-11-26 14:13:30 +00:00
|
|
|
};
|
2021-11-28 20:24:49 +00:00
|
|
|
tor.enforce = nbLib.tor.enforce;
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cfg = config.services.rtl;
|
|
|
|
nbLib = config.nix-bitcoin.lib;
|
2021-12-14 18:51:22 +00:00
|
|
|
nbPkgs = config.nix-bitcoin.pkgs;
|
2021-11-08 11:43:14 +00:00
|
|
|
secretsDir = config.nix-bitcoin.secretsDir;
|
|
|
|
|
2022-05-14 13:21:37 +00:00
|
|
|
inherit (nbLib) optionalAttr;
|
2021-11-08 11:43:14 +00:00
|
|
|
|
2022-05-14 13:21:37 +00:00
|
|
|
node = { isLnd, index }: {
|
|
|
|
inherit index;
|
|
|
|
lnNode = "Node";
|
|
|
|
lnImplementation = if isLnd then "LND" else "CLT";
|
|
|
|
Authentication = {
|
|
|
|
${optionalAttr (isLnd && lndLoopEnabled) "swapMacaroonPath"} = "${lightning-loop.dataDir}/${bitcoind.network}";
|
|
|
|
macaroonPath = if isLnd
|
|
|
|
then "${cfg.dataDir}/macaroons"
|
|
|
|
else "${clightning-rest.dataDir}/certs";
|
|
|
|
};
|
|
|
|
Settings = {
|
|
|
|
userPersona = "OPERATOR";
|
|
|
|
themeMode = if cfg.nightTheme then "NIGHT" else "DAY";
|
|
|
|
themeColor = "PURPLE";
|
|
|
|
${optionalAttr isLnd "channelBackupPath"} = "${cfg.dataDir}/backup/lnd";
|
|
|
|
logLevel = "INFO";
|
|
|
|
fiatConversion = cfg.extraCurrency != null;
|
|
|
|
${optionalAttr (cfg.extraCurrency != null) "currencyUnit"} = cfg.extraCurrency;
|
|
|
|
${optionalAttr (isLnd && lndLoopEnabled) "swapServerUrl"} =
|
|
|
|
"https://${nbLib.addressWithPort lightning-loop.restAddress lightning-loop.restPort}";
|
|
|
|
lnServerUrl = "https://${
|
|
|
|
if isLnd
|
|
|
|
then nbLib.addressWithPort lnd.restAddress lnd.restPort
|
|
|
|
else nbLib.addressWithPort clightning-rest.address clightning-rest.port
|
|
|
|
}";
|
|
|
|
};
|
|
|
|
};
|
2021-11-08 11:43:14 +00:00
|
|
|
|
2022-05-14 13:21:37 +00:00
|
|
|
nodes' =
|
|
|
|
optional cfg.nodes.clightning.enable
|
|
|
|
(recursiveUpdate (node { isLnd = false; index = 1; }) cfg.nodes.clightning.extraConfig) ++
|
|
|
|
optional cfg.nodes.lnd.enable
|
|
|
|
(recursiveUpdate (node { isLnd = true; index = 2; }) cfg.nodes.lnd.extraConfig);
|
2021-11-08 11:43:14 +00:00
|
|
|
|
|
|
|
nodes = if cfg.nodes.reverseOrder then reverseList nodes' else nodes';
|
|
|
|
|
2022-05-14 13:21:37 +00:00
|
|
|
rtlConfig = {
|
|
|
|
multiPass = "@multiPass@";
|
|
|
|
host = cfg.address;
|
|
|
|
port = cfg.port;
|
|
|
|
SSO.rtlSSO = 0;
|
|
|
|
inherit nodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = builtins.toFile "config" (builtins.toJSON rtlConfig);
|
2021-11-08 11:43:14 +00:00
|
|
|
|
|
|
|
inherit (config.services)
|
|
|
|
bitcoind
|
|
|
|
lnd
|
2022-05-05 19:56:16 +00:00
|
|
|
clightning-rest
|
2021-11-08 11:43:14 +00:00
|
|
|
lightning-loop;
|
2022-05-14 13:21:36 +00:00
|
|
|
|
|
|
|
lndLoopEnabled = cfg.nodes.lnd.enable && cfg.nodes.lnd.loop;
|
2021-11-08 11:43:14 +00:00
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
2022-05-14 13:21:36 +00:00
|
|
|
{ assertion = cfg.nodes.clightning.enable || cfg.nodes.lnd.enable;
|
2021-11-08 11:43:14 +00:00
|
|
|
message = ''
|
2022-05-14 13:21:36 +00:00
|
|
|
RTL: At least one of `nodes.lnd.enable` or `nodes.clightning.enable` must be `true`.
|
2021-11-08 11:43:14 +00:00
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2022-05-14 13:21:36 +00:00
|
|
|
services.lnd.enable = mkIf cfg.nodes.lnd.enable true;
|
|
|
|
services.lightning-loop.enable = mkIf lndLoopEnabled true;
|
|
|
|
services.clightning-rest.enable = mkIf cfg.nodes.clightning.enable true;
|
2021-11-08 11:43:14 +00:00
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -"
|
|
|
|
];
|
|
|
|
|
2021-11-26 14:13:37 +00:00
|
|
|
services.rtl.tor.enforce = mkIf (cfg.extraCurrency != null) false;
|
|
|
|
|
2021-11-08 11:43:14 +00:00
|
|
|
systemd.services.rtl = rec {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-05-14 13:21:36 +00:00
|
|
|
requires = optional cfg.nodes.clightning.enable "clightning-rest.service" ++
|
|
|
|
optional cfg.nodes.lnd.enable "lnd.service";
|
2021-11-08 11:43:14 +00:00
|
|
|
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'
|
|
|
|
'')
|
2022-05-14 13:21:36 +00:00
|
|
|
] ++ optional cfg.nodes.lnd.enable
|
2021-11-08 11:43:14 +00:00
|
|
|
(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 18:51:22 +00:00
|
|
|
ExecStart = "${nbPkgs.rtl}/bin/rtl";
|
2021-11-08 11:43:14 +00:00
|
|
|
# Show "rtl" instead of "node" in the journal
|
|
|
|
SyslogIdentifier = "rtl";
|
|
|
|
User = cfg.user;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
2022-05-07 18:34:21 +00:00
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
2021-11-28 20:24:49 +00:00
|
|
|
} // nbLib.allowedIPAddresses cfg.tor.enforce
|
2021-11-08 11:43:14 +00:00
|
|
|
// nbLib.nodejs;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.${cfg.user} = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = cfg.group;
|
|
|
|
extraGroups =
|
2022-05-05 19:56:16 +00:00
|
|
|
# Reads cert and macaroon from the clightning-rest datadir
|
2022-05-14 13:21:36 +00:00
|
|
|
optional cfg.nodes.clightning.enable clightning-rest.group ++
|
|
|
|
optional lndLoopEnabled lnd.group;
|
2021-11-08 11:43:14 +00:00
|
|
|
};
|
|
|
|
users.groups.${cfg.group} = {};
|
|
|
|
|
|
|
|
nix-bitcoin.secrets.rtl-password.user = cfg.user;
|
|
|
|
nix-bitcoin.generateSecretsCmds.rtl = ''
|
|
|
|
makePasswordSecret rtl-password
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|