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 ids.uids.mastodon = 399;
15 ids.gids.mastodon = 399;
17 users.users.mastodon = {
19 uid = config.ids.uids.mastodon;
21 description = "Mastodon user";
22 home = mastodon.railsRoot;
23 useDefaultShell = true;
26 users.groups.mastodon.gid = config.ids.gids.mastodon;
28 systemd.services.mastodon-streaming = {
29 description = "Mastodon Streaming";
30 wantedBy = [ "multi-user.target" ];
31 after = [ "network.target" "mastodon-web.service" ];
33 environment.NODE_ENV = "production";
34 environment.SOCKET = mastodon.nodeSocket;
36 path = [ pkgs.nodejs pkgs.bashInteractive ];
43 while [ ! -S $SOCKET ]; do
55 EnvironmentFile = mastodon.config;
60 WorkingDirectory = mastodon.railsRoot;
63 unitConfig.RequiresMountsFor = mastodon.varDir;
66 systemd.services.mastodon-web = {
67 description = "Mastodon Web app";
68 wantedBy = [ "multi-user.target" ];
69 after = [ "network.target" ];
71 environment.RAILS_ENV = "production";
72 environment.SOCKET = mastodon.railsSocket;
74 path = [ pkgs.bundler pkgs.file ];
77 bundle exec rails db:migrate
81 exec bundle exec puma -C config/puma.rb
86 EnvironmentFile = mastodon.config;
91 WorkingDirectory = mastodon.railsRoot;
94 unitConfig.RequiresMountsFor = mastodon.varDir;
97 systemd.services.mastodon-sidekiq = {
98 description = "Mastodon Sidekiq";
99 wantedBy = [ "multi-user.target" ];
100 after = [ "network.target" "mastodon-web.service" ];
102 environment.RAILS_ENV="production";
103 environment.DB_POOL="5";
105 path = [ pkgs.imagemagick pkgs.ffmpeg pkgs.bundler pkgs.file ];
108 exec bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
113 EnvironmentFile = mastodon.config;
118 WorkingDirectory = mastodon.railsRoot;
121 unitConfig.RequiresMountsFor = mastodon.varDir;
124 system.activationScripts.mastodon = {
127 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
128 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir}
132 services.myWebsites.tools.modules = [
133 "headers" "proxy" "proxy_wstunnel" "proxy_http" "proxy_balancer"
134 "lbmethod_byrequests" "lbmethod_bytraffic" "lbmethod_bybusyness" "lbmethod_heartbeat"
136 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
137 services.myWebsites.tools.vhostConfs.mastodon = {
138 certName = "eldiron";
139 hosts = ["mastodon.immae.eu" ];
140 root = "${mastodon.railsRoot}/public/";
142 Header always set Referrer-Policy "strict-origin-when-cross-origin"
143 Header always set Strict-Transport-Security "max-age=31536000"
145 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
146 Header always set Cache-Control "public, max-age=31536000, immutable"
151 RequestHeader set X-Forwarded-Proto "https"
155 ProxyPass /500.html !
157 ProxyPass /embed.js !
158 ProxyPass /robots.txt !
159 ProxyPass /manifest.json !
160 ProxyPass /browserconfig.xml !
161 ProxyPass /mask-icon.svg !
162 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
163 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
165 ProxyPassMatch /api/v1/streaming/(.+)$ balancer://node_servers_http/api/v1/streaming/$1
166 ProxyPass /api/v1/streaming/ balancer://node_servers/
167 ProxyPassReverse /api/v1/streaming/ balancer://node_servers/
168 ProxyPass / balancer://puma_servers/
169 ProxyPassReverse / balancer://puma_servers/
171 <Proxy balancer://puma_servers>
172 BalancerMember unix://${mastodon.railsSocket}|http://
175 <Proxy balancer://node_servers>
176 BalancerMember unix://${mastodon.nodeSocket}|ws://localhost
179 <Proxy balancer://node_servers_http>
180 BalancerMember unix://${mastodon.nodeSocket}|http://localhost
183 Alias /system ${mastodon.varDir}
185 <Directory ${mastodon.varDir}>
190 <Directory ${mastodon.railsRoot}/public/>
192 Options -MultiViews +FollowSymlinks
195 ErrorDocument 500 /500.html
196 ErrorDocument 501 /500.html
197 ErrorDocument 502 /500.html
198 ErrorDocument 503 /500.html
199 ErrorDocument 504 /500.html