nixpkgs/nixos/modules/services/networking/ergo.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

144 lines
3.7 KiB
Nix
Raw Normal View History

{ config, lib, options, pkgs, ... }:
2020-05-24 19:39:10 +01:00
let
cfg = config.services.ergo;
opt = options.services.ergo;
2020-05-24 19:39:10 +01:00
inherit (lib) literalExpression mkEnableOption mkIf mkOption optionalString types;
2020-05-24 19:39:10 +01:00
configFile = pkgs.writeText "ergo.conf" (''
ergo {
directory = "${cfg.dataDir}"
node {
mining = false
}
wallet.secretStorage.secretDir = "${cfg.dataDir}/wallet/keystore"
}
scorex {
network {
bindAddress = "${cfg.listen.ip}:${toString cfg.listen.port}"
}
'' + optionalString (cfg.api.keyHash != null) ''
restApi {
apiKeyHash = "${cfg.api.keyHash}"
bindAddress = "${cfg.api.listen.ip}:${toString cfg.api.listen.port}"
}
'' + ''
}
'');
in {
options = {
services.ergo = {
enable = mkEnableOption "Ergo service";
dataDir = mkOption {
type = types.path;
default = "/var/lib/ergo";
description = lib.mdDoc "The data directory for the Ergo node.";
2020-05-24 19:39:10 +01:00
};
listen = {
ip = mkOption {
type = types.str;
default = "0.0.0.0";
description = lib.mdDoc "IP address on which the Ergo node should listen.";
2020-05-24 19:39:10 +01:00
};
port = mkOption {
type = types.port;
default = 9006;
description = lib.mdDoc "Listen port for the Ergo node.";
2020-05-24 19:39:10 +01:00
};
};
api = {
keyHash = mkOption {
type = types.nullOr types.str;
default = null;
example = "324dcf027dd4a30a932c441f365a25e86b173defa4b8e58948253471b81b72cf";
description = lib.mdDoc "Hex-encoded Blake2b256 hash of an API key as a 64-chars long Base16 string.";
2020-05-24 19:39:10 +01:00
};
listen = {
ip = mkOption {
type = types.str;
default = "0.0.0.0";
description = lib.mdDoc "IP address that the Ergo node API should listen on if {option}`api.keyHash` is defined.";
2020-05-24 19:39:10 +01:00
};
port = mkOption {
type = types.port;
default = 9052;
description = lib.mdDoc "Listen port for the API endpoint if {option}`api.keyHash` is defined.";
2020-05-24 19:39:10 +01:00
};
};
};
testnet = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Connect to testnet network instead of the default mainnet.";
2020-05-24 19:39:10 +01:00
};
user = mkOption {
type = types.str;
default = "ergo";
description = lib.mdDoc "The user as which to run the Ergo node.";
2020-05-24 19:39:10 +01:00
};
group = mkOption {
type = types.str;
default = cfg.user;
defaultText = literalExpression "config.${opt.user}";
description = lib.mdDoc "The group as which to run the Ergo node.";
2020-05-24 19:39:10 +01:00
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Open ports in the firewall for the Ergo node as well as the API.";
2020-05-24 19:39:10 +01:00
};
};
};
config = mkIf cfg.enable {
systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' 0770 '${cfg.user}' '${cfg.group}' - -"
];
systemd.services.ergo = {
description = "ergo server";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = ''${pkgs.ergo}/bin/ergo \
${optionalString (!cfg.testnet)
"--mainnet"} \
-c ${configFile}'';
};
};
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.listen.port ] ++ [ cfg.api.listen.port ];
};
users.users.${cfg.user} = {
name = cfg.user;
group = cfg.group;
description = "Ergo daemon user";
home = cfg.dataDir;
isSystemUser = true;
};
users.groups.${cfg.group} = {};
};
}