]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/webapps/mediagoblin.nix
Use new withPlugin system
[perso/Immae/Config/Nix.git] / modules / webapps / mediagoblin.nix
1 { lib, pkgs, config, ... }:
2 let
3 name = "mediagoblin";
4 cfg = config.services.mediagoblin;
5
6 uid = config.ids.uids.mediagoblin;
7 gid = config.ids.gids.mediagoblin;
8
9 paste_local = pkgs.writeText "paste_local.ini" ''
10 [DEFAULT]
11 debug = false
12
13 [pipeline:main]
14 pipeline = mediagoblin
15
16 [app:mediagoblin]
17 use = egg:mediagoblin#app
18 config = ${cfg.configFile} ${cfg.package}/mediagoblin.ini
19 /mgoblin_static = ${cfg.package}/mediagoblin/static
20
21 [loggers]
22 keys = root
23
24 [handlers]
25 keys = console
26
27 [formatters]
28 keys = generic
29
30 [logger_root]
31 level = INFO
32 handlers = console
33
34 [handler_console]
35 class = StreamHandler
36 args = (sys.stderr,)
37 level = NOTSET
38 formatter = generic
39
40 [formatter_generic]
41 format = %(levelname)-7.7s [%(name)s] %(message)s
42
43 [filter:errors]
44 use = egg:mediagoblin#errors
45 debug = false
46
47 [server:main]
48 use = egg:waitress#main
49 unix_socket = ${cfg.sockets.paster}
50 unix_socket_perms = 777
51 url_scheme = https
52 '';
53 in
54 {
55 options.services.mediagoblin = {
56 enable = lib.mkEnableOption "Enable Mediagoblin’s service";
57 user = lib.mkOption {
58 type = lib.types.str;
59 default = name;
60 description = "User account under which Mediagoblin runs";
61 };
62 group = lib.mkOption {
63 type = lib.types.str;
64 default = name;
65 description = "Group under which Mediagoblin runs";
66 };
67 dataDir = lib.mkOption {
68 type = lib.types.path;
69 default = "/var/lib/${name}";
70 description = ''
71 The directory where Mediagoblin stores its data.
72 '';
73 };
74 socketsDir = lib.mkOption {
75 type = lib.types.path;
76 default = "/run/${name}";
77 description = ''
78 The directory where Mediagoblin puts runtime files and sockets.
79 '';
80 };
81 configFile = lib.mkOption {
82 type = lib.types.path;
83 description = ''
84 The configuration file path for Mediagoblin.
85 '';
86 };
87 package = lib.mkOption {
88 type = lib.types.package;
89 default = pkgs.webapps.mediagoblin;
90 example = lib.literalExample ''
91 pkgs.webapps.mediagoblin.withPlugins (p: [p.basicsearch])
92 '';
93 description = ''
94 Mediagoblin package to use.
95 '';
96 };
97 systemdStateDirectory = lib.mkOption {
98 type = lib.types.str;
99 # Use ReadWritePaths= instead if varDir is outside of /var/lib
100 default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
101 lib.strings.removePrefix "/var/lib/" cfg.dataDir;
102 description = ''
103 Adjusted Mediagoblin data directory for systemd
104 '';
105 readOnly = true;
106 };
107 systemdRuntimeDirectory = lib.mkOption {
108 type = lib.types.str;
109 # Use ReadWritePaths= instead if socketsDir is outside of /run
110 default = assert lib.strings.hasPrefix "/run/" cfg.socketsDir;
111 lib.strings.removePrefix "/run/" cfg.socketsDir;
112 description = ''
113 Adjusted Mediagoblin sockets directory for systemd
114 '';
115 readOnly = true;
116 };
117 sockets = lib.mkOption {
118 type = lib.types.attrsOf lib.types.path;
119 default = {
120 paster = "${cfg.socketsDir}/mediagoblin.sock";
121 };
122 readOnly = true;
123 description = ''
124 Mediagoblin sockets
125 '';
126 };
127 pids = lib.mkOption {
128 type = lib.types.attrsOf lib.types.path;
129 default = {
130 paster = "${cfg.socketsDir}/mediagoblin.pid";
131 celery = "${cfg.socketsDir}/mediagoblin-celeryd.pid";
132 };
133 readOnly = true;
134 description = ''
135 Mediagoblin pid files
136 '';
137 };
138 };
139
140 config = lib.mkIf cfg.enable {
141 users.users = lib.optionalAttrs (cfg.user == name) {
142 "${name}" = {
143 inherit uid;
144 group = cfg.group;
145 description = "Mediagoblin user";
146 home = cfg.dataDir;
147 useDefaultShell = true;
148 };
149 };
150 users.groups = lib.optionalAttrs (cfg.group == name) {
151 "${name}" = {
152 inherit gid;
153 };
154 };
155
156 systemd.services.mediagoblin-web = {
157 description = "Mediagoblin service";
158 wantedBy = [ "multi-user.target" ];
159 after = [ "network.target" ];
160 wants = [ "postgresql.service" "redis.service" ];
161
162 environment.SCRIPT_NAME = "/mediagoblin/";
163
164 script = ''
165 exec ./bin/paster serve \
166 ${paste_local} \
167 --pid-file=${cfg.pids.paster}
168 '';
169 preStop = ''
170 exec ./bin/paster serve \
171 --pid-file=${cfg.pids.paster} \
172 ${paste_local} stop
173 '';
174 preStart = ''
175 if [ -d ${cfg.dataDir}/plugin_static/ ]; then
176 rm ${cfg.dataDir}/plugin_static/coreplugin_basic_auth
177 ln -sf ${cfg.package}/mediagoblin/plugins/basic_auth/static ${cfg.dataDir}/plugin_static/coreplugin_basic_auth
178 fi
179 ./bin/gmg -cf ${cfg.configFile} dbupdate
180 '';
181
182 serviceConfig = {
183 User = cfg.user;
184 PrivateTmp = true;
185 Restart = "always";
186 TimeoutSec = 15;
187 Type = "simple";
188 WorkingDirectory = cfg.package;
189 RuntimeDirectory = cfg.systemdRuntimeDirectory;
190 StateDirectory= cfg.systemdStateDirectory;
191 PIDFile = cfg.pids.paster;
192 };
193
194 unitConfig.RequiresMountsFor = cfg.dataDir;
195 };
196
197 systemd.services.mediagoblin-celeryd = {
198 description = "Mediagoblin service";
199 wantedBy = [ "multi-user.target" ];
200 after = [ "network.target" "mediagoblin-web.service" ];
201
202 environment.MEDIAGOBLIN_CONFIG = cfg.configFile;
203 environment.CELERY_CONFIG_MODULE = "mediagoblin.init.celery.from_celery";
204
205 script = ''
206 exec ./bin/celery worker \
207 --logfile=${cfg.dataDir}/celery.log \
208 --loglevel=INFO
209 '';
210
211 serviceConfig = {
212 User = cfg.user;
213 PrivateTmp = true;
214 Restart = "always";
215 TimeoutSec = 60;
216 Type = "simple";
217 WorkingDirectory = cfg.package;
218 RuntimeDirectory = cfg.systemdRuntimeDirectory;
219 StateDirectory= cfg.systemdStateDirectory;
220 PIDFile = cfg.pids.celery;
221 };
222
223 unitConfig.RequiresMountsFor = cfg.dataDir;
224 };
225 };
226 }