2018-12-10 19:34:41 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.services.spark-wallet = {
|
2021-02-02 00:53:15 +03:00
|
|
|
enable = mkEnableOption "spark-wallet";
|
2021-01-14 15:24:08 +03:00
|
|
|
address = mkOption {
|
2020-06-10 17:41:13 +03:00
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "http(s) server address.";
|
2021-01-14 15:24:08 +03:00
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 9737;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "http(s) server port.";
|
2020-06-10 17:41:13 +03:00
|
|
|
};
|
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.separatedString " ";
|
|
|
|
default = "";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "Extra command line arguments passed to spark-wallet.";
|
2020-06-10 17:41:13 +03:00
|
|
|
};
|
2021-01-14 15:24:19 +03:00
|
|
|
getPublicAddressCmd = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2021-01-14 15:24:19 +03:00
|
|
|
Bash expression which outputs the public service address.
|
|
|
|
If set, spark-wallet prints a QR code to the systemd journal which
|
|
|
|
encodes an URL for accessing the web interface.
|
|
|
|
'';
|
|
|
|
};
|
2021-02-16 19:51:15 +03:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "spark-wallet";
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "The user as which to run spark-wallet.";
|
2021-02-16 19:51:15 +03:00
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = cfg.user;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc "The group as which to run spark-wallet.";
|
2021-02-16 19:51:15 +03:00
|
|
|
};
|
2021-11-28 23:24:49 +03:00
|
|
|
tor = nbLib.tor;
|
2018-12-10 19:34:41 +03:00
|
|
|
};
|
|
|
|
|
2021-09-13 14:40:47 +03:00
|
|
|
cfg = config.services.spark-wallet;
|
|
|
|
nbLib = config.nix-bitcoin.lib;
|
|
|
|
|
2021-09-13 14:40:49 +03:00
|
|
|
clightning = config.services.clightning;
|
|
|
|
|
2021-09-13 14:40:47 +03:00
|
|
|
# Use wasabi rate provider because the default (bitstamp) doesn't accept
|
|
|
|
# connections through Tor
|
|
|
|
torRateProvider = "--rate-provider wasabi --proxy socks5h://${config.nix-bitcoin.torClientAddressWithPort}";
|
|
|
|
startScript = ''
|
|
|
|
${optionalString (cfg.getPublicAddressCmd != "") ''
|
2022-09-05 18:45:45 +03:00
|
|
|
publicURL=(--public-url "http://$(${cfg.getPublicAddressCmd})")
|
2021-09-13 14:40:47 +03:00
|
|
|
''}
|
|
|
|
exec ${config.nix-bitcoin.pkgs.spark-wallet}/bin/spark-wallet \
|
2021-09-13 14:40:49 +03:00
|
|
|
--ln-path '${clightning.networkDir}' \
|
2021-09-13 14:40:47 +03:00
|
|
|
--host ${cfg.address} --port ${toString cfg.port} \
|
|
|
|
--config '${config.nix-bitcoin.secretsDir}/spark-wallet-login' \
|
2021-11-28 23:24:49 +03:00
|
|
|
${optionalString cfg.tor.proxy torRateProvider} \
|
2022-09-05 18:45:45 +03:00
|
|
|
${optionalString (cfg.getPublicAddressCmd != "") ''"''${publicURL[@]}"''} \
|
2021-09-13 14:40:47 +03:00
|
|
|
--pairing-qr --print-key ${cfg.extraArgs}
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
2018-12-10 19:34:41 +03:00
|
|
|
config = mkIf cfg.enable {
|
2020-10-18 15:49:20 +03:00
|
|
|
services.clightning.enable = true;
|
2020-06-15 13:34:11 +03:00
|
|
|
|
2019-01-02 18:17:57 +03:00
|
|
|
systemd.services.spark-wallet = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-01-14 15:24:19 +03:00
|
|
|
requires = [ "clightning.service" ];
|
|
|
|
after = [ "clightning.service" ];
|
2020-10-16 18:43:02 +03:00
|
|
|
script = startScript;
|
2021-02-04 00:44:41 +03:00
|
|
|
serviceConfig = nbLib.defaultHardening // {
|
2021-02-16 19:51:15 +03:00
|
|
|
User = cfg.user;
|
2019-01-02 18:17:57 +03:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
2021-11-28 23:24:49 +03:00
|
|
|
} // nbLib.allowedIPAddresses cfg.tor.enforce
|
2021-02-04 00:44:41 +03:00
|
|
|
// nbLib.nodejs;
|
2018-12-10 19:34:41 +03:00
|
|
|
};
|
2021-09-08 18:01:18 +03:00
|
|
|
|
2021-09-13 14:40:48 +03:00
|
|
|
users.users.${cfg.user} = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = cfg.group;
|
2021-09-13 14:40:49 +03:00
|
|
|
extraGroups = [ clightning.group ];
|
2021-09-13 14:40:48 +03:00
|
|
|
};
|
|
|
|
users.groups.${cfg.group} = {};
|
|
|
|
|
2021-02-16 19:51:15 +03:00
|
|
|
nix-bitcoin.secrets.spark-wallet-login.user = cfg.user;
|
2021-09-08 18:01:18 +03:00
|
|
|
nix-bitcoin.generateSecretsCmds.spark-wallet = ''
|
|
|
|
makePasswordSecret spark-wallet-password
|
|
|
|
if [[ spark-wallet-password -nt spark-wallet-login ]]; then
|
|
|
|
echo "login=spark-wallet:$(cat spark-wallet-password)" > spark-wallet-login
|
|
|
|
fi
|
|
|
|
'';
|
2019-01-02 18:17:57 +03:00
|
|
|
};
|
2018-12-10 19:34:41 +03:00
|
|
|
}
|