]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/webapps/mediagoblin.nix
Use systemd RuntimeDirectory and StateDirectory entries to ensure runtime directory...
[perso/Immae/Config/Nix.git] / modules / webapps / mediagoblin.nix
CommitLineData
996a68c2
IB
1{ lib, pkgs, config, ... }:
2let
3 name = "mediagoblin";
4 cfg = config.services.mediagoblin;
5
6 uid = config.ids.uids.mediagoblin;
7 gid = config.ids.gids.mediagoblin;
8
996a68c2
IB
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
658822fb
IB
18 config = ${cfg.configFile} ${cfg.workdir}/mediagoblin.ini
19 /mgoblin_static = ${cfg.workdir}/mediagoblin/static
996a68c2
IB
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
658822fb 49 unix_socket = ${cfg.sockets.paster}
996a68c2
IB
50 unix_socket_perms = 777
51 url_scheme = https
52 '';
53in
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 description = ''
91 Mediagoblin package to use.
92 '';
93 };
94 plugins = lib.mkOption {
95 type = lib.types.listOf lib.types.package;
96 default = [];
97 description = ''
98 Mediagoblin plugins to use.
99 '';
100 };
658822fb
IB
101 # Output variables
102 workdir = lib.mkOption {
103 type = lib.types.package;
104 default = cfg.package.withPlugins cfg.plugins;
105 description = ''
106 Adjusted Mediagoblin package with plugins
107 '';
108 readOnly = true;
109 };
81b9ff89
IB
110 systemdStateDirectory = lib.mkOption {
111 type = lib.types.str;
112 # Use ReadWritePaths= instead if varDir is outside of /var/lib
113 default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
114 lib.strings.removePrefix "/var/lib/" cfg.dataDir;
115 description = ''
116 Adjusted Mediagoblin data directory for systemd
117 '';
118 readOnly = true;
119 };
120 systemdRuntimeDirectory = lib.mkOption {
121 type = lib.types.str;
122 # Use ReadWritePaths= instead if socketsDir is outside of /run
123 default = assert lib.strings.hasPrefix "/run/" cfg.socketsDir;
124 lib.strings.removePrefix "/run/" cfg.socketsDir;
125 description = ''
126 Adjusted Mediagoblin sockets directory for systemd
127 '';
128 readOnly = true;
129 };
658822fb
IB
130 sockets = lib.mkOption {
131 type = lib.types.attrsOf lib.types.path;
132 default = {
133 paster = "${cfg.socketsDir}/mediagoblin.sock";
134 };
135 readOnly = true;
136 description = ''
137 Mediagoblin sockets
138 '';
139 };
140 pids = lib.mkOption {
141 type = lib.types.attrsOf lib.types.path;
142 default = {
143 paster = "${cfg.socketsDir}/mediagoblin.pid";
144 celery = "${cfg.socketsDir}/mediagoblin-celeryd.pid";
145 };
146 readOnly = true;
147 description = ''
148 Mediagoblin pid files
149 '';
150 };
996a68c2
IB
151 };
152
153 config = lib.mkIf cfg.enable {
154 users.users = lib.optionalAttrs (cfg.user == name) (lib.singleton {
155 inherit name;
156 inherit uid;
157 group = cfg.group;
158 description = "Mediagoblin user";
159 home = cfg.dataDir;
160 useDefaultShell = true;
161 });
162 users.groups = lib.optionalAttrs (cfg.group == name) (lib.singleton {
163 inherit name;
164 inherit gid;
165 });
166
167 systemd.services.mediagoblin-web = {
168 description = "Mediagoblin service";
169 wantedBy = [ "multi-user.target" ];
170 after = [ "network.target" ];
171 wants = [ "postgresql.service" "redis.service" ];
172
173 environment.SCRIPT_NAME = "/mediagoblin/";
174
175 script = ''
176 exec ./bin/paster serve \
177 ${paste_local} \
658822fb 178 --pid-file=${cfg.pids.paster}
996a68c2
IB
179 '';
180 preStop = ''
181 exec ./bin/paster serve \
658822fb 182 --pid-file=${cfg.pids.paster} \
996a68c2
IB
183 ${paste_local} stop
184 '';
185 preStart = ''
186 ./bin/gmg -cf ${cfg.configFile} dbupdate
187 '';
188
189 serviceConfig = {
190 User = cfg.user;
191 PrivateTmp = true;
192 Restart = "always";
193 TimeoutSec = 15;
194 Type = "simple";
658822fb 195 WorkingDirectory = cfg.workdir;
81b9ff89
IB
196 RuntimeDirectory = cfg.systemdRuntimeDirectory;
197 StateDirectory= cfg.systemdStateDirectory;
658822fb 198 PIDFile = cfg.pids.paster;
996a68c2
IB
199 };
200
201 unitConfig.RequiresMountsFor = cfg.dataDir;
202 };
203
204 systemd.services.mediagoblin-celeryd = {
205 description = "Mediagoblin service";
206 wantedBy = [ "multi-user.target" ];
207 after = [ "network.target" "mediagoblin-web.service" ];
208
209 environment.MEDIAGOBLIN_CONFIG = cfg.configFile;
210 environment.CELERY_CONFIG_MODULE = "mediagoblin.init.celery.from_celery";
211
212 script = ''
213 exec ./bin/celery worker \
214 --logfile=${cfg.dataDir}/celery.log \
215 --loglevel=INFO
216 '';
217
218 serviceConfig = {
219 User = cfg.user;
220 PrivateTmp = true;
221 Restart = "always";
222 TimeoutSec = 60;
223 Type = "simple";
658822fb 224 WorkingDirectory = cfg.workdir;
81b9ff89
IB
225 RuntimeDirectory = cfg.systemdRuntimeDirectory;
226 StateDirectory= cfg.systemdStateDirectory;
658822fb 227 PIDFile = cfg.pids.celery;
996a68c2
IB
228 };
229
230 unitConfig.RequiresMountsFor = cfg.dataDir;
231 };
232
233 system.activationScripts.mediagoblin = {
234 deps = [ "users" ];
235 text = ''
996a68c2
IB
236 if [ -d ${cfg.dataDir}/plugin_static/ ]; then
237 rm ${cfg.dataDir}/plugin_static/coreplugin_basic_auth
658822fb 238 ln -sf ${cfg.workdir}/mediagoblin/plugins/basic_auth/static ${cfg.dataDir}/plugin_static/coreplugin_basic_auth
996a68c2
IB
239 fi
240 '';
241 };
242
243 };
244}