nix-bitcoin/modules/nixbitcoin.nix

106 lines
3.1 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
2018-11-20 14:21:45 -08:00
cfg = config.services.nixbitcoin;
in {
2018-11-22 10:32:26 -08:00
imports =
[
./tor.nix
2018-12-01 08:36:07 -08:00
./bitcoind.nix
2018-11-22 10:32:26 -08:00
./clightning.nix
2018-12-01 08:36:07 -08:00
./lightning-charge.nix
2018-11-22 10:32:26 -08:00
];
2018-11-20 14:21:45 -08:00
options.services.nixbitcoin = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
2018-11-20 14:21:45 -08:00
If enabled, the nix-bitcoin service will be installed.
'';
};
};
config = mkIf cfg.enable {
2018-11-28 15:54:19 -08:00
# Add bitcoinrpc group
2018-11-28 14:58:36 -08:00
users.groups.bitcoinrpc = {};
2018-11-22 10:32:26 -08:00
# Tor
services.tor.enable = true;
services.tor.client.enable = true;
2018-11-19 16:22:16 -08:00
services.tor.hiddenServices.bitcoind = {
map = [{
2018-11-22 16:46:56 -08:00
port = config.services.bitcoind.port;
2018-11-19 16:22:16 -08:00
}];
version = 3;
};
2018-11-22 10:32:26 -08:00
2018-11-22 16:46:56 -08:00
# bitcoind
services.bitcoind.enable = true;
services.bitcoind.listen = true;
services.bitcoind.proxy = config.services.tor.client.socksListenAddress;
services.bitcoind.port = 8333;
services.bitcoind.rpcuser = "bitcoinrpc";
2018-11-23 12:37:50 -08:00
services.bitcoind.extraConfig = ''
assumevalid=0000000000000000000726d186d6298b5054b9a5c49639752294b322a305d240
addnode=ecoc5q34tmbq54wl.onion
discover=0
'';
services.bitcoind.prune = 2000;
2018-11-22 10:32:26 -08:00
# clightning
services.clightning.enable = true;
2018-11-22 16:46:56 -08:00
services.clightning.bitcoin-rpcuser = config.services.bitcoind.rpcuser;
2018-11-22 10:32:26 -08:00
2018-12-01 08:36:07 -08:00
services.lightning-charge.enable = true;
2018-11-22 10:32:26 -08:00
# nodeinfo
2018-11-20 14:21:45 -08:00
systemd.services.nodeinfo = {
description = "Get node info";
wantedBy = [ "multi-user.target" ];
after = [ "clightning.service" "tor.service" ];
2018-11-22 10:32:26 -08:00
path = [ pkgs.clightning pkgs.jq pkgs.sudo ];
2018-11-20 14:21:45 -08:00
serviceConfig = {
2018-11-28 16:30:12 -08:00
ExecStart="${pkgs.bash}/bin/bash ${pkgs.nodeinfo}/bin/nodeinfo > /var/lib/nodeinfo.nix";
User = "root";
Type = "simple";
RemainAfterExit="yes";
Restart = "on-failure";
RestartSec = "10s";
};
2018-11-28 16:30:12 -08:00
};
2018-11-23 12:37:50 -08:00
# Define a user account. Don't forget to set a password with passwd.
2018-11-28 15:54:19 -08:00
users.users.operator = {
isNormalUser = true;
2018-11-28 15:54:19 -08:00
extraGroups = [ "clightning" config.services.bitcoind.group ];
};
2018-11-28 15:54:19 -08:00
environment.interactiveShellInit = ''
alias bitcoin-cli='bitcoin-cli -datadir=${config.services.bitcoind.dataDir}'
alias lightning-cli='sudo -u clightning lightning-cli --lightning-dir=${config.services.clightning.dataDir}'
'';
# Unfortunately c-lightning doesn't allow setting the permissions of the rpc socket
# https://github.com/ElementsProject/lightning/issues/1366
security.sudo.configFile = ''
operator ALL=(clightning) NOPASSWD: ALL
'';
# Give root ssh access to the operator account
systemd.services.copy-root-authorized-keys = {
description = "Copy root authorized keys";
wantedBy = [ "multi-user.target" ];
path = [ ];
serviceConfig = {
2018-11-28 15:54:19 -08:00
ExecStart = "${pkgs.bash}/bin/bash -c \"mkdir -p ${config.users.users.operator.home}/.ssh && cp ${config.users.users.root.home}/.vbox-nixops-client-key ${config.users.users.operator.home}/.ssh/authorized_keys && chown -R operator ${config.users.users.operator.home}/.ssh\"";
2018-11-20 14:21:45 -08:00
user = "root";
type = "oneshot";
};
};
2018-11-19 16:22:16 -08:00
};
}