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 = literalExpression "[ 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 type = types.listOf types.str; 94 default = [ 95 "-Xms${cfg.initialJavaHeapSize}" 96 "-Xmx${cfg.maxJavaHeapMemory}" 97 "-Djava.io.tmpdir=/tmp" 98 "-Dcruise.console.publish.interval=10" 99 "-Djava.security.egd=file:/dev/./urandom" 100 ]; 101 description = '' 102 Specifies startup command line arguments to pass to Go.CD agent 103 java process. 104 ''; 105 }; 106 107 extraOptions = mkOption { 108 default = [ ]; 109 type = types.listOf types.str; 110 example = [ 111 "-X debug" 112 "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006" 113 "-verbose:gc" 114 "-Xloggc:go-agent-gc.log" 115 "-XX:+PrintGCTimeStamps" 116 "-XX:+PrintTenuringDistribution" 117 "-XX:+PrintGCDetails" 118 "-XX:+PrintGC" 119 ]; 120 description = '' 121 Specifies additional command line arguments to pass to Go.CD agent 122 java process. Example contains debug and gcLog arguments. 123 ''; 124 }; 125 126 environment = mkOption { 127 default = { }; 128 type = with types; attrsOf str; 129 description = '' 130 Additional environment variables to be passed to the Go.CD agent process. 131 As a base environment, Go.CD agent receives NIX_PATH from 132 <option>environment.sessionVariables</option>, NIX_REMOTE is set to 133 "daemon". 134 ''; 135 }; 136 }; 137 }; 138 139 config = mkIf cfg.enable { 140 users.groups = optionalAttrs (cfg.group == "gocd-agent") { 141 gocd-agent.gid = config.ids.gids.gocd-agent; 142 }; 143 144 users.users = optionalAttrs (cfg.user == "gocd-agent") { 145 gocd-agent = { 146 description = "gocd-agent user"; 147 createHome = true; 148 home = cfg.workDir; 149 group = cfg.group; 150 extraGroups = cfg.extraGroups; 151 useDefaultShell = true; 152 uid = config.ids.uids.gocd-agent; 153 }; 154 }; 155 156 systemd.services.gocd-agent = { 157 description = "GoCD Agent"; 158 after = [ "network.target" ]; 159 wantedBy = [ "multi-user.target" ]; 160 161 environment = 162 let 163 selectedSessionVars = 164 lib.filterAttrs (n: v: builtins.elem n [ "NIX_PATH" ]) 165 config.environment.sessionVariables; 166 in 167 selectedSessionVars // 168 { 169 NIX_REMOTE = "daemon"; 170 AGENT_WORK_DIR = cfg.workDir; 171 AGENT_STARTUP_ARGS = ''${concatStringsSep " " cfg.startupOptions}''; 172 LOG_DIR = cfg.workDir; 173 LOG_FILE = "${cfg.workDir}/go-agent-start.log"; 174 } // 175 cfg.environment; 176 177 path = cfg.packages; 178 179 script = '' 180 MPATH="''${PATH}"; 181 source /etc/profile 182 export PATH="''${MPATH}:''${PATH}"; 183 184 if ! test -f ~/.nixpkgs/config.nix; then 185 mkdir -p ~/.nixpkgs/ 186 echo "{ allowUnfree = true; }" > ~/.nixpkgs/config.nix 187 fi 188 189 mkdir -p config 190 rm -f config/autoregister.properties 191 ln -s "${pkgs.writeText "autoregister.properties" cfg.agentConfig}" config/autoregister.properties 192 193 ${pkgs.git}/bin/git config --global --add http.sslCAinfo /etc/ssl/certs/ca-certificates.crt 194 ${pkgs.jre}/bin/java ${concatStringsSep " " cfg.startupOptions} \ 195 ${concatStringsSep " " cfg.extraOptions} \ 196 -jar ${pkgs.gocd-agent}/go-agent/agent-bootstrapper.jar \ 197 -serverUrl ${cfg.goServer} 198 ''; 199 200 serviceConfig = { 201 User = cfg.user; 202 WorkingDirectory = cfg.workDir; 203 RestartSec = 30; 204 Restart = "on-failure"; 205 }; 206 }; 207 }; 208}