1{ config, lib, pkgs, ... }:
2with lib;
3let
4 stateDir = "/var/lib/foldingathome";
5 cfg = config.services.foldingAtHome;
6 fahUser = "foldingathome";
7in {
8
9 ###### interface
10
11 options = {
12
13 services.foldingAtHome = {
14
15 enable = mkOption {
16 default = false;
17 description = ''
18 Whether to enable the Folding@Home to use idle CPU time.
19 '';
20 };
21
22 nickname = mkOption {
23 default = "Anonymous";
24 description = ''
25 A unique handle for statistics.
26 '';
27 };
28
29 config = mkOption {
30 default = "";
31 description = ''
32 Extra configuration. Contents will be added verbatim to the
33 configuration file.
34 '';
35 };
36
37 };
38
39 };
40
41 ###### implementation
42
43 config = mkIf cfg.enable {
44
45 users.extraUsers = singleton
46 { name = fahUser;
47 uid = config.ids.uids.foldingathome;
48 description = "Folding@Home user";
49 home = stateDir;
50 };
51
52 jobs.foldingAtHome =
53 { name = "foldingathome";
54
55 startOn = "started network-interfaces";
56 stopOn = "stopping network-interfaces";
57
58 preStart =
59 ''
60 mkdir -m 0755 -p ${stateDir}
61 chown ${fahUser} ${stateDir}
62 cp -f ${pkgs.writeText "client.cfg" cfg.config} ${stateDir}/client.cfg
63 '';
64 exec = "${pkgs.su}/bin/su -s ${pkgs.stdenv.shell} ${fahUser} -c 'cd ${stateDir}; ${pkgs.foldingathome}/bin/fah6'";
65 };
66
67 services.foldingAtHome.config = ''
68 [settings]
69 username=${cfg.nickname}
70 '';
71
72 };
73
74}