From 9ada1d32f3076e1d5d17f7cb65f227cc44e1ea05 Mon Sep 17 00:00:00 2001 From: nixbitcoin Date: Wed, 2 Jan 2019 22:40:53 +0100 Subject: [PATCH] Incorproate PR comments, correct erroneous description in clightning module, add electrs module, add electrs package, add electrs to nix-bitcoin.nix, add electrs to modules/nix-bitcoin.nix --- modules/clightning.nix | 2 +- modules/electrs.nix | 60 +++++++++++++++++++++++++++++++++++++++++ modules/nix-bitcoin.nix | 5 +++- nix-bitcoin.nix | 2 ++ pkgs/electrs.nix | 44 ++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 modules/electrs.nix create mode 100644 pkgs/electrs.nix diff --git a/modules/clightning.nix b/modules/clightning.nix index af259cb..f1a0b8f 100644 --- a/modules/clightning.nix +++ b/modules/clightning.nix @@ -34,7 +34,7 @@ in { dataDir = mkOption { type = types.path; default = "/var/lib/clightning"; - description = "The data directory for bitcoind."; + description = "The data directory for clightning."; }; }; diff --git a/modules/electrs.nix b/modules/electrs.nix new file mode 100644 index 0000000..bb70f92 --- /dev/null +++ b/modules/electrs.nix @@ -0,0 +1,60 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.electrs; +in { + options.services.electrs = { + enable = mkOption { + type = types.bool; + default = false; + description = '' + If enabled, the electrs service will be installed. + ''; + }; + dataDir = mkOption { + type = types.path; + default = "/var/lib/electrs"; + description = "The data directory for electrs."; + }; + }; + + config = mkIf cfg.enable { + users.users.electrs = { + description = "electrs User"; + group = "electrs"; + extraGroups = [ "bitcoinrpc" "keys" ]; + home = cfg.dataDir; + }; + users.groups.electrs = { + name = "electrs"; + }; + + systemd.services.electrs = { + description = "Run electrs"; + wantedBy = [ "muli-user.target" ]; + requires = [ "bitcoind.service" ]; + after = [ "bitcoind.service" ]; + # create shell script to start up electrs safely with password parameter + preStart = '' + mkdir -m 0770 -p ${cfg.dataDir} + chown 'electrs:electrs' ${cfg.dataDir} + echo "${pkgs.electrs}/bin/electrs -vvv --timestamp --db-dir ${cfg.dataDir} --daemon-dir /var/lib/bitcoind --cookie=${config.services.bitcoind.rpcuser}:$(cat /secrets/bitcoin-rpcpassword)" > /var/lib/electrs/startscript.sh + chown -R 'electrs:electrs' ${cfg.dataDir} + chmod u+x ${cfg.dataDir}/startscript.sh + ''; + serviceConfig = { + PermissionsStartOnly = "true"; + ExecStart = "${pkgs.bash}/bin/bash ${cfg.dataDir}/startscript.sh"; + User = "electrs"; + Restart = "on-failure"; + RestartSec = "10s"; + PrivateTmp = "true"; + ProtectSystem = "full"; + NoNewPrivileges = "true"; + PrivateDevices = "true"; + }; + }; + }; +} diff --git a/modules/nix-bitcoin.nix b/modules/nix-bitcoin.nix index 2bfa1f8..f48d670 100644 --- a/modules/nix-bitcoin.nix +++ b/modules/nix-bitcoin.nix @@ -16,6 +16,7 @@ let lightning-charge.package nanopos.package spark-wallet.package + electrs nodejs-8_x nginx ]; @@ -38,6 +39,7 @@ in { ./nix-bitcoin-webindex.nix ./liquid.nix ./spark-wallet.nix + ./electrs.nix ]; options.services.nix-bitcoin = { @@ -83,7 +85,7 @@ in { addnode=ecoc5q34tmbq54wl.onion discover=0 ''; - services.bitcoind.prune = 2000; + services.bitcoind.prune = if (cfg.modules == "minimal") then 2000 else 0; services.bitcoind.dbCache = 1000; services.tor.hiddenServices.bitcoind = { map = [{ @@ -146,6 +148,7 @@ in { services.nix-bitcoin-webindex.enable = cfg.modules == "all"; services.clightning.autolisten = cfg.modules == "all"; services.spark-wallet.enable = cfg.modules == "all"; + services.electrs.enable = cfg.modules == "all"; services.tor.hiddenServices.spark-wallet = { map = [{ port = 80; toPort = 9737; diff --git a/nix-bitcoin.nix b/nix-bitcoin.nix index 79e4f42..719bf24 100644 --- a/nix-bitcoin.nix +++ b/nix-bitcoin.nix @@ -12,6 +12,7 @@ lightning-charge = import pkgs/lightning-charge.nix { inherit pkgs; }; nanopos = import pkgs/nanopos.nix { inherit pkgs; }; spark-wallet = import pkgs/spark-wallet.nix { inherit pkgs; }; + electrs = pkgs.callPackage (import pkgs/electrs.nix) { }; liquidd = pkgs.callPackage (import pkgs/liquidd.nix) { }; in { disabledModules = [ "services/security/tor.nix" ]; @@ -31,6 +32,7 @@ in { inherit lightning-charge; inherit nanopos; inherit spark-wallet; + inherit electrs; inherit liquidd; }; } diff --git a/pkgs/electrs.nix b/pkgs/electrs.nix new file mode 100644 index 0000000..f88170a --- /dev/null +++ b/pkgs/electrs.nix @@ -0,0 +1,44 @@ +let + PkgsSource = builtins.fetchGit { url = "https://github.com/nixos/nixpkgs-channels"; ref = "nixos-18.09"; }; + defaultPkgs = import PkgsSource {overlays = [(import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz)) ]; }; + defaultRust = defaultPkgs.latest.rustChannels.nightly.rust; + defaultCargo = defaultPkgs.latest.rustChannels.nightly.cargo; + defaultBuildRustPackage = defaultPkgs.callPackage (import "${PkgsSource}/pkgs/build-support/rust") { + rust = { + rustc = defaultRust; + cargo = defaultCargo; + }; + }; + +in { pkgs ? defaultPkgs, rust ? defaultRust, buildRustPackage ? defaultBuildRustPackage }: +pkgs.lib.flip pkgs.callPackage { inherit buildRustPackage; } ( + { lib, buildRustPackage, fetchFromGitHub, llvmPackages, clang }: + + let + version = "0.4.2"; + + in buildRustPackage { + name = "electrs-${version}"; + + src = fetchFromGitHub { + owner = "romanz"; + repo = "electrs"; + rev = "5f2d4289dcb98ef283725b3d12f8733a7b9e832b"; + sha256 = "1lqhrcyd8hdaja5k01a2banvjcbxxcwvb2p7zh05984fpzzs02gr"; + }; + + cargoSha256 = "0v0cc62mx728cqfyz3x1bfh2436yiw2hkv58672j2f45cafcgp2h"; + + LIBCLANG_PATH = "${llvmPackages.libclang}/lib"; + buildInputs = [ clang ]; + + meta = with lib; { + description = "An efficient re-implementation of Electrum Server in Rust"; + homepage = https://github.com/romanz/electrs; + maintainers = with maintainers; [ tailhook ]; + platforms = platforms.all; + }; + } +) + +