]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/tools/mastodon/default.nix
Temporarily fix mastodon package
[perso/Immae/Config/Nix.git] / nixops / modules / websites / tools / mastodon / default.nix
1 { lib, pkgs, config, myconfig, mylibs, ... }:
2 let
3 mastodon = pkgs.callPackage ./mastodon.nix {
4 inherit (mylibs) fetchedGithub;
5 env = myconfig.env.tools.mastodon;
6 };
7
8 root = "/run/current-system/webapps/tools_mastodon";
9 cfg = config.services.myWebsites.tools.mastodon;
10 in {
11 options.services.myWebsites.tools.mastodon = {
12 enable = lib.mkEnableOption "enable mastodon's website";
13 };
14
15 config = lib.mkIf cfg.enable {
16 ids.uids.mastodon = myconfig.env.tools.mastodon.user.uid;
17 ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid;
18
19 users.users.mastodon = {
20 name = "mastodon";
21 uid = config.ids.uids.mastodon;
22 group = "mastodon";
23 description = "Mastodon user";
24 home = mastodon.varDir;
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.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
75 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
76 environment.SOCKET = mastodon.railsSocket;
77
78 path = [ mastodon.gems mastodon.gems.ruby pkgs.file ];
79
80 preStart = ''
81 ./bin/bundle exec rails db:migrate
82 '';
83
84 script = ''
85 exec ./bin/bundle exec puma -C config/puma.rb
86 '';
87
88 serviceConfig = {
89 User = "mastodon";
90 EnvironmentFile = mastodon.config;
91 PrivateTmp = true;
92 Restart = "always";
93 TimeoutSec = 60;
94 Type = "simple";
95 WorkingDirectory = mastodon.railsRoot;
96 };
97
98 unitConfig.RequiresMountsFor = mastodon.varDir;
99 };
100
101 systemd.services.mastodon-sidekiq = {
102 description = "Mastodon Sidekiq";
103 wantedBy = [ "multi-user.target" ];
104 after = [ "network.target" "mastodon-web.service" ];
105
106 environment.RAILS_ENV="production";
107 environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
108 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
109 environment.DB_POOL="5";
110
111 path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ];
112
113 script = ''
114 exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
115 '';
116
117 serviceConfig = {
118 User = "mastodon";
119 EnvironmentFile = mastodon.config;
120 PrivateTmp = true;
121 Restart = "always";
122 TimeoutSec = 15;
123 Type = "simple";
124 WorkingDirectory = mastodon.railsRoot;
125 };
126
127 unitConfig.RequiresMountsFor = mastodon.varDir;
128 };
129
130 system.activationScripts.mastodon = {
131 deps = [ "users" ];
132 text = ''
133 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
134 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir} ${mastodon.varDir}/tmp/cache
135 '';
136 };
137
138 services.myWebsites.tools.modules = [
139 "headers" "proxy" "proxy_wstunnel" "proxy_http"
140 ];
141 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
142 system.extraSystemBuilderCmds = ''
143 mkdir -p $out/webapps
144 ln -s ${mastodon.railsRoot}/public/ $out/webapps/tools_mastodon
145 '';
146 services.myWebsites.tools.vhostConfs.mastodon = {
147 certName = "eldiron";
148 hosts = ["mastodon.immae.eu" ];
149 root = root;
150 extraConfig = [ ''
151 Header always set Referrer-Policy "strict-origin-when-cross-origin"
152 Header always set Strict-Transport-Security "max-age=31536000"
153
154 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
155 Header always set Cache-Control "public, max-age=31536000, immutable"
156 Require all granted
157 </LocationMatch>
158
159 ProxyPreserveHost On
160 RequestHeader set X-Forwarded-Proto "https"
161
162 RewriteEngine On
163
164 ProxyPass /500.html !
165 ProxyPass /sw.js !
166 ProxyPass /embed.js !
167 ProxyPass /robots.txt !
168 ProxyPass /manifest.json !
169 ProxyPass /browserconfig.xml !
170 ProxyPass /mask-icon.svg !
171 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
172 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
173
174 RewriteRule ^/api/v1/streaming/(.+)$ unix://${mastodon.nodeSocket}|http://mastodon.immae.eu/api/v1/streaming/$1 [P,NE,QSA,L]
175 RewriteRule ^/api/v1/streaming/$ unix://${mastodon.nodeSocket}|ws://mastodon.immae.eu/ [P,NE,QSA,L]
176 ProxyPass / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
177 ProxyPassReverse / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
178
179 Alias /system ${mastodon.varDir}
180
181 <Directory ${mastodon.varDir}>
182 Require all granted
183 Options -MultiViews
184 </Directory>
185
186 <Directory ${root}>
187 Require all granted
188 Options -MultiViews +FollowSymlinks
189 </Directory>
190
191 ErrorDocument 500 /500.html
192 ErrorDocument 501 /500.html
193 ErrorDocument 502 /500.html
194 ErrorDocument 503 /500.html
195 ErrorDocument 504 /500.html
196 '' ];
197 };
198 };
199 }