summary refs log tree commit diff
path: root/nixos/modules/services/web-servers/winstone.nix
blob: 6dab467b35ef8b1a1423813dfead2969fa93838f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.winstone;

  winstoneOpts = { name, ... }: {
    options = {
      name = mkOption {
        default = name;
        internal = true;
      };

      serviceName = mkOption {
        type = types.str;
        description = ''
          The name of the systemd service. By default, it is
          derived from the winstone instance name.
        '';
      };

      warFile = mkOption {
        type = types.str;
        description = ''
          The WAR file that Winstone should serve.
        '';
      };

      javaPackage = mkOption {
        type = types.package;
        default = pkgs.jre;
        defaultText = "pkgs.jre";
        description = ''
          Which Java derivation to use for running Winstone.
        '';
      };

      user = mkOption {
        type = types.str;
        description = ''
          The user that should run this Winstone process and
          own the working directory.
        '';
      };

      group = mkOption {
        type = types.str;
        description = ''
          The group that will own the working directory.
        '';
      };

      workDir = mkOption {
        type = types.str;
        description = ''
          The working directory for this Winstone instance. Will
          contain extracted webapps etc. The directory will be
          created if it doesn't exist.
        '';
      };

      extraJavaOptions = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Extra command line options given to the java process running
          Winstone.
        '';
      };

      extraOptions = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Extra command line options given to the Winstone process.
        '';
      };
    };

    config = {
      workDir = mkDefault "/run/winstone/${name}";
      serviceName = mkDefault "winstone-${name}";
    };
  };

  mkService = cfg: let
    opts = concatStringsSep " " (cfg.extraOptions ++ [
      "--warfile ${cfg.warFile}"
    ]);

    javaOpts = concatStringsSep " " (cfg.extraJavaOptions ++ [
      "-Djava.io.tmpdir=${cfg.workDir}"
      "-jar ${pkgs.winstone}/lib/winstone.jar"
    ]);
  in {
    wantedBy = [ "multi-user.target" ];
    description = "winstone service for ${cfg.name}";
    preStart = ''
      mkdir -p "${cfg.workDir}"
      chown ${cfg.user}:${cfg.group} "${cfg.workDir}"
    '';
    serviceConfig = {
      ExecStart = "${cfg.javaPackage}/bin/java ${javaOpts} ${opts}";
      User = cfg.user;
      PermissionsStartOnly = true;
    };
  };

in {

  options = {
    services.winstone = mkOption {
      default = {};
      type = types.attrsOf types.optionSet;
      options = [ winstoneOpts ];
      description = ''
        Defines independent Winstone services, each serving one WAR-file.
      '';
    };
  };

  config = mkIf (cfg != {}) {

    systemd.services = mapAttrs' (n: c: nameValuePair c.serviceName (mkService c)) cfg;

  };

}