nix-bitcoin/modules/joinmarket-ob-watcher.nix

122 lines
3.5 KiB
Nix
Raw Normal View History

2021-01-17 15:24:57 +03:00
{ config, lib, pkgs, ... }:
with lib;
let
options.services.joinmarket-ob-watcher = {
enable = mkEnableOption "JoinMarket orderbook watcher";
address = mkOption {
type = types.str;
default = "127.0.0.1";
description = "HTTP server address.";
};
port = mkOption {
type = types.port;
default = 62601;
description = "HTTP server port.";
};
dataDir = mkOption {
readOnly = true;
default = "/var/lib/joinmarket-ob-watcher";
description = "The data directory for JoinMarket orderbook watcher.";
};
user = mkOption {
type = types.str;
default = "joinmarket-ob-watcher";
description = "The user as which to run JoinMarket.";
};
group = mkOption {
type = types.str;
default = cfg.user;
description = "The group as which to run JoinMarket.";
};
# This option is only used by netns-isolation
enforceTor = mkOption {
readOnly = true;
default = true;
};
};
2021-01-17 15:24:57 +03:00
cfg = config.services.joinmarket-ob-watcher;
nbLib = config.nix-bitcoin.lib;
2021-01-17 15:24:57 +03:00
nbPkgs = config.nix-bitcoin.pkgs;
secretsDir = config.nix-bitcoin.secretsDir;
2021-08-05 01:49:00 +03:00
inherit (config.services) bitcoind joinmarket;
2021-08-05 01:49:00 +03:00
2021-01-17 15:24:57 +03:00
configFile = builtins.toFile "config" ''
[BLOCKCHAIN]
blockchain_source = bitcoin-rpc
network = ${bitcoind.network}
rpc_host = ${bitcoind.rpc.address}
rpc_port = ${toString bitcoind.rpc.port}
rpc_user = ${bitcoind.rpc.users.joinmarket-ob-watcher.name}
2021-01-17 15:24:57 +03:00
${joinmarket.ircServers}
2021-01-17 15:24:57 +03:00
'';
in {
inherit options;
2021-01-17 15:24:57 +03:00
config = mkIf cfg.enable {
services.bitcoind.rpc.users.joinmarket-ob-watcher = {
passwordHMACFromFile = true;
rpcwhitelist = bitcoind.rpc.users.public.rpcwhitelist ++ [
"listwallets"
];
};
# Joinmarket is Tor-only
2021-01-17 15:24:57 +03:00
services.tor = {
enable = true;
client.enable = true;
};
systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -"
];
systemd.services.joinmarket-ob-watcher = rec {
2021-01-17 15:24:57 +03:00
wantedBy = [ "multi-user.target" ];
requires = [ "tor.service" "bitcoind.service" ];
after = requires;
# The service writes to HOME/.config/matplotlib
environment.HOME = cfg.dataDir;
2021-01-17 15:24:57 +03:00
preStart = ''
{
cat ${configFile}
echo
echo '[BLOCKCHAIN]'
echo "rpc_password = $(cat ${secretsDir}/bitcoin-rpcpassword-joinmarket-ob-watcher)"
} > '${cfg.dataDir}/joinmarket.cfg'
2021-01-17 15:24:57 +03:00
'';
serviceConfig = nbLib.defaultHardening // rec {
2021-01-17 15:24:57 +03:00
StateDirectory = "joinmarket-ob-watcher";
StateDirectoryMode = "770";
WorkingDirectory = cfg.dataDir; # The service creates dir 'logs' in the working dir
User = cfg.user;
2021-01-17 15:24:57 +03:00
ExecStart = ''
${nbPkgs.joinmarket}/bin/jm-ob-watcher --datadir=${cfg.dataDir} \
2021-01-17 15:24:57 +03:00
--host=${cfg.address} --port=${toString cfg.port}
'';
SystemCallFilter = nbLib.defaultHardening.SystemCallFilter ++ [ "mbind" ] ;
2021-01-17 15:24:57 +03:00
Restart = "on-failure";
RestartSec = "10s";
} // nbLib.allowTor;
2021-01-17 15:24:57 +03:00
};
users.users.${cfg.user} = {
isSystemUser = true;
group = cfg.group;
home = cfg.dataDir;
};
users.groups.${cfg.group} = {};
nix-bitcoin.secrets = {
bitcoin-rpcpassword-joinmarket-ob-watcher.user = cfg.user;
bitcoin-HMAC-joinmarket-ob-watcher.user = bitcoind.user;
};
nix-bitcoin.generateSecretsCmds.joinmarket-ob-watcher = ''
makeBitcoinRPCPassword joinmarket-ob-watcher
'';
2021-01-17 15:24:57 +03:00
};
}