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 = myconfig.env.tools.mastodon.user.uid;
16 ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid;
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.BUNDLE_PATH = "${mastodon.gems}/lib/ruby/gems/2.5.0";
74 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
75 environment.SOCKET = mastodon.railsSocket;
77 path = [ mastodon.gems mastodon.gems.ruby pkgs.file ];
80 ./bin/bundle exec rails db:migrate
84 exec ./bin/bundle exec puma -C config/puma.rb
89 EnvironmentFile = mastodon.config;
94 WorkingDirectory = mastodon.railsRoot;
97 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.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
107 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
108 environment.DB_POOL="5";
110 path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ];
113 exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
118 EnvironmentFile = mastodon.config;
123 WorkingDirectory = mastodon.railsRoot;
126 unitConfig.RequiresMountsFor = mastodon.varDir;
129 system.activationScripts.mastodon = {
132 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
133 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir} ${mastodon.varDir}/tmp/cache
137 services.myWebsites.tools.modules = [
138 "headers" "proxy" "proxy_wstunnel" "proxy_http"
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 RewriteRule ^/api/v1/streaming/(.+)$ unix://${mastodon.nodeSocket}|http://mastodon.immae.eu/api/v1/streaming/$1 [P,NE,QSA,L]
170 RewriteRule ^/api/v1/streaming/$ unix://${mastodon.nodeSocket}|ws://mastodon.immae.eu/ [P,NE,QSA,L]
171 ProxyPass / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
172 ProxyPassReverse / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
174 Alias /system ${mastodon.varDir}
176 <Directory ${mastodon.varDir}>
181 <Directory ${mastodon.railsRoot}/public/>
183 Options -MultiViews +FollowSymlinks
186 ErrorDocument 500 /500.html
187 ErrorDocument 501 /500.html
188 ErrorDocument 502 /500.html
189 ErrorDocument 503 /500.html
190 ErrorDocument 504 /500.html