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