2019-01-02 22:40:53 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.services.electrs = {
|
2021-12-14 19:51:23 +01:00
|
|
|
enable = mkEnableOption "electrs, an Electrum server implemented in Rust";
|
2021-01-14 13:24:06 +01:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
description = "Address to listen for RPC connections.";
|
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 50001;
|
2021-11-26 15:13:29 +01:00
|
|
|
description = "Port to listen for RPC connections.";
|
2021-01-14 13:24:06 +01:00
|
|
|
};
|
2019-01-02 22:40:53 +01:00
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/electrs";
|
|
|
|
description = "The data directory for electrs.";
|
|
|
|
};
|
2020-09-27 12:43:16 +02:00
|
|
|
monitoringPort = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 4224;
|
|
|
|
description = "Prometheus monitoring port.";
|
|
|
|
};
|
2020-03-04 18:09:03 +01:00
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.separatedString " ";
|
|
|
|
default = "";
|
|
|
|
description = "Extra command line arguments passed to electrs.";
|
|
|
|
};
|
2021-09-13 13:40:48 +02:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "electrs";
|
|
|
|
description = "The user as which to run electrs.";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = cfg.user;
|
|
|
|
description = "The group as which to run electrs.";
|
|
|
|
};
|
2021-11-28 21:24:49 +01:00
|
|
|
tor.enforce = nbLib.tor.enforce;
|
2019-01-02 22:40:53 +01:00
|
|
|
};
|
|
|
|
|
2021-09-13 13:40:47 +02:00
|
|
|
cfg = config.services.electrs;
|
|
|
|
nbLib = config.nix-bitcoin.lib;
|
|
|
|
secretsDir = config.nix-bitcoin.secretsDir;
|
|
|
|
bitcoind = config.services.bitcoind;
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
2020-05-10 16:13:20 +02:00
|
|
|
config = mkIf cfg.enable {
|
2020-06-15 10:34:11 +00:00
|
|
|
assertions = [
|
2020-09-27 12:43:14 +02:00
|
|
|
{ assertion = bitcoind.prune == 0;
|
2020-06-15 10:34:11 +00:00
|
|
|
message = "electrs does not support bitcoind pruning.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-10-05 16:52:02 +02:00
|
|
|
services.bitcoind = {
|
|
|
|
enable = true;
|
2021-10-29 17:56:57 +02:00
|
|
|
listenWhitelisted = true;
|
2021-10-05 16:52:02 +02:00
|
|
|
};
|
2020-10-18 14:49:20 +02:00
|
|
|
|
2020-05-06 12:43:57 +02:00
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -"
|
|
|
|
];
|
|
|
|
|
2019-01-02 22:40:53 +01:00
|
|
|
systemd.services.electrs = {
|
2019-01-04 10:47:06 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-03-04 18:08:49 +01:00
|
|
|
requires = [ "bitcoind.service" ];
|
2019-01-02 22:40:53 +01:00
|
|
|
after = [ "bitcoind.service" ];
|
|
|
|
preStart = ''
|
2021-02-23 10:49:59 +00:00
|
|
|
echo "auth = \"${bitcoind.rpc.users.public.name}:$(cat ${secretsDir}/bitcoin-rpcpassword-public)\"" \
|
2020-03-04 18:08:57 +01:00
|
|
|
> electrs.toml
|
2020-03-04 18:08:51 +01:00
|
|
|
'';
|
2021-02-03 22:44:41 +01:00
|
|
|
serviceConfig = nbLib.defaultHardening // {
|
2021-10-05 16:52:02 +02:00
|
|
|
# electrs only uses the working directory for reading electrs.toml
|
2021-11-26 15:13:36 +01:00
|
|
|
WorkingDirectory = cfg.dataDir;
|
2020-03-04 18:08:57 +01:00
|
|
|
ExecStart = ''
|
2021-11-28 17:34:10 +00:00
|
|
|
${config.nix-bitcoin.pkgs.electrs}/bin/electrs \
|
|
|
|
--log-filters=INFO \
|
2020-10-16 17:43:09 +02:00
|
|
|
--network=${bitcoind.makeNetworkName "bitcoin" "regtest"} \
|
2020-09-27 12:43:13 +02:00
|
|
|
--db-dir='${cfg.dataDir}' \
|
2020-09-27 12:43:14 +02:00
|
|
|
--daemon-dir='${bitcoind.dataDir}' \
|
2020-09-27 12:43:16 +02:00
|
|
|
--electrum-rpc-addr=${cfg.address}:${toString cfg.port} \
|
|
|
|
--monitoring-addr=${cfg.address}:${toString cfg.monitoringPort} \
|
2021-10-01 11:51:57 +02:00
|
|
|
--daemon-rpc-addr=${nbLib.addressWithPort bitcoind.rpc.address bitcoind.rpc.port} \
|
2021-10-29 17:56:57 +02:00
|
|
|
--daemon-p2p-addr=${nbLib.addressWithPort bitcoind.address bitcoind.whitelistedPort} \
|
2020-09-27 12:43:13 +02:00
|
|
|
${cfg.extraArgs}
|
2020-03-04 18:08:57 +01:00
|
|
|
'';
|
2020-03-04 18:09:00 +01:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2019-01-02 22:40:53 +01:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
2022-05-07 20:34:21 +02:00
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
2021-11-28 21:24:49 +01:00
|
|
|
} // nbLib.allowedIPAddresses cfg.tor.enforce;
|
2019-01-02 22:40:53 +01:00
|
|
|
};
|
2019-04-26 11:09:55 +02:00
|
|
|
|
2020-03-04 18:08:49 +01:00
|
|
|
users.users.${cfg.user} = {
|
2021-08-05 00:48:59 +02:00
|
|
|
isSystemUser = true;
|
2020-03-04 18:08:49 +01:00
|
|
|
group = cfg.group;
|
2021-10-05 16:52:02 +02:00
|
|
|
extraGroups = [ "bitcoinrpc-public" ];
|
2020-03-04 18:08:49 +01:00
|
|
|
};
|
|
|
|
users.groups.${cfg.group} = {};
|
2020-05-10 16:13:20 +02:00
|
|
|
};
|
2019-01-02 22:40:53 +01:00
|
|
|
}
|