1 { lib, pkgs, pkgsPrevious, config, myconfig, mylibs, ... }:
3 mastodon = pkgsPrevious.callPackage ./mastodon.nix {
4 inherit (mylibs) fetchedGithub;
5 env = myconfig.env.tools.mastodon;
8 root = "/run/current-system/webapps/tools_mastodon";
9 cfg = config.services.myWebsites.tools.mastodon;
11 options.services.myWebsites.tools.mastodon = {
12 enable = lib.mkEnableOption "enable mastodon's website";
15 config = lib.mkIf cfg.enable {
16 ids.uids.mastodon = myconfig.env.tools.mastodon.user.uid;
17 ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid;
19 users.users.mastodon = {
21 uid = config.ids.uids.mastodon;
23 description = "Mastodon user";
24 home = mastodon.varDir;
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.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
75 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
76 environment.SOCKET = mastodon.railsSocket;
78 path = [ mastodon.gems mastodon.gems.ruby pkgs.file ];
81 ./bin/bundle exec rails db:migrate
85 exec ./bin/bundle exec puma -C config/puma.rb
90 EnvironmentFile = mastodon.config;
95 WorkingDirectory = mastodon.railsRoot;
98 unitConfig.RequiresMountsFor = mastodon.varDir;
101 systemd.services.mastodon-sidekiq = {
102 description = "Mastodon Sidekiq";
103 wantedBy = [ "multi-user.target" ];
104 after = [ "network.target" "mastodon-web.service" ];
106 environment.RAILS_ENV="production";
107 environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
108 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
109 environment.DB_POOL="5";
111 path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ];
114 exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
119 EnvironmentFile = mastodon.config;
124 WorkingDirectory = mastodon.railsRoot;
127 unitConfig.RequiresMountsFor = mastodon.varDir;
130 system.activationScripts.mastodon = {
133 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
134 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir} ${mastodon.varDir}/tmp/cache
138 services.myWebsites.tools.modules = [
139 "headers" "proxy" "proxy_wstunnel" "proxy_http"
141 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
142 system.extraSystemBuilderCmds = ''
143 mkdir -p $out/webapps
144 ln -s ${mastodon.railsRoot}/public/ $out/webapps/tools_mastodon
146 services.myWebsites.tools.vhostConfs.mastodon = {
147 certName = "eldiron";
148 hosts = ["mastodon.immae.eu" ];
151 Header always set Referrer-Policy "strict-origin-when-cross-origin"
152 Header always set Strict-Transport-Security "max-age=31536000"
154 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
155 Header always set Cache-Control "public, max-age=31536000, immutable"
160 RequestHeader set X-Forwarded-Proto "https"
164 ProxyPass /500.html !
166 ProxyPass /embed.js !
167 ProxyPass /robots.txt !
168 ProxyPass /manifest.json !
169 ProxyPass /browserconfig.xml !
170 ProxyPass /mask-icon.svg !
171 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
172 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
174 RewriteRule ^/api/v1/streaming/(.+)$ unix://${mastodon.nodeSocket}|http://mastodon.immae.eu/api/v1/streaming/$1 [P,NE,QSA,L]
175 RewriteRule ^/api/v1/streaming/$ unix://${mastodon.nodeSocket}|ws://mastodon.immae.eu/ [P,NE,QSA,L]
176 ProxyPass / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
177 ProxyPassReverse / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
179 Alias /system ${mastodon.varDir}
181 <Directory ${mastodon.varDir}>
188 Options -MultiViews +FollowSymlinks
191 ErrorDocument 500 /500.html
192 ErrorDocument 501 /500.html
193 ErrorDocument 502 /500.html
194 ErrorDocument 503 /500.html
195 ErrorDocument 504 /500.html