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 systemd.services.foldingathome = { 53 after = [ "network-interfaces.target" ]; 54 wantedBy = [ "multi-user.target" ]; 55 preStart = '' 56 mkdir -m 0755 -p ${stateDir} 57 chown ${fahUser} ${stateDir} 58 cp -f ${pkgs.writeText "client.cfg" cfg.config} ${stateDir}/client.cfg 59 ''; 60 script = "${pkgs.su}/bin/su -s ${pkgs.stdenv.shell} ${fahUser} -c 'cd ${stateDir}; ${pkgs.foldingathome}/bin/fah6'"; 61 }; 62 63 services.foldingAtHome.config = '' 64 [settings] 65 username=${cfg.nickname} 66 ''; 67 }; 68}