at 23.11-pre 3.8 kB view raw
1{ config, lib, options, pkgs, ... }: 2 3let 4 cfg = config.services.ergo; 5 opt = options.services.ergo; 6 7 inherit (lib) literalExpression mkEnableOption mkIf mkOption optionalString types; 8 9 configFile = pkgs.writeText "ergo.conf" ('' 10ergo { 11 directory = "${cfg.dataDir}" 12 node { 13 mining = false 14 } 15 wallet.secretStorage.secretDir = "${cfg.dataDir}/wallet/keystore" 16} 17 18scorex { 19 network { 20 bindAddress = "${cfg.listen.ip}:${toString cfg.listen.port}" 21 } 22'' + optionalString (cfg.api.keyHash != null) '' 23 restApi { 24 apiKeyHash = "${cfg.api.keyHash}" 25 bindAddress = "${cfg.api.listen.ip}:${toString cfg.api.listen.port}" 26 } 27'' + '' 28} 29''); 30 31in { 32 33 options = { 34 35 services.ergo = { 36 enable = mkEnableOption (lib.mdDoc "Ergo service"); 37 38 dataDir = mkOption { 39 type = types.path; 40 default = "/var/lib/ergo"; 41 description = lib.mdDoc "The data directory for the Ergo node."; 42 }; 43 44 listen = { 45 ip = mkOption { 46 type = types.str; 47 default = "0.0.0.0"; 48 description = lib.mdDoc "IP address on which the Ergo node should listen."; 49 }; 50 51 port = mkOption { 52 type = types.port; 53 default = 9006; 54 description = lib.mdDoc "Listen port for the Ergo node."; 55 }; 56 }; 57 58 api = { 59 keyHash = mkOption { 60 type = types.nullOr types.str; 61 default = null; 62 example = "324dcf027dd4a30a932c441f365a25e86b173defa4b8e58948253471b81b72cf"; 63 description = lib.mdDoc "Hex-encoded Blake2b256 hash of an API key as a 64-chars long Base16 string."; 64 }; 65 66 listen = { 67 ip = mkOption { 68 type = types.str; 69 default = "0.0.0.0"; 70 description = lib.mdDoc "IP address that the Ergo node API should listen on if {option}`api.keyHash` is defined."; 71 }; 72 73 port = mkOption { 74 type = types.port; 75 default = 9052; 76 description = lib.mdDoc "Listen port for the API endpoint if {option}`api.keyHash` is defined."; 77 }; 78 }; 79 }; 80 81 testnet = mkOption { 82 type = types.bool; 83 default = false; 84 description = lib.mdDoc "Connect to testnet network instead of the default mainnet."; 85 }; 86 87 user = mkOption { 88 type = types.str; 89 default = "ergo"; 90 description = lib.mdDoc "The user as which to run the Ergo node."; 91 }; 92 93 group = mkOption { 94 type = types.str; 95 default = cfg.user; 96 defaultText = literalExpression "config.${opt.user}"; 97 description = lib.mdDoc "The group as which to run the Ergo node."; 98 }; 99 100 openFirewall = mkOption { 101 type = types.bool; 102 default = false; 103 description = lib.mdDoc "Open ports in the firewall for the Ergo node as well as the API."; 104 }; 105 }; 106 }; 107 108 config = mkIf cfg.enable { 109 110 systemd.tmpfiles.rules = [ 111 "d '${cfg.dataDir}' 0770 '${cfg.user}' '${cfg.group}' - -" 112 ]; 113 114 systemd.services.ergo = { 115 description = "ergo server"; 116 wantedBy = [ "multi-user.target" ]; 117 after = [ "network-online.target" ]; 118 serviceConfig = { 119 User = cfg.user; 120 Group = cfg.group; 121 ExecStart = ''${pkgs.ergo}/bin/ergo \ 122 ${optionalString (!cfg.testnet) 123 "--mainnet"} \ 124 -c ${configFile}''; 125 }; 126 }; 127 128 networking.firewall = mkIf cfg.openFirewall { 129 allowedTCPPorts = [ cfg.listen.port ] ++ [ cfg.api.listen.port ]; 130 }; 131 132 users.users.${cfg.user} = { 133 name = cfg.user; 134 group = cfg.group; 135 description = "Ergo daemon user"; 136 home = cfg.dataDir; 137 isSystemUser = true; 138 }; 139 140 users.groups.${cfg.group} = {}; 141 142 }; 143}