nixos/modules/chia.nix

96 lines
2.5 KiB
Nix
Raw Normal View History

2023-06-05 22:35:44 +01:00
{ config, pkgs, lib, nixpkgs-chia, ... }:
2023-05-11 21:12:57 +01:00
let
cfg = config.custom.chia;
2023-06-05 22:35:44 +01:00
chia = nixpkgs-chia.legacyPackages.x86_64-linux.chia;
2023-05-11 21:12:57 +01:00
in
{
options.custom.chia = {
enable = lib.mkEnableOption "chia";
path = lib.mkOption {
type = lib.types.str;
default = "/var/lib/chia";
};
keyFile = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
2023-06-10 09:32:27 +01:00
keyLabel = lib.mkOption {
type = lib.types.str;
default = "default";
};
2023-05-11 21:12:57 +01:00
targetAddress = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
plotDirectories = lib.mkOption {
type = with lib.types; nullOr (listOf str);
default = null;
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
};
2023-05-11 21:12:57 +01:00
};
config = lib.mkIf cfg.enable {
2023-06-05 22:35:44 +01:00
environment.systemPackages = [ chia ];
2023-05-11 21:12:57 +01:00
users.groups.chia = { };
users.users.chia = {
home = cfg.path;
createHome = true;
isSystemUser = true;
group = "chia";
};
systemd.services.chia = {
description = "Chia daemon.";
wantedBy = [ "multi-user.target" ];
2023-06-05 22:35:44 +01:00
preStart = lib.strings.concatStringsSep "\n" ([ "${chia}/bin/chia init" ]
2023-06-10 09:32:27 +01:00
++ (if cfg.keyFile == null then [ ] else [ "${chia}/bin/chia keys add -f ${cfg.keyFile} -l '${cfg.keyLabel}'" ])
2023-05-11 21:12:57 +01:00
++ (if cfg.targetAddress == null then [ ] else [
''
${pkgs.yq-go}/bin/yq e \
'.farmer.xch_target_address = "${cfg.targetAddress}" | .pool.xch_target_address = "${cfg.targetAddress}"' \
-i ${cfg.path}/.chia/mainnet/config/config.yaml
''
]) ++ (if cfg.plotDirectories == null then [ ] else [
''
${pkgs.yq-go}/bin/yq e \
'.harvester.plot_directories = [${lib.strings.concatMapStringsSep "," (x: "\"" + x + "\"") cfg.plotDirectories}]' \
-i ${cfg.path}/.chia/mainnet/config/config.yaml
''
]));
2023-06-05 22:35:44 +01:00
script = "${chia}/bin/chia start farmer";
preStop = "${chia}/bin/chia stop -d farmer";
2023-05-11 21:12:57 +01:00
serviceConfig = {
Type = "forking";
User = "chia";
Group = "chia";
WorkingDirectory = cfg.path;
Restart = "always";
RestartSec = 10;
TimeoutStopSec = 120;
OOMScoreAdjust = 1000;
Nice = 2;
IOSchedulingClass = "best-effort";
IOSchedulingPriority = 7;
};
};
networking.firewall = lib.mkIf cfg.openFirewall {
allowedTCPPorts = [ 8444 ];
};
2023-05-11 21:12:57 +01:00
};
}
2023-06-10 09:32:27 +01:00