1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.services.gocd-agent; 7in { 8 options = { 9 services.gocd-agent = { 10 enable = mkEnableOption "gocd-agent"; 11 12 user = mkOption { 13 default = "gocd-agent"; 14 type = types.str; 15 description = '' 16 User the Go.CD agent should execute under. 17 ''; 18 }; 19 20 group = mkOption { 21 default = "gocd-agent"; 22 type = types.str; 23 description = '' 24 If the default user "gocd-agent" is configured then this is the primary 25 group of that user. 26 ''; 27 }; 28 29 extraGroups = mkOption { 30 type = types.listOf types.str; 31 default = [ ]; 32 example = [ "wheel" "docker" ]; 33 description = '' 34 List of extra groups that the "gocd-agent" user should be a part of. 35 ''; 36 }; 37 38 packages = mkOption { 39 default = [ pkgs.stdenv pkgs.jre pkgs.git config.programs.ssh.package pkgs.nix ]; 40 defaultText = "[ pkgs.stdenv pkgs.jre pkgs.git config.programs.ssh.package pkgs.nix ]"; 41 type = types.listOf types.package; 42 description = '' 43 Packages to add to PATH for the Go.CD agent process. 44 ''; 45 }; 46 47 agentConfig = mkOption { 48 default = ""; 49 type = types.str; 50 example = '' 51 agent.auto.register.resources=ant,java 52 agent.auto.register.environments=QA,Performance 53 agent.auto.register.hostname=Agent01 54 ''; 55 description = '' 56 Agent registration configuration. 57 ''; 58 }; 59 60 goServer = mkOption { 61 default = "https://127.0.0.1:8154/go"; 62 type = types.str; 63 description = '' 64 URL of the GoCD Server to attach the Go.CD Agent to. 65 ''; 66 }; 67 68 workDir = mkOption { 69 default = "/var/lib/go-agent"; 70 type = types.str; 71 description = '' 72 Specifies the working directory in which the Go.CD agent java archive resides. 73 ''; 74 }; 75 76 initialJavaHeapSize = mkOption { 77 default = "128m"; 78 type = types.str; 79 description = '' 80 Specifies the initial java heap memory size for the Go.CD agent java process. 81 ''; 82 }; 83 84 maxJavaHeapMemory = mkOption { 85 default = "256m"; 86 type = types.str; 87 description = '' 88 Specifies the java maximum heap memory size for the Go.CD agent java process. 89 ''; 90 }; 91 92 startupOptions = mkOption { 93 default = [ 94 "-Xms${cfg.initialJavaHeapSize}" 95 "-Xmx${cfg.maxJavaHeapMemory}" 96 "-Djava.io.tmpdir=/tmp" 97 "-Dcruise.console.publish.interval=10" 98 "-Djava.security.egd=file:/dev/./urandom" 99 ]; 100 description = '' 101 Specifies startup command line arguments to pass to Go.CD agent 102 java process. 103 ''; 104 }; 105 106 extraOptions = mkOption { 107 default = [ ]; 108 example = [ 109 "-X debug" 110 "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006" 111 "-verbose:gc" 112 "-Xloggc:go-agent-gc.log" 113 "-XX:+PrintGCTimeStamps" 114 "-XX:+PrintTenuringDistribution" 115 "-XX:+PrintGCDetails" 116 "-XX:+PrintGC" 117 ]; 118 description = '' 119 Specifies additional command line arguments to pass to Go.CD agent 120 java process. Example contains debug and gcLog arguments. 121 ''; 122 }; 123 124 environment = mkOption { 125 default = { }; 126 type = with types; attrsOf str; 127 description = '' 128 Additional environment variables to be passed to the Go.CD agent process. 129 As a base environment, Go.CD agent receives NIX_PATH from 130 <option>environment.sessionVariables</option>, NIX_REMOTE is set to 131 "daemon". 132 ''; 133 }; 134 }; 135 }; 136 137 config = mkIf cfg.enable { 138 users.extraGroups = optional (cfg.group == "gocd-agent") { 139 name = "gocd-agent"; 140 gid = config.ids.gids.gocd-agent; 141 }; 142 143 users.extraUsers = optional (cfg.user == "gocd-agent") { 144 name = "gocd-agent"; 145 description = "gocd-agent user"; 146 createHome = true; 147 home = cfg.workDir; 148 group = cfg.group; 149 extraGroups = cfg.extraGroups; 150 useDefaultShell = true; 151 uid = config.ids.uids.gocd-agent; 152 }; 153 154 systemd.services.gocd-agent = { 155 description = "GoCD Agent"; 156 after = [ "network.target" ]; 157 wantedBy = [ "multi-user.target" ]; 158 159 environment = 160 let 161 selectedSessionVars = 162 lib.filterAttrs (n: v: builtins.elem n [ "NIX_PATH" ]) 163 config.environment.sessionVariables; 164 in 165 selectedSessionVars // 166 { 167 NIX_REMOTE = "daemon"; 168 AGENT_WORK_DIR = cfg.workDir; 169 AGENT_STARTUP_ARGS = ''${concatStringsSep " " cfg.startupOptions}''; 170 LOG_DIR = cfg.workDir; 171 LOG_FILE = "${cfg.workDir}/go-agent-start.log"; 172 } // 173 cfg.environment; 174 175 path = cfg.packages; 176 177 script = '' 178 MPATH="''${PATH}"; 179 source /etc/profile 180 export PATH="''${MPATH}:''${PATH}"; 181 182 if ! test -f ~/.nixpkgs/config.nix; then 183 mkdir -p ~/.nixpkgs/ 184 echo "{ allowUnfree = true; }" > ~/.nixpkgs/config.nix 185 fi 186 187 mkdir -p config 188 rm -f config/autoregister.properties 189 ln -s "${pkgs.writeText "autoregister.properties" cfg.agentConfig}" config/autoregister.properties 190 191 ${pkgs.git}/bin/git config --global --add http.sslCAinfo /etc/ssl/certs/ca-certificates.crt 192 ${pkgs.jre}/bin/java ${concatStringsSep " " cfg.startupOptions} \ 193 ${concatStringsSep " " cfg.extraOptions} \ 194 -jar ${pkgs.gocd-agent}/go-agent/agent-bootstrapper.jar \ 195 -serverUrl ${cfg.goServer} 196 ''; 197 198 serviceConfig = { 199 User = cfg.user; 200 WorkingDirectory = cfg.workDir; 201 RestartSec = 30; 202 Restart = "on-failure"; 203 }; 204 }; 205 }; 206}