1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.gollum;
7in
8
9{
10 options.services.gollum = {
11 enable = mkOption {
12 type = types.bool;
13 default = false;
14 description = "Enable the Gollum service.";
15 };
16
17 address = mkOption {
18 type = types.str;
19 default = "0.0.0.0";
20 description = "IP address on which the web server will listen.";
21 };
22
23 port = mkOption {
24 type = types.int;
25 default = 4567;
26 description = "Port on which the web server will run.";
27 };
28
29 extraConfig = mkOption {
30 type = types.lines;
31 default = "";
32 description = "Content of the configuration file";
33 };
34
35 mathjax = mkOption {
36 type = types.bool;
37 default = false;
38 description = "Enable support for math rendering using MathJax";
39 };
40
41 allowUploads = mkOption {
42 type = types.nullOr (types.enum [ "dir" "page" ]);
43 default = null;
44 description = "Enable uploads of external files";
45 };
46
47 emoji = mkOption {
48 type = types.bool;
49 default = false;
50 description = "Parse and interpret emoji tags";
51 };
52
53 branch = mkOption {
54 type = types.str;
55 default = "master";
56 example = "develop";
57 description = "Git branch to serve";
58 };
59
60 stateDir = mkOption {
61 type = types.path;
62 default = "/var/lib/gollum";
63 description = "Specifies the path of the repository directory. If it does not exist, Gollum will create it on startup.";
64 };
65
66 };
67
68 config = mkIf cfg.enable {
69
70 users.users.gollum = {
71 group = config.users.users.gollum.name;
72 description = "Gollum user";
73 createHome = false;
74 };
75
76 users.groups.gollum = { };
77
78 systemd.services.gollum = {
79 description = "Gollum wiki";
80 after = [ "network.target" ];
81 wantedBy = [ "multi-user.target" ];
82 path = [ pkgs.git ];
83
84 preStart = let
85 userName = config.users.users.gollum.name;
86 groupName = config.users.groups.gollum.name;
87 in ''
88 # All of this is safe to be run on an existing repo
89 mkdir -p ${cfg.stateDir}
90 git init ${cfg.stateDir}
91 chmod 755 ${cfg.stateDir}
92 chown -R ${userName}:${groupName} ${cfg.stateDir}
93 '';
94
95 serviceConfig = {
96 User = config.users.extraUsers.gollum.name;
97 Group = config.users.extraGroups.gollum.name;
98 PermissionsStartOnly = true;
99 ExecStart = ''
100 ${pkgs.gollum}/bin/gollum \
101 --port ${toString cfg.port} \
102 --host ${cfg.address} \
103 --config ${builtins.toFile "gollum-config.rb" cfg.extraConfig} \
104 --ref ${cfg.branch} \
105 ${optionalString cfg.mathjax "--mathjax"} \
106 ${optionalString cfg.emoji "--emoji"} \
107 ${optionalString (cfg.allowUploads != null) "--allow-uploads ${cfg.allowUploads}"} \
108 ${cfg.stateDir}
109 '';
110 };
111 };
112 };
113}