]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - virtual/modules/websites/tools/mastodon/default.nix
d25a072967cd4eb2efddffde0d360fc7c45472b0
[perso/Immae/Config/Nix.git] / virtual / modules / websites / tools / mastodon / default.nix
1 { lib, pkgs, config, mylibs, ... }:
2 let
3 mastodon = pkgs.callPackage ./mastodon.nix {
4 inherit (mylibs) fetchedGithub checkEnv;
5 };
6
7 cfg = config.services.myWebsites.tools.mastodon;
8 in {
9 options.services.myWebsites.tools.mastodon = {
10 enable = lib.mkEnableOption "enable mastodon's website";
11 };
12
13 config = lib.mkIf cfg.enable {
14 # FIXME: Can we use dynamic users from systemd?
15 # nixos/modules/misc/ids.nix
16 ids.uids.mastodon = 399;
17 ids.gids.mastodon = 399;
18
19 users.users.mastodon = {
20 name = "mastodon";
21 uid = config.ids.uids.mastodon;
22 group = "mastodon";
23 description = "Mastodon user";
24 home = mastodon.railsRoot;
25 useDefaultShell = true;
26 };
27
28 users.groups.mastodon.gid = config.ids.gids.mastodon;
29
30 systemd.services.mastodon-streaming = {
31 description = "Mastodon Streaming";
32 wantedBy = [ "multi-user.target" ];
33 after = [ "network.target" "mastodon-web.service" ];
34
35 environment.NODE_ENV = "production";
36 environment.SOCKET = mastodon.nodeSocket;
37
38 path = [ pkgs.nodejs pkgs.bashInteractive ];
39
40 script = ''
41 exec npm run start
42 '';
43
44 postStart = ''
45 while [ ! -S $SOCKET ]; do
46 sleep 0.5
47 done
48 chmod a+w $SOCKET
49 '';
50
51 postStop = ''
52 rm $SOCKET
53 '';
54
55 serviceConfig = {
56 User = "mastodon";
57 EnvironmentFile = mastodon.config;
58 PrivateTmp = true;
59 Restart = "always";
60 TimeoutSec = 15;
61 Type = "simple";
62 WorkingDirectory = mastodon.railsRoot;
63 };
64
65 unitConfig.RequiresMountsFor = mastodon.varDir;
66 };
67
68 systemd.services.mastodon-web = {
69 description = "Mastodon Web app";
70 wantedBy = [ "multi-user.target" ];
71 after = [ "network.target" ];
72
73 environment.RAILS_ENV = "production";
74 environment.SOCKET = mastodon.railsSocket;
75
76 path = [ pkgs.bundler pkgs.file ];
77
78 preStart = ''
79 bundle exec rails db:migrate
80 '';
81
82 script = ''
83 exec bundle exec puma -C config/puma.rb
84 '';
85
86 serviceConfig = {
87 User = "mastodon";
88 EnvironmentFile = mastodon.config;
89 PrivateTmp = true;
90 Restart = "always";
91 TimeoutSec = 60;
92 Type = "simple";
93 WorkingDirectory = mastodon.railsRoot;
94 };
95
96 unitConfig.RequiresMountsFor = mastodon.varDir;
97 };
98
99 # FIXME: monitor jobs
100 systemd.services.mastodon-sidekiq = {
101 description = "Mastodon Sidekiq";
102 wantedBy = [ "multi-user.target" ];
103 after = [ "network.target" "mastodon-web.service" ];
104
105 environment.RAILS_ENV="production";
106 environment.DB_POOL="5";
107
108 path = [ pkgs.imagemagick pkgs.ffmpeg pkgs.bundler pkgs.file ];
109
110 script = ''
111 exec bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
112 '';
113
114 serviceConfig = {
115 User = "mastodon";
116 EnvironmentFile = mastodon.config;
117 PrivateTmp = true;
118 Restart = "always";
119 TimeoutSec = 15;
120 Type = "simple";
121 WorkingDirectory = mastodon.railsRoot;
122 };
123
124 unitConfig.RequiresMountsFor = mastodon.varDir;
125 };
126
127 # FIXME: initial sync
128 system.activationScripts.mastodon = {
129 deps = [ "users" ];
130 text = ''
131 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
132 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir}
133 '';
134 };
135
136 services.myWebsites.tools.modules = [
137 "headers" "proxy" "proxy_wstunnel" "proxy_http" "proxy_balancer"
138 # FIXME: probably only one balancer method is needed:
139 "lbmethod_byrequests" "lbmethod_bytraffic" "lbmethod_bybusyness" "lbmethod_heartbeat"
140 ];
141 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
142 services.myWebsites.tools.vhostConfs.mastodon = {
143 certName = "eldiron";
144 hosts = ["mastodon.immae.eu" ];
145 root = "${mastodon.railsRoot}/public/";
146 extraConfig = [ ''
147 Header always set Referrer-Policy "strict-origin-when-cross-origin"
148 Header always set Strict-Transport-Security "max-age=31536000"
149
150 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
151 Header always set Cache-Control "public, max-age=31536000, immutable"
152 Require all granted
153 </LocationMatch>
154
155 ProxyPreserveHost On
156 RequestHeader set X-Forwarded-Proto "https"
157
158 RewriteEngine On
159
160 ProxyPass /500.html !
161 ProxyPass /sw.js !
162 ProxyPass /embed.js !
163 ProxyPass /robots.txt !
164 ProxyPass /manifest.json !
165 ProxyPass /browserconfig.xml !
166 ProxyPass /mask-icon.svg !
167 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
168 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
169
170 ProxyPassMatch /api/v1/streaming/(.+)$ balancer://node_servers_http/api/v1/streaming/$1
171 ProxyPass /api/v1/streaming/ balancer://node_servers/
172 ProxyPassReverse /api/v1/streaming/ balancer://node_servers/
173 ProxyPass / balancer://puma_servers/
174 ProxyPassReverse / balancer://puma_servers/
175
176 <Proxy balancer://puma_servers>
177 BalancerMember unix://${mastodon.railsSocket}|http://
178 </Proxy>
179
180 <Proxy balancer://node_servers>
181 BalancerMember unix://${mastodon.nodeSocket}|ws://localhost
182 </Proxy>
183
184 <Proxy balancer://node_servers_http>
185 BalancerMember unix://${mastodon.nodeSocket}|http://localhost
186 </Proxy>
187
188 Alias /system ${mastodon.varDir}
189
190 <Directory ${mastodon.varDir}>
191 Require all granted
192 Options -MultiViews
193 </Directory>
194
195 <Directory ${mastodon.railsRoot}/public/>
196 Require all granted
197 Options -MultiViews +FollowSymlinks
198 </Directory>
199
200 ErrorDocument 500 /500.html
201 ErrorDocument 501 /500.html
202 ErrorDocument 502 /500.html
203 ErrorDocument 503 /500.html
204 ErrorDocument 504 /500.html
205 '' ];
206 };
207 };
208 }