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