1 { lib, pkgs, config, myconfig, mylibs, ... }:
3 mastodon = pkgs.callPackage ./mastodon.nix {
4 inherit (mylibs) fetchedGithub;
5 env = myconfig.env.tools.mastodon;
8 cfg = config.services.myWebsites.tools.mastodon;
10 options.services.myWebsites.tools.mastodon = {
11 enable = lib.mkEnableOption "enable mastodon's website";
14 config = lib.mkIf cfg.enable {
15 ids.uids.mastodon = 399;
16 ids.gids.mastodon = 399;
18 users.users.mastodon = {
20 uid = config.ids.uids.mastodon;
22 description = "Mastodon user";
23 home = mastodon.railsRoot;
24 useDefaultShell = true;
27 users.groups.mastodon.gid = config.ids.gids.mastodon;
29 systemd.services.mastodon-streaming = {
30 description = "Mastodon Streaming";
31 wantedBy = [ "multi-user.target" ];
32 after = [ "network.target" "mastodon-web.service" ];
34 environment.NODE_ENV = "production";
35 environment.SOCKET = mastodon.nodeSocket;
37 path = [ pkgs.nodejs pkgs.bashInteractive ];
44 while [ ! -S $SOCKET ]; do
56 EnvironmentFile = mastodon.config;
61 WorkingDirectory = mastodon.railsRoot;
64 unitConfig.RequiresMountsFor = mastodon.varDir;
67 systemd.services.mastodon-web = {
68 description = "Mastodon Web app";
69 wantedBy = [ "multi-user.target" ];
70 after = [ "network.target" ];
72 environment.RAILS_ENV = "production";
73 environment.SOCKET = mastodon.railsSocket;
75 path = [ pkgs.bundler pkgs.file ];
78 bundle exec rails db:migrate
82 exec bundle exec puma -C config/puma.rb
87 EnvironmentFile = mastodon.config;
92 WorkingDirectory = mastodon.railsRoot;
95 unitConfig.RequiresMountsFor = mastodon.varDir;
98 systemd.services.mastodon-sidekiq = {
99 description = "Mastodon Sidekiq";
100 wantedBy = [ "multi-user.target" ];
101 after = [ "network.target" "mastodon-web.service" ];
103 environment.RAILS_ENV="production";
104 environment.DB_POOL="5";
106 path = [ pkgs.imagemagick pkgs.ffmpeg pkgs.bundler pkgs.file ];
109 exec bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
114 EnvironmentFile = mastodon.config;
119 WorkingDirectory = mastodon.railsRoot;
122 unitConfig.RequiresMountsFor = mastodon.varDir;
125 system.activationScripts.mastodon = {
128 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
129 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir}
133 services.myWebsites.tools.modules = [
134 "headers" "proxy" "proxy_wstunnel" "proxy_http" "proxy_balancer"
135 "lbmethod_byrequests" "lbmethod_bytraffic" "lbmethod_bybusyness" "lbmethod_heartbeat"
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/";
143 Header always set Referrer-Policy "strict-origin-when-cross-origin"
144 Header always set Strict-Transport-Security "max-age=31536000"
146 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
147 Header always set Cache-Control "public, max-age=31536000, immutable"
152 RequestHeader set X-Forwarded-Proto "https"
156 ProxyPass /500.html !
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) !
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/
172 <Proxy balancer://puma_servers>
173 BalancerMember unix://${mastodon.railsSocket}|http://
176 <Proxy balancer://node_servers>
177 BalancerMember unix://${mastodon.nodeSocket}|ws://localhost
180 <Proxy balancer://node_servers_http>
181 BalancerMember unix://${mastodon.nodeSocket}|http://localhost
184 Alias /system ${mastodon.varDir}
186 <Directory ${mastodon.varDir}>
191 <Directory ${mastodon.railsRoot}/public/>
193 Options -MultiViews +FollowSymlinks
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