|
| 1 | +{ config, lib, pkgs, ... }: |
| 2 | + |
| 3 | +with lib; |
| 4 | +let |
| 5 | + options.services = { |
| 6 | + lndhub-go = { |
| 7 | + enable = mkEnableOption "LndHub.go, an accounting wrapper for the Lightning Network"; |
| 8 | + address = mkOption { |
| 9 | + type = types.str; |
| 10 | + default = "127.0.0.1"; |
| 11 | + description = "Address to listen on."; |
| 12 | + }; |
| 13 | + port = mkOption { |
| 14 | + type = types.port; |
| 15 | + default = 8082; |
| 16 | + description = "Port to listen on."; |
| 17 | + }; |
| 18 | + settings = mkOption { |
| 19 | + type = with types; attrsOf (oneOf [ str int bool ]); |
| 20 | + example = { |
| 21 | + ALLOW_ACCOUNT_CREATION = false; |
| 22 | + FEE_RESERVE = true; |
| 23 | + MAX_SEND_AMOUNT = 1000000; |
| 24 | + }; |
| 25 | + description = '' |
| 26 | + LndHub.go settings. |
| 27 | + See here for possible options: |
| 28 | + https://github.com/getAlby/lndhub.go#available-configuration |
| 29 | + ''; |
| 30 | + }; |
| 31 | + package = mkOption { |
| 32 | + type = types.package; |
| 33 | + default = config.nix-bitcoin.pkgs.lndhub-go; |
| 34 | + defaultText = "config.nix-bitcoin.pkgs.lndhub-go"; |
| 35 | + description = "The package providing LndHub.go binaries."; |
| 36 | + }; |
| 37 | + user = mkOption { |
| 38 | + type = types.str; |
| 39 | + default = "lndhub-go"; |
| 40 | + description = "The user as which to run LndHub.go."; |
| 41 | + }; |
| 42 | + group = mkOption { |
| 43 | + type = types.str; |
| 44 | + default = cfg.user; |
| 45 | + description = "The group as which to run LndHub.go."; |
| 46 | + }; |
| 47 | + tor.enforce = nbLib.tor.enforce; |
| 48 | + }; |
| 49 | + }; |
| 50 | + |
| 51 | + cfg = config.services.lndhub-go; |
| 52 | + nbLib = config.nix-bitcoin.lib; |
| 53 | + |
| 54 | + inherit (config.services) |
| 55 | + lnd |
| 56 | + postgresql; |
| 57 | + |
| 58 | + configFile = builtins.toFile "lndhub-go-conf" (lib.generators.toKeyValue {} cfg.settings); |
| 59 | + |
| 60 | + dbName = "lndhub-go"; |
| 61 | +in { |
| 62 | + inherit options; |
| 63 | + |
| 64 | + config = mkIf cfg.enable { |
| 65 | + services.lnd = { |
| 66 | + enable = true; |
| 67 | + macaroons.lndhub-go = { |
| 68 | + inherit (cfg) user; |
| 69 | + permissions = ''{"entity":"info","action":"read"},{"entity":"invoices","action":"read"},{"entity":"invoices","action":"write"},{"entity":"offchain","action":"read"},{"entity":"offchain","action":"write"}''; |
| 70 | + }; |
| 71 | + }; |
| 72 | + services.postgresql = { |
| 73 | + enable = true; |
| 74 | + ensureDatabases = [ dbName ]; |
| 75 | + ensureUsers = [ |
| 76 | + { |
| 77 | + name = cfg.user; |
| 78 | + ensurePermissions."DATABASE \"${dbName}\"" = "ALL PRIVILEGES"; |
| 79 | + } |
| 80 | + ]; |
| 81 | + }; |
| 82 | + |
| 83 | + services.lndhub-go.settings = { |
| 84 | + HOST = cfg.address; |
| 85 | + PORT = cfg.port; |
| 86 | + DATABASE_URI = "unix://${cfg.user}@${dbName}/run/postgresql/.s.PGSQL.${toString postgresql.port}?sslmode=disable"; |
| 87 | + LND_ADDRESS = "${nbLib.addressWithPort lnd.address lnd.rpcPort}"; |
| 88 | + LND_MACAROON_FILE = "/run/lnd/lndhub-go.macaroon"; |
| 89 | + LND_CERT_FILE = lnd.certPath; |
| 90 | + BRANDING_TITLE = "LndHub.go - Nix-Bitcoin"; |
| 91 | + BRANDING_DESC = "Accounting wrapper for the Lightning Network"; |
| 92 | + BRANDING_URL = "https://nixbitcoin.org"; |
| 93 | + BRANDING_LOGO = "https://nixbitcoin.org/files/nix-bitcoin-logo-text.png"; |
| 94 | + BRANDING_FAVICON = "https://nixbitcoin.org/files/nix-bitcoin-logo.png"; |
| 95 | + BRANDING_FOOTER = "about=https://nixbitcoin.org;github=https://github.com/fort-nix/nix-bitcoin"; |
| 96 | + }; |
| 97 | + |
| 98 | + systemd.services.lndhub-go = rec { |
| 99 | + wantedBy = [ "multi-user.target" ]; |
| 100 | + requires = [ "lnd.service" "postgresql.service" ]; |
| 101 | + after = requires; |
| 102 | + preStart = '' |
| 103 | + { |
| 104 | + cat ${configFile} |
| 105 | + echo "JWT_SECRET=$(cat '${config.nix-bitcoin.secretsDir}/lndhub.go-jwt-secret')" |
| 106 | + } > .env |
| 107 | + ''; |
| 108 | + serviceConfig = nbLib.defaultHardening // { |
| 109 | + StateDirectory = "lndhub-go"; |
| 110 | + StateDirectoryMode = "770"; |
| 111 | + # lndhub-go reads file `.env` from the working directory |
| 112 | + WorkingDirectory = "/var/lib/lndhub-go"; |
| 113 | + ExecStart = "${config.nix-bitcoin.pkgs.lndhub-go}/bin/lndhub.go"; |
| 114 | + User = cfg.user; |
| 115 | + Restart = "on-failure"; |
| 116 | + RestartSec = "10s"; |
| 117 | + } // nbLib.allowedIPAddresses cfg.tor.enforce; |
| 118 | + }; |
| 119 | + |
| 120 | + users.users.${cfg.user} = { |
| 121 | + isSystemUser = true; |
| 122 | + group = cfg.group; |
| 123 | + }; |
| 124 | + users.groups.${cfg.group} = {}; |
| 125 | + |
| 126 | + nix-bitcoin.secrets."lndhub.go-jwt-secret".user = cfg.user; |
| 127 | + nix-bitcoin.generateSecretsCmds.lndhub-go = '' |
| 128 | + makePasswordSecret lndhub.go-jwt-secret |
| 129 | + ''; |
| 130 | + }; |
| 131 | +} |
0 commit comments