about summary refs log tree commit diff
path: root/nixos/modules/services/misc/folding-at-home.nix
blob: 9f4c4645279f0a434b5e666bf58c4868eed2d934 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{ config, pkgs, ... }:
with pkgs.lib;
let
  stateDir = "/var/lib/foldingathome";
  cfg = config.services.foldingAtHome;
  fahUser = "foldingathome";
in {

  ###### interface

  options = {

    services.foldingAtHome = {

      enable = mkOption {
        default = false;
        description = ''
          Whether to enable the Folding@Home to use idle CPU time.
        '';
      };

      nickname = mkOption {
        default = "Anonymous";
        description = ''
          A unique handle for statistics.
        '';
      };

      config = mkOption {
        default = "";
        description = ''
          Extra configuration. Contents will be added verbatim to the
          configuration file.
        '';
      };

    };

  };

  ###### implementation

  config = mkIf cfg.enable {

    users.extraUsers = singleton
      { name = fahUser;
        uid = config.ids.uids.foldingAtHome;
        description = "Folding@Home user";
        home = stateDir;
      };

    jobs.foldingAtHome =
      { name = "foldingathome";

        startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        preStart =
          ''
            mkdir -m 0755 -p ${stateDir}
            chown ${fahUser} ${stateDir}
            cp -f ${pkgs.writeText "client.cfg" cfg.config} ${stateDir}/client.cfg
          '';
        exec = "${pkgs.su}/bin/su -s ${pkgs.stdenv.shell} ${fahUser} -c 'cd ${stateDir}; ${pkgs.foldingathome}/bin/fah6'";
      };

      services.foldingAtHome.config = ''
          [settings]
          username=${cfg.nickname}
      '';

  };

}