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;
100 systemd.services.mastodon-sidekiq = {
101 description = "Mastodon Sidekiq";
102 wantedBy = [ "multi-user.target" ];
103 after = [ "network.target" "mastodon-web.service" ];
105 environment.RAILS_ENV="production";
106 environment.DB_POOL="5";
108 path = [ pkgs.imagemagick pkgs.ffmpeg pkgs.bundler pkgs.file ];
111 exec bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
116 EnvironmentFile = mastodon.config;
121 WorkingDirectory = mastodon.railsRoot;
124 unitConfig.RequiresMountsFor = mastodon.varDir;
127 # FIXME: initial sync
128 system.activationScripts.mastodon = {
131 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
132 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir}
136 services.myWebsites.tools.modules = [
137 "headers" "proxy" "proxy_wstunnel" "proxy_http" "proxy_balancer"
138 # FIXME: probably only one balancer method is needed:
139 "lbmethod_byrequests" "lbmethod_bytraffic" "lbmethod_bybusyness" "lbmethod_heartbeat"
141 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
142 services.myWebsites.tools.vhostConfs.mastodon = {
143 certName = "eldiron";
144 hosts = ["mastodon.immae.eu" ];
145 root = "${mastodon.railsRoot}/public/";
147 Header always set Referrer-Policy "strict-origin-when-cross-origin"
148 Header always set Strict-Transport-Security "max-age=31536000"
150 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
151 Header always set Cache-Control "public, max-age=31536000, immutable"
156 RequestHeader set X-Forwarded-Proto "https"
160 ProxyPass /500.html !
162 ProxyPass /embed.js !
163 ProxyPass /robots.txt !
164 ProxyPass /manifest.json !
165 ProxyPass /browserconfig.xml !
166 ProxyPass /mask-icon.svg !
167 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
168 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
170 ProxyPassMatch /api/v1/streaming/(.+)$ balancer://node_servers_http/api/v1/streaming/$1
171 ProxyPass /api/v1/streaming/ balancer://node_servers/
172 ProxyPassReverse /api/v1/streaming/ balancer://node_servers/
173 ProxyPass / balancer://puma_servers/
174 ProxyPassReverse / balancer://puma_servers/
176 <Proxy balancer://puma_servers>
177 BalancerMember unix://${mastodon.railsSocket}|http://
180 <Proxy balancer://node_servers>
181 BalancerMember unix://${mastodon.nodeSocket}|ws://localhost
184 <Proxy balancer://node_servers_http>
185 BalancerMember unix://${mastodon.nodeSocket}|http://localhost
188 Alias /system ${mastodon.varDir}
190 <Directory ${mastodon.varDir}>
195 <Directory ${mastodon.railsRoot}/public/>
197 Options -MultiViews +FollowSymlinks
200 ErrorDocument 500 /500.html
201 ErrorDocument 501 /500.html
202 ErrorDocument 502 /500.html
203 ErrorDocument 503 /500.html
204 ErrorDocument 504 /500.html