1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.winstone;
8
9 winstoneOpts = { name, ... }: {
10 options = {
11 name = mkOption {
12 default = name;
13 internal = true;
14 };
15
16 serviceName = mkOption {
17 type = types.str;
18 description = ''
19 The name of the systemd service. By default, it is
20 derived from the winstone instance name.
21 '';
22 };
23
24 warFile = mkOption {
25 type = types.str;
26 description = ''
27 The WAR file that Winstone should serve.
28 '';
29 };
30
31 javaPackage = mkOption {
32 type = types.package;
33 default = pkgs.jre;
34 description = ''
35 Which Java derivation to use for running Winstone.
36 '';
37 };
38
39 user = mkOption {
40 type = types.str;
41 description = ''
42 The user that should run this Winstone process and
43 own the working directory.
44 '';
45 };
46
47 group = mkOption {
48 type = types.str;
49 description = ''
50 The group that will own the working directory.
51 '';
52 };
53
54 workDir = mkOption {
55 type = types.str;
56 description = ''
57 The working directory for this Winstone instance. Will
58 contain extracted webapps etc. The directory will be
59 created if it doesn't exist.
60 '';
61 };
62
63 extraJavaOptions = mkOption {
64 type = types.listOf types.str;
65 default = [];
66 description = ''
67 Extra command line options given to the java process running
68 Winstone.
69 '';
70 };
71
72 extraOptions = mkOption {
73 type = types.listOf types.str;
74 default = [];
75 description = ''
76 Extra command line options given to the Winstone process.
77 '';
78 };
79 };
80
81 config = {
82 workDir = mkDefault "/run/winstone/${name}";
83 serviceName = mkDefault "winstone-${name}";
84 };
85 };
86
87 mkService = cfg: let
88 opts = concatStringsSep " " (cfg.extraOptions ++ [
89 "--warfile ${cfg.warFile}"
90 ]);
91
92 javaOpts = concatStringsSep " " (cfg.extraJavaOptions ++ [
93 "-Djava.io.tmpdir=${cfg.workDir}"
94 "-jar ${pkgs.winstone}/lib/winstone.jar"
95 ]);
96 in {
97 wantedBy = [ "multi-user.target" ];
98 description = "winstone service for ${cfg.name}";
99 preStart = ''
100 mkdir -p "${cfg.workDir}"
101 chown ${cfg.user}:${cfg.group} "${cfg.workDir}"
102 '';
103 serviceConfig = {
104 ExecStart = "${cfg.javaPackage}/bin/java ${javaOpts} ${opts}";
105 User = cfg.user;
106 PermissionsStartOnly = true;
107 };
108 };
109
110in {
111
112 options = {
113 services.winstone = mkOption {
114 default = {};
115 type = types.attrsOf types.optionSet;
116 options = [ winstoneOpts ];
117 description = ''
118 Defines independent Winstone services, each serving one WAR-file.
119 '';
120 };
121 };
122
123 config = mkIf (cfg != {}) {
124
125 systemd.services = mapAttrs' (n: c: nameValuePair c.serviceName (mkService c)) cfg;
126
127 };
128
129}