summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-servers/molly-brown.nix
blob: 153ace5a1aa4355984fa8089ecf4e171d171faca (plain)
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
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;

let
  cfg = config.services.molly-brown;
  settingsFormat = pkgs.formats.toml { };
  configFile = settingsFormat.generate "molly-brown.toml" cfg.settings;
in
{

  options.services.molly-brown = {

    enable = mkEnableOption "Molly-Brown Gemini server";

    port = mkOption {
      default = 1965;
      type = types.port;
      description = ''
        TCP port for molly-brown to bind to.
      '';
    };

    hostName = mkOption {
      type = types.str;
      default = config.networking.hostName;
      defaultText = literalExpression "config.networking.hostName";
      description = ''
        The hostname to respond to requests for. Requests for URLs with
        other hosts will result in a status 53 (PROXY REQUEST REFUSED)
        response.
      '';
    };

    certPath = mkOption {
      type = types.path;
      example = "/var/lib/acme/example.com/cert.pem";
      description = ''
        Path to TLS certificate. An ACME certificate and key may be
        shared with an HTTP server, but only if molly-brown has
        permissions allowing it to read such keys.

        As an example:
        ```
        systemd.services.molly-brown.serviceConfig.SupplementaryGroups =
          [ config.security.acme.certs."example.com".group ];
        ```
      '';
    };

    keyPath = mkOption {
      type = types.path;
      example = "/var/lib/acme/example.com/key.pem";
      description = "Path to TLS key. See {option}`CertPath`.";
    };

    docBase = mkOption {
      type = types.path;
      example = "/var/lib/molly-brown";
      description = "Base directory for Gemini content.";
    };

    settings = mkOption {
      inherit (settingsFormat) type;
      default = { };
      description = ''
        molly-brown configuration. Refer to
        <https://tildegit.org/solderpunk/molly-brown/src/branch/master/example.conf>
        for details on supported values.
      '';
    };

  };

  config = mkIf cfg.enable {

    services.molly-brown.settings =
      let
        logDir = "/var/log/molly-brown";
      in
      {
        Port = cfg.port;
        Hostname = cfg.hostName;
        CertPath = cfg.certPath;
        KeyPath = cfg.keyPath;
        DocBase = cfg.docBase;
        AccessLog = "${logDir}/access.log";
        ErrorLog = "${logDir}/error.log";
      };

    systemd.services.molly-brown = {
      description = "Molly Brown gemini server";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        DynamicUser = true;
        LogsDirectory = "molly-brown";
        ExecStart = "${pkgs.molly-brown}/bin/molly-brown -c ${configFile}";
        Restart = "always";
      };
    };

  };

}