blockstream-electrs/module.nix

107 lines
4.0 KiB
Nix
Raw Normal View History

2023-08-12 03:53:53 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
options.services.blockstream-electrs = {
enable = mkEnableOption "Blockstream electrs, an Electrum server implemented in Rust, modified for use by Blockstream esplora";
address = mkOption {
type = types.str;
default = "127.0.0.1";
description = mdDoc "Address to listen for RPC connections.";
};
port = mkOption {
type = types.port;
default = 50001;
description = mdDoc "Port to listen for RPC connections.";
};
dataDir = mkOption {
type = types.path;
default = "/var/lib/blockstream-electrs";
description = mdDoc "The data directory for electrs.";
};
monitoringPort = mkOption {
type = types.port;
default = 4224;
description = mdDoc "Prometheus monitoring port.";
};
extraArgs = mkOption {
type = types.separatedString " ";
default = "";
description = mdDoc "Extra command line arguments passed to electrs.";
};
user = mkOption {
type = types.str;
default = "blockstream-electrs";
description = mdDoc "The user as which to run electrs.";
};
group = mkOption {
type = types.str;
2023-08-12 15:42:56 +00:00
default = config.services.blockstream-electrs.user;
2023-08-12 03:53:53 +00:00
description = mdDoc "The group as which to run electrs.";
};
tor.enforce = nbLib.tor.enforce;
};
2023-08-12 15:42:56 +00:00
# config.services.blockstream-electrs = config.services.blockstream-electrs;
2023-08-12 15:55:44 +00:00
myPackage = import ./package.nix;
2023-08-12 03:53:53 +00:00
nbLib = config.nix-bitcoin.lib;
secretsDir = config.nix-bitcoin.secretsDir;
bitcoind = config.services.bitcoind;
2023-08-12 12:27:54 +00:00
in {
2023-08-12 03:53:53 +00:00
inherit options;
2023-08-12 15:42:56 +00:00
config = mkIf config.services.blockstream-electrs.enable {
2023-08-12 03:53:53 +00:00
assertions = [
{ assertion = bitcoind.prune == 0;
message = "electrs does not support bitcoind pruning.";
}
];
services.bitcoind = {
enable = true;
listenWhitelisted = true;
};
systemd.tmpfiles.rules = [
2023-08-12 15:42:56 +00:00
"d '${config.services.blockstream-electrs.dataDir}' 0770 ${config.services.blockstream-electrs.user} ${config.services.blockstream-electrs.group} - -"
2023-08-12 03:53:53 +00:00
];
systemd.services.blockstream-electrs = {
wantedBy = [ "multi-user.target" ];
requires = [ "bitcoind.service" ];
after = [ "bitcoind.service" ];
preStart = ''
echo "auth = \"${bitcoind.rpc.users.public.name}:$(cat ${secretsDir}/bitcoin-rpcpassword-public)\"" \
> electrs.toml
'';
serviceConfig = nbLib.defaultHardening // {
# electrs only uses the working directory for reading electrs.toml
2023-08-12 15:42:56 +00:00
WorkingDirectory = config.services.blockstream-electrs.dataDir;
2023-08-12 03:53:53 +00:00
ExecStart = ''
2023-08-12 15:55:44 +00:00
${myPackage}/bin/electrs \
2023-08-12 03:53:53 +00:00
--log-filters=INFO \
--network=${bitcoind.makeNetworkName "bitcoin" "regtest"} \
2023-08-12 15:42:56 +00:00
--db-dir='${config.services.blockstream-electrs.dataDir}' \
2023-08-12 03:53:53 +00:00
--daemon-dir='${bitcoind.dataDir}' \
2023-08-12 15:42:56 +00:00
--electrum-rpc-addr=${config.services.blockstream-electrs.address}:${toString config.services.blockstream-electrs.port} \
--monitoring-addr=${config.services.blockstream-electrs.address}:${toString config.services.blockstream-electrs.monitoringPort} \
2023-08-12 03:53:53 +00:00
--daemon-rpc-addr=${nbLib.addressWithPort bitcoind.rpc.address bitcoind.rpc.port} \
--daemon-p2p-addr=${nbLib.addressWithPort bitcoind.address bitcoind.whitelistedPort} \
2023-08-12 15:42:56 +00:00
${config.services.blockstream-electrs.extraArgs}
2023-08-12 03:53:53 +00:00
'';
2023-08-12 15:42:56 +00:00
User = config.services.blockstream-electrs.user;
Group = config.services.blockstream-electrs.group;
2023-08-12 03:53:53 +00:00
Restart = "on-failure";
RestartSec = "10s";
2023-08-12 15:42:56 +00:00
ReadWritePaths = [ config.services.blockstream-electrs.dataDir ];
} // nbLib.allowedIPAddresses config.services.blockstream-electrs.tor.enforce;
2023-08-12 03:53:53 +00:00
};
2023-08-12 15:42:56 +00:00
users.users.${config.services.blockstream-electrs.user} = {
2023-08-12 03:53:53 +00:00
isSystemUser = true;
2023-08-12 15:42:56 +00:00
group = config.services.blockstream-electrs.group;
2023-08-12 03:53:53 +00:00
extraGroups = [ "bitcoinrpc-public" ];
};
2023-08-12 15:42:56 +00:00
users.groups.${config.services.blockstream-electrs.group} = {};
2023-08-12 03:53:53 +00:00
};
2023-08-12 11:55:35 +00:00
}