nix-bitcoin/modules/nixbitcoin.nix

186 lines
4.8 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
2018-11-20 22:21:45 +00:00
cfg = config.services.nixbitcoin;
2018-12-01 20:48:58 +00:00
indexFile = pkgs.writeText "index.html" ''
<html>
<body>
<p>
<h1>
nix-bitcoin
</h1>
</p>
<p>
<h2>
<a href="store/">store</a>
</h2>
</p>
<p>
<h3>
lightning node: CLIGHTNING_ID
</h3>
</p>
</body>
</html>
'';
createWebIndex = pkgs.writeText "make-index.sh" ''
set -e
mkdir -p /var/www/
cp ${indexFile} /var/www/index.html
chown -R nginx /var/www/
nodeinfo
. <(nodeinfo)
sed -i "s/CLIGHTNING_ID/$CLIGHTNING_ID/g" /var/www/index.html
'';
in {
2018-11-22 18:32:26 +00:00
imports =
[
./tor.nix
2018-12-01 16:36:07 +00:00
./bitcoind.nix
2018-11-22 18:32:26 +00:00
./clightning.nix
2018-12-01 16:36:07 +00:00
./lightning-charge.nix
2018-12-01 20:48:58 +00:00
./nanopos.nix
2018-11-22 18:32:26 +00:00
];
2018-11-20 22:21:45 +00:00
options.services.nixbitcoin = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
2018-11-20 22:21:45 +00:00
If enabled, the nix-bitcoin service will be installed.
'';
};
};
config = mkIf cfg.enable {
2018-11-28 23:54:19 +00:00
# Add bitcoinrpc group
2018-11-28 22:58:36 +00:00
users.groups.bitcoinrpc = {};
2018-11-22 18:32:26 +00:00
# Tor
services.tor.enable = true;
services.tor.client.enable = true;
2018-11-20 00:22:16 +00:00
services.tor.hiddenServices.bitcoind = {
map = [{
2018-11-23 00:46:56 +00:00
port = config.services.bitcoind.port;
2018-11-20 00:22:16 +00:00
}];
version = 3;
};
2018-11-22 18:32:26 +00:00
2018-11-23 00:46:56 +00: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 20:37:50 +00:00
services.bitcoind.extraConfig = ''
assumevalid=0000000000000000000726d186d6298b5054b9a5c49639752294b322a305d240
addnode=ecoc5q34tmbq54wl.onion
discover=0
'';
services.bitcoind.prune = 2000;
2018-11-22 18:32:26 +00:00
# clightning
2018-12-01 20:48:58 +00:00
services.clightning = {
enable = true;
bitcoin-rpcuser = config.services.bitcoind.rpcuser;
};
services.tor.hiddenServices.clightning = {
map = [{
port = 9375; toPort = 9375;
}];
version = 3;
};
2018-11-22 18:32:26 +00:00
2018-12-01 16:36:07 +00:00
services.lightning-charge.enable = true;
2018-12-01 20:48:58 +00:00
services.nanopos.enable = true;
services.nginx = {
enable = true;
virtualHosts."_" = {
root = "/var/www";
extraConfig = ''
location /store/ {
proxy_pass http://127.0.0.1:${toString config.services.nanopos.port};
rewrite /store/(.*) /$1 break;
}
'';
};
};
services.tor.hiddenServices.nginx = {
map = [{
port = 80;
} {
port = 443;
}];
version = 3;
};
# create-web-index
systemd.services.create-web-index = {
description = "Get node info";
wantedBy = [ "multi-user.target" ];
after = [ "nodeinfo.service" ];
path = [ pkgs.nodeinfo pkgs.clightning pkgs.jq pkgs.sudo ];
serviceConfig = {
ExecStart="${pkgs.bash}/bin/bash ${createWebIndex}";
User = "root";
Type = "simple";
RemainAfterExit="yes";
Restart = "on-failure";
RestartSec = "10s";
};
};
2018-12-01 16:36:07 +00:00
2018-11-22 18:32:26 +00:00
# nodeinfo
2018-11-20 22:21:45 +00:00
systemd.services.nodeinfo = {
description = "Get node info";
wantedBy = [ "multi-user.target" ];
after = [ "clightning.service" "tor.service" ];
2018-11-22 18:32:26 +00:00
path = [ pkgs.clightning pkgs.jq pkgs.sudo ];
2018-11-20 22:21:45 +00:00
serviceConfig = {
2018-12-01 20:48:58 +00:00
ExecStart="${pkgs.bash}/bin/bash ${pkgs.nodeinfo}/bin/nodeinfo > /var/lib/nodeinfo.sh";
User = "root";
Type = "simple";
RemainAfterExit="yes";
Restart = "on-failure";
RestartSec = "10s";
};
};
2018-11-23 20:37:50 +00:00
# Define a user account. Don't forget to set a password with passwd.
2018-11-28 23:54:19 +00:00
users.users.operator = {
isNormalUser = true;
2018-11-28 23:54:19 +00:00
extraGroups = [ "clightning" config.services.bitcoind.group ];
};
2018-11-28 23:54:19 +00: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 23:54:19 +00: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 22:21:45 +00:00
user = "root";
type = "oneshot";
};
};
2018-11-20 00:22:16 +00:00
};
}