1{
2 config,
3 lib,
4 options,
5 pkgs,
6 ...
7}:
8
9with lib;
10
11let
12 cfg = config.services.gocd-server;
13 opt = options.services.gocd-server;
14in
15{
16 options = {
17 services.gocd-server = {
18 enable = mkEnableOption "gocd-server";
19
20 user = mkOption {
21 default = "gocd-server";
22 type = types.str;
23 description = ''
24 User the Go.CD server should execute under.
25 '';
26 };
27
28 group = mkOption {
29 default = "gocd-server";
30 type = types.str;
31 description = ''
32 If the default user "gocd-server" is configured then this is the primary group of that user.
33 '';
34 };
35
36 extraGroups = mkOption {
37 default = [ ];
38 type = types.listOf types.str;
39 example = [
40 "wheel"
41 "docker"
42 ];
43 description = ''
44 List of extra groups that the "gocd-server" user should be a part of.
45 '';
46 };
47
48 listenAddress = mkOption {
49 default = "0.0.0.0";
50 example = "localhost";
51 type = types.str;
52 description = ''
53 Specifies the bind address on which the Go.CD server HTTP interface listens.
54 '';
55 };
56
57 port = mkOption {
58 default = 8153;
59 type = types.port;
60 description = ''
61 Specifies port number on which the Go.CD server HTTP interface listens.
62 '';
63 };
64
65 sslPort = mkOption {
66 default = 8154;
67 type = types.int;
68 description = ''
69 Specifies port number on which the Go.CD server HTTPS interface listens.
70 '';
71 };
72
73 workDir = mkOption {
74 default = "/var/lib/go-server";
75 type = types.str;
76 description = ''
77 Specifies the working directory in which the Go.CD server java archive resides.
78 '';
79 };
80
81 packages = mkOption {
82 default = [
83 pkgs.stdenv
84 pkgs.jre
85 pkgs.git
86 config.programs.ssh.package
87 pkgs.nix
88 ];
89 defaultText = literalExpression "[ pkgs.stdenv pkgs.jre pkgs.git config.programs.ssh.package pkgs.nix ]";
90 type = types.listOf types.package;
91 description = ''
92 Packages to add to PATH for the Go.CD server's process.
93 '';
94 };
95
96 initialJavaHeapSize = mkOption {
97 default = "512m";
98 type = types.str;
99 description = ''
100 Specifies the initial java heap memory size for the Go.CD server's java process.
101 '';
102 };
103
104 maxJavaHeapMemory = mkOption {
105 default = "1024m";
106 type = types.str;
107 description = ''
108 Specifies the java maximum heap memory size for the Go.CD server's java process.
109 '';
110 };
111
112 startupOptions = mkOption {
113 type = types.listOf types.str;
114 default = [
115 "-Xms${cfg.initialJavaHeapSize}"
116 "-Xmx${cfg.maxJavaHeapMemory}"
117 "-Dcruise.listen.host=${cfg.listenAddress}"
118 "-Duser.language=en"
119 "-Djruby.rack.request.size.threshold.bytes=30000000"
120 "-Duser.country=US"
121 "-Dcruise.config.dir=${cfg.workDir}/conf"
122 "-Dcruise.config.file=${cfg.workDir}/conf/cruise-config.xml"
123 "-Dcruise.server.port=${toString cfg.port}"
124 "-Dcruise.server.ssl.port=${toString cfg.sslPort}"
125 "--add-opens=java.base/java.lang=ALL-UNNAMED"
126 "--add-opens=java.base/java.util=ALL-UNNAMED"
127 ];
128 defaultText = literalExpression ''
129 [
130 "-Xms''${config.${opt.initialJavaHeapSize}}"
131 "-Xmx''${config.${opt.maxJavaHeapMemory}}"
132 "-Dcruise.listen.host=''${config.${opt.listenAddress}}"
133 "-Duser.language=en"
134 "-Djruby.rack.request.size.threshold.bytes=30000000"
135 "-Duser.country=US"
136 "-Dcruise.config.dir=''${config.${opt.workDir}}/conf"
137 "-Dcruise.config.file=''${config.${opt.workDir}}/conf/cruise-config.xml"
138 "-Dcruise.server.port=''${toString config.${opt.port}}"
139 "-Dcruise.server.ssl.port=''${toString config.${opt.sslPort}}"
140 "--add-opens=java.base/java.lang=ALL-UNNAMED"
141 "--add-opens=java.base/java.util=ALL-UNNAMED"
142 ]
143 '';
144
145 description = ''
146 Specifies startup command line arguments to pass to Go.CD server
147 java process.
148 '';
149 };
150
151 extraOptions = mkOption {
152 default = [ ];
153 type = types.listOf types.str;
154 example = [
155 "-X debug"
156 "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
157 "-verbose:gc"
158 "-Xloggc:go-server-gc.log"
159 "-XX:+PrintGCTimeStamps"
160 "-XX:+PrintTenuringDistribution"
161 "-XX:+PrintGCDetails"
162 "-XX:+PrintGC"
163 ];
164 description = ''
165 Specifies additional command line arguments to pass to Go.CD server's
166 java process. Example contains debug and gcLog arguments.
167 '';
168 };
169
170 environment = mkOption {
171 default = { };
172 type = with types; attrsOf str;
173 description = ''
174 Additional environment variables to be passed to the gocd-server process.
175 As a base environment, gocd-server receives NIX_PATH from
176 {option}`environment.sessionVariables`, NIX_REMOTE is set to
177 "daemon".
178 '';
179 };
180 };
181 };
182
183 config = mkIf cfg.enable {
184 users.groups = optionalAttrs (cfg.group == "gocd-server") {
185 gocd-server.gid = config.ids.gids.gocd-server;
186 };
187
188 users.users = optionalAttrs (cfg.user == "gocd-server") {
189 gocd-server = {
190 description = "gocd-server user";
191 createHome = true;
192 home = cfg.workDir;
193 group = cfg.group;
194 extraGroups = cfg.extraGroups;
195 useDefaultShell = true;
196 uid = config.ids.uids.gocd-server;
197 };
198 };
199
200 systemd.services.gocd-server = {
201 description = "GoCD Server";
202 after = [ "network.target" ];
203 wantedBy = [ "multi-user.target" ];
204
205 environment =
206 let
207 selectedSessionVars = lib.filterAttrs (
208 n: v: builtins.elem n [ "NIX_PATH" ]
209 ) config.environment.sessionVariables;
210 in
211 selectedSessionVars
212 // {
213 NIX_REMOTE = "daemon";
214 }
215 // cfg.environment;
216
217 path = cfg.packages;
218
219 script = ''
220 ${pkgs.git}/bin/git config --global --add http.sslCAinfo ${config.security.pki.caBundle}
221 ${pkgs.jre}/bin/java -server ${concatStringsSep " " cfg.startupOptions} \
222 ${concatStringsSep " " cfg.extraOptions} \
223 -jar ${pkgs.gocd-server}/go-server/lib/go.jar
224 '';
225
226 serviceConfig = {
227 User = cfg.user;
228 Group = cfg.group;
229 WorkingDirectory = cfg.workDir;
230 };
231 };
232 };
233}