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