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