|
| 1 | +{ config, lib, pkgs, ... }: |
| 2 | + |
| 3 | +with lib; |
| 4 | +let |
| 5 | + options.services.lianad = { |
| 6 | + enable = mkEnableOption "lianad bitcoin wallet"; |
| 7 | + daemon = mkOption { |
| 8 | + type = types.bool; |
| 9 | + default = false; |
| 10 | + description = mdDoc "Whether to run the process as a UNIX daemon (double fork magic)."; |
| 11 | + }; |
| 12 | + data_dir = mkOption { |
| 13 | + type = types.path; |
| 14 | + default = "/var/lib/lianad"; |
| 15 | + description = mdDoc "Path to the folder where we should store the application data."; |
| 16 | + }; |
| 17 | + main_descriptor = mkOption { |
| 18 | + type = types.str; |
| 19 | + default = "wsh(or_d(pk([0dd8c6f0/48'/1'/0'/2']tpubDFMbZ7U5k5hEfsttnZTKMmwrGMHnqUGxhShsvBjHimXBpmAp5KmxpyGsLx2toCaQgYq5TipBLhTUtA2pRSB9b14m5KwSohTDoCHkk1EnqtZ/<0;1>/*),and_v(v:pkh([d4ab66f1/48'/1'/0'/2']tpubDEXYN145WM4rVKtcWpySBYiVQ229pmrnyAGJT14BBh2QJr7ABJswchDicZfFaauLyXhDad1nCoCZQEwAW87JPotP93ykC9WJvoASnBjYBxW/<0;1>/*),older(65535))))#7nvn6ssc"; |
| 20 | + description = mdDoc "The wallet descriptor."; |
| 21 | + }; |
| 22 | + network = mkOption { |
| 23 | + type = types.str; |
| 24 | + default = "bitcoin"; |
| 25 | + description = mdDoc "bitcoin, testnet, signet, or regtest"; |
| 26 | + }; |
| 27 | + bitcoind_addr = mkOption { |
| 28 | + type = types.str; |
| 29 | + default = "127.0.0.1"; |
| 30 | + description = mdDoc "bitcoind address."; |
| 31 | + }; |
| 32 | + bitcoind_port = mkOption { |
| 33 | + type = types.port; |
| 34 | + default = 8332; |
| 35 | + description = mdDoc "bitcoind port."; |
| 36 | + }; |
| 37 | + }; |
| 38 | + |
| 39 | + cfg = config.services.lianad; |
| 40 | + nbLib = config.nix-bitcoin.lib; |
| 41 | + secretsDir = config.nix-bitcoin.secretsDir; |
| 42 | + bitcoind = config.services.bitcoind; |
| 43 | +in { |
| 44 | + inherit options; |
| 45 | + |
| 46 | + config = mkIf cfg.enable { |
| 47 | + services.bitcoind = { |
| 48 | + enable = true; |
| 49 | + listenWhitelisted = true; |
| 50 | + }; |
| 51 | + |
| 52 | + systemd.tmpfiles.rules = [ |
| 53 | + "d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -" |
| 54 | + ]; |
| 55 | + |
| 56 | + systemd.services.lianad = { |
| 57 | + wantedBy = [ "multi-user.target" ]; |
| 58 | + requires = [ "bitcoind.service" ]; |
| 59 | + after = [ "bitcoind.service" "nix-bitcoin-secrets.target" ]; |
| 60 | + preStart = '' |
| 61 | + cat << EOF > lianad_config.toml |
| 62 | +# these should come from options.services.lianad |
| 63 | +daemon = false |
| 64 | +data_dir = "/var/lib/lianad" |
| 65 | +log_level = "debug" |
| 66 | +main_descriptor = "wsh(or_d(pk([0dd8c6f0/48'/1'/0'/2']tpubDFMbZ7U5k5hEfsttnZTKMmwrGMHnqUGxhShsvBjHimXBpmAp5KmxpyGsLx2toCaQgYq5TipBLhTUtA2pRSB9b14m5KwSohTDoCHkk1EnqtZ/<0;1>/*),and_v(v:pkh([d4ab66f1/48'/1'/0'/2']tpubDEXYN145WM4rVKtcWpySBYiVQ229pmrnyAGJT14BBh2QJr7ABJswchDicZfFaauLyXhDad1nCoCZQEwAW87JPotP93ykC9WJvoASnBjYBxW/<0;1>/*),older(65535))))#7nvn6ssc" |
| 67 | +
|
| 68 | +# these should come from options.services.lianad |
| 69 | +[bitcoin_config] |
| 70 | +network = "signet" |
| 71 | +poll_interval_secs = 30 |
| 72 | +
|
| 73 | +# these should come from options.services.bitcoind |
| 74 | +[bitcoind_config] |
| 75 | +addr = "127.0.0.1:38332" |
| 76 | +auth = "username:password" |
| 77 | +
|
| 78 | +EOF |
| 79 | + ''; |
| 80 | + serviceConfig = nbLib.defaultHardening // { |
| 81 | + # lianad only uses the working directory for reading lianad_config.toml |
| 82 | + WorkingDirectory = cfg.dataDir; |
| 83 | + ExecStart = '' |
| 84 | + ${config.nix-bitcoin.pkgs.lianad}/bin/lianad \ |
| 85 | + --conf lianad_config.toml |
| 86 | + ''; |
| 87 | + User = cfg.user; |
| 88 | + Group = cfg.group; |
| 89 | + Restart = "on-failure"; |
| 90 | + RestartSec = "10s"; |
| 91 | + ReadWritePaths = [ cfg.dataDir ]; |
| 92 | + } // nbLib.allowedIPAddresses cfg.tor.enforce; |
| 93 | + }; |
| 94 | + |
| 95 | + users.users.${cfg.user} = { |
| 96 | + isSystemUser = true; |
| 97 | + group = cfg.group; |
| 98 | + extraGroups = [ "bitcoinrpc-public" ]; |
| 99 | + }; |
| 100 | + users.groups.${cfg.group} = {}; |
| 101 | + }; |
| 102 | +} |
0 commit comments