1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.ethminer;
7 poolUrl = escapeShellArg "stratum1+tcp://${cfg.wallet}@${cfg.pool}:${toString cfg.stratumPort}/${cfg.rig}/${cfg.registerMail}";
8in
9
10{
11
12 ###### interface
13
14 options = {
15
16 services.ethminer = {
17
18 enable = mkOption {
19 type = types.bool;
20 default = false;
21 description = "Enable ethminer ether mining.";
22 };
23
24 recheckInterval = mkOption {
25 type = types.int;
26 default = 2000;
27 description = "Interval in milliseconds between farm rechecks.";
28 };
29
30 toolkit = mkOption {
31 type = types.enum [ "cuda" "opencl" ];
32 default = "cuda";
33 description = "Cuda or opencl toolkit.";
34 };
35
36 apiPort = mkOption {
37 type = types.int;
38 default = -3333;
39 description = "Ethminer api port. minus sign puts api in read-only mode.";
40 };
41
42 wallet = mkOption {
43 type = types.str;
44 example = "0x0123456789abcdef0123456789abcdef01234567";
45 description = "Ethereum wallet address.";
46 };
47
48 pool = mkOption {
49 type = types.str;
50 example = "eth-us-east1.nanopool.org";
51 description = "Mining pool address.";
52 };
53
54 stratumPort = mkOption {
55 type = types.port;
56 default = 9999;
57 description = "Stratum protocol tcp port.";
58 };
59
60 rig = mkOption {
61 type = types.str;
62 default = "mining-rig-name";
63 description = "Mining rig name.";
64 };
65
66 registerMail = mkOption {
67 type = types.str;
68 example = "email%40example.org";
69 description = "Url encoded email address to register with pool.";
70 };
71
72 maxPower = mkOption {
73 type = types.int;
74 default = 113;
75 description = "Miner max watt usage.";
76 };
77
78 };
79
80 };
81
82
83 ###### implementation
84
85 config = mkIf cfg.enable {
86
87 systemd.services.ethminer = {
88 path = [ pkgs.cudatoolkit ];
89 description = "ethminer ethereum mining service";
90 wantedBy = [ "multi-user.target" ];
91 after = [ "network.target" ];
92
93 serviceConfig = {
94 DynamicUser = true;
95 ExecStartPre = "${pkgs.ethminer}/bin/.ethminer-wrapped --list-devices";
96 ExecStartPost = optional (cfg.toolkit == "cuda") "+${getBin config.boot.kernelPackages.nvidia_x11}/bin/nvidia-smi -pl ${toString cfg.maxPower}";
97 Restart = "always";
98 };
99
100 environment = {
101 LD_LIBRARY_PATH = "${config.boot.kernelPackages.nvidia_x11}/lib";
102 };
103
104 script = ''
105 ${pkgs.ethminer}/bin/.ethminer-wrapped \
106 --farm-recheck ${toString cfg.recheckInterval} \
107 --report-hashrate \
108 --${cfg.toolkit} \
109 --api-port ${toString cfg.apiPort} \
110 --pool ${poolUrl}
111 '';
112
113 };
114
115 };
116
117}