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