1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.cgminer;
7
8 convType = with builtins;
9 v: if isBool v then (if v then "true" else "false") else toString v;
10 mergedHwConfig =
11 mapAttrsToList (n: v: ''"${n}": "${(concatStringsSep "," (map convType v))}"'')
12 (foldAttrs (n: a: [n] ++ a) [] cfg.hardware);
13 mergedConfig = with builtins;
14 mapAttrsToList (n: v: ''"${n}": ${if isBool v then "" else ''"''}${convType v}${if isBool v then "" else ''"''}'')
15 cfg.config;
16
17 cgminerConfig = pkgs.writeText "cgminer.conf" ''
18 {
19 ${concatStringsSep ",\n" mergedHwConfig},
20 ${concatStringsSep ",\n" mergedConfig},
21 "pools": [
22 ${concatStringsSep ",\n"
23 (map (v: ''{"url": "${v.url}", "user": "${v.user}", "pass": "${v.pass}"}'')
24 cfg.pools)}]
25 }
26 '';
27in
28{
29 ###### interface
30 options = {
31
32 services.cgminer = {
33
34 enable = mkOption {
35 default = false;
36 description = ''
37 Whether to enable cgminer, an ASIC/FPGA/GPU miner for bitcoin and
38 litecoin.
39 '';
40 };
41
42 package = mkOption {
43 default = pkgs.cgminer;
44 description = "Which cgminer derivation to use.";
45 type = types.package;
46 };
47
48 user = mkOption {
49 default = "cgminer";
50 description = "User account under which cgminer runs";
51 };
52
53 pools = mkOption {
54 default = []; # Run benchmark
55 description = "List of pools where to mine";
56 example = [{
57 url = "http://p2pool.org:9332";
58 username = "17EUZxTvs9uRmPsjPZSYUU3zCz9iwstudk";
59 password="X";
60 }];
61 };
62
63 hardware = mkOption {
64 default = []; # Run without options
65 description= "List of config options for every GPU";
66 example = [
67 {
68 intensity = 9;
69 gpu-engine = "0-985";
70 gpu-fan = "0-85";
71 gpu-memclock = 860;
72 gpu-powertune = 20;
73 temp-cutoff = 95;
74 temp-overheat = 85;
75 temp-target = 75;
76 }
77 {
78 intensity = 9;
79 gpu-engine = "0-950";
80 gpu-fan = "0-85";
81 gpu-memclock = 825;
82 gpu-powertune = 20;
83 temp-cutoff = 95;
84 temp-overheat = 85;
85 temp-target = 75;
86 }];
87 };
88
89 config = mkOption {
90 default = {};
91 description = "Additional config";
92 example = {
93 auto-fan = true;
94 auto-gpu = true;
95 expiry = 120;
96 failover-only = true;
97 gpu-threads = 2;
98 log = 5;
99 queue = 1;
100 scan-time = 60;
101 temp-histeresys = 3;
102 };
103 };
104 };
105 };
106
107
108 ###### implementation
109
110 config = mkIf config.services.cgminer.enable {
111
112 users.extraUsers = optionalAttrs (cfg.user == "cgminer") (singleton
113 { name = "cgminer";
114 uid = config.ids.uids.cgminer;
115 description = "Cgminer user";
116 });
117
118 environment.systemPackages = [ cfg.package ];
119
120 systemd.services.cgminer = {
121 path = [ pkgs.cgminer ];
122
123 after = [ "network.target" "display-manager.service" ];
124 wantedBy = [ "multi-user.target" ];
125
126 environment = {
127 LD_LIBRARY_PATH = ''/run/opengl-driver/lib:/run/opengl-driver-32/lib'';
128 DISPLAY = ":0";
129 GPU_MAX_ALLOC_PERCENT = "100";
130 GPU_USE_SYNC_OBJECTS = "1";
131 };
132
133 serviceConfig = {
134 ExecStart = "${pkgs.cgminer}/bin/cgminer --syslog --text-only --config ${cgminerConfig}";
135 User = cfg.user;
136 RestartSec = "30s";
137 Restart = "always";
138 StartLimitInterval = "1m";
139 };
140 };
141
142 };
143
144}