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