1 { lib, pkgs, config, mylibs, ... }:
3 mastodon = pkgs.callPackage ./mastodon.nix {
4 inherit (mylibs) fetchedGithub checkEnv;
7 cfg = config.services.myWebsites.tools.mastodon;
9 options.services.myWebsites.tools.mastodon = {
10 enable = lib.mkEnableOption "enable mastodon's website";
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;
19 users.users.mastodon = {
21 uid = config.ids.uids.mastodon;
23 description = "Mastodon user";
24 home = mastodon.railsRoot;
25 useDefaultShell = true;
28 users.groups.mastodon.gid = config.ids.gids.mastodon;
30 systemd.services.mastodon-streaming = {
31 description = "Mastodon Streaming";
32 wantedBy = [ "multi-user.target" ];
33 after = [ "network.target" "mastodon-web.service" ];
35 environment.NODE_ENV = "production";
36 environment.SOCKET = mastodon.nodeSocket;
38 path = [ pkgs.nodejs pkgs.bashInteractive ];
45 while [ ! -S $SOCKET ]; do
57 EnvironmentFile = mastodon.config;
62 WorkingDirectory = mastodon.railsRoot;
65 unitConfig.RequiresMountsFor = mastodon.varDir;
68 systemd.services.mastodon-web = {
69 description = "Mastodon Web app";
70 wantedBy = [ "multi-user.target" ];
71 after = [ "network.target" ];
73 environment.RAILS_ENV = "production";
74 environment.SOCKET = mastodon.railsSocket;
76 path = [ pkgs.bundler pkgs.file ];
79 bundle exec rails db:migrate
83 exec bundle exec puma -C config/puma.rb
88 EnvironmentFile = mastodon.config;
93 WorkingDirectory = mastodon.railsRoot;
96 unitConfig.RequiresMountsFor = mastodon.varDir;
99 systemd.services.mastodon-sidekiq = {
100 description = "Mastodon Sidekiq";
101 wantedBy = [ "multi-user.target" ];
102 after = [ "network.target" "mastodon-web.service" ];
104 environment.RAILS_ENV="production";
105 environment.DB_POOL="5";
107 path = [ pkgs.bundler pkgs.file ];
110 exec bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
115 EnvironmentFile = mastodon.config;
120 WorkingDirectory = mastodon.railsRoot;
123 unitConfig.RequiresMountsFor = mastodon.varDir;
126 # FIXME: initial sync
127 system.activationScripts.mastodon = {
130 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
131 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir}
135 services.myWebsites.tools.modules = [
136 "headers" "proxy" "proxy_wstunnel" "proxy_http" "proxy_balancer"
137 # FIXME: probably only one balancer method is needed:
138 "lbmethod_byrequests" "lbmethod_bytraffic" "lbmethod_bybusyness" "lbmethod_heartbeat"
140 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
141 services.myWebsites.tools.vhostConfs.mastodon = {
142 certName = "eldiron";
143 hosts = ["mastodon.immae.eu" ];
144 root = "${mastodon.railsRoot}/public/";
146 Header always set Referrer-Policy "strict-origin-when-cross-origin"
147 Header always set Strict-Transport-Security "max-age=31536000"
149 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
150 Header always set Cache-Control "public, max-age=31536000, immutable"
155 RequestHeader set X-Forwarded-Proto "https"
159 ProxyPass /500.html !
161 ProxyPass /embed.js !
162 ProxyPass /robots.txt !
163 ProxyPass /manifest.json !
164 ProxyPass /browserconfig.xml !
165 ProxyPass /mask-icon.svg !
166 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
167 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
169 ProxyPassMatch /api/v1/streaming/(.+)$ balancer://node_servers_http/api/v1/streaming/$1
170 ProxyPass /api/v1/streaming/ balancer://node_servers/
171 ProxyPassReverse /api/v1/streaming/ balancer://node_servers/
172 ProxyPass / balancer://puma_servers/
173 ProxyPassReverse / balancer://puma_servers/
175 <Proxy balancer://puma_servers>
176 BalancerMember unix://${mastodon.railsSocket}|http://
179 <Proxy balancer://node_servers>
180 BalancerMember unix://${mastodon.nodeSocket}|ws://localhost
183 <Proxy balancer://node_servers_http>
184 BalancerMember unix://${mastodon.nodeSocket}|http://localhost
187 Alias /system ${mastodon.varDir}
189 <Directory ${mastodon.varDir}>
194 <Directory ${mastodon.railsRoot}/public/>
196 Options -MultiViews +FollowSymlinks
199 ErrorDocument 500 /500.html
200 ErrorDocument 501 /500.html
201 ErrorDocument 502 /500.html
202 ErrorDocument 503 /500.html
203 ErrorDocument 504 /500.html