1 { lib, pkgs, config, myconfig, mylibs, ... }:
3 mastodon = pkgs.callPackage ./mastodon.nix {
4 inherit (mylibs) fetchedGithub yarn2nixPackage;
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 deployment.keys = mastodon.keys;
17 ids.uids.mastodon = myconfig.env.tools.mastodon.user.uid;
18 ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid;
20 users.users.mastodon = {
22 uid = config.ids.uids.mastodon;
24 description = "Mastodon user";
25 home = mastodon.varDir;
26 useDefaultShell = true;
29 users.groups.mastodon.gid = config.ids.gids.mastodon;
31 systemd.services.mastodon-streaming = {
32 description = "Mastodon Streaming";
33 wantedBy = [ "multi-user.target" ];
34 after = [ "network.target" "mastodon-web.service" ];
36 environment.NODE_ENV = "production";
37 environment.SOCKET = mastodon.nodeSocket;
39 path = [ pkgs.nodejs pkgs.bashInteractive ];
46 while [ ! -S $SOCKET ]; do
58 EnvironmentFile = "/run/keys/webapps/tools-mastodon";
63 WorkingDirectory = mastodon.railsRoot;
66 unitConfig.RequiresMountsFor = mastodon.varDir;
69 systemd.services.mastodon-web = {
70 description = "Mastodon Web app";
71 wantedBy = [ "multi-user.target" ];
72 after = [ "network.target" ];
74 environment.RAILS_ENV = "production";
75 environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
76 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
77 environment.SOCKET = mastodon.railsSocket;
79 path = [ mastodon.gems mastodon.gems.ruby pkgs.file ];
82 ./bin/bundle exec rails db:migrate
86 exec ./bin/bundle exec puma -C config/puma.rb
91 EnvironmentFile = "/run/keys/webapps/tools-mastodon";
96 WorkingDirectory = mastodon.railsRoot;
99 unitConfig.RequiresMountsFor = mastodon.varDir;
102 systemd.services.mastodon-sidekiq = {
103 description = "Mastodon Sidekiq";
104 wantedBy = [ "multi-user.target" ];
105 after = [ "network.target" "mastodon-web.service" ];
107 environment.RAILS_ENV="production";
108 environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
109 environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
110 environment.DB_POOL="5";
112 path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ];
115 exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
120 EnvironmentFile = "/run/keys/webapps/tools-mastodon";
125 WorkingDirectory = mastodon.railsRoot;
128 unitConfig.RequiresMountsFor = mastodon.varDir;
131 system.activationScripts.mastodon = {
134 install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
135 install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir} ${mastodon.varDir}/tmp/cache
139 services.myWebsites.tools.modules = [
140 "headers" "proxy" "proxy_wstunnel" "proxy_http"
142 security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
143 system.extraSystemBuilderCmds = ''
144 mkdir -p $out/webapps
145 ln -s ${mastodon.railsRoot}/public/ $out/webapps/tools_mastodon
147 services.myWebsites.tools.vhostConfs.mastodon = {
148 certName = "eldiron";
149 hosts = ["mastodon.immae.eu" ];
152 Header always set Referrer-Policy "strict-origin-when-cross-origin"
153 Header always set Strict-Transport-Security "max-age=31536000"
155 <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
156 Header always set Cache-Control "public, max-age=31536000, immutable"
161 RequestHeader set X-Forwarded-Proto "https"
165 ProxyPass /500.html !
167 ProxyPass /embed.js !
168 ProxyPass /robots.txt !
169 ProxyPass /manifest.json !
170 ProxyPass /browserconfig.xml !
171 ProxyPass /mask-icon.svg !
172 ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
173 ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
175 RewriteRule ^/api/v1/streaming/(.+)$ unix://${mastodon.nodeSocket}|http://mastodon.immae.eu/api/v1/streaming/$1 [P,NE,QSA,L]
176 RewriteRule ^/api/v1/streaming/$ unix://${mastodon.nodeSocket}|ws://mastodon.immae.eu/ [P,NE,QSA,L]
177 ProxyPass / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
178 ProxyPassReverse / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
180 Alias /system ${mastodon.varDir}
182 <Directory ${mastodon.varDir}>
189 Options -MultiViews +FollowSymlinks
192 ErrorDocument 500 /500.html
193 ErrorDocument 501 /500.html
194 ErrorDocument 502 /500.html
195 ErrorDocument 503 /500.html
196 ErrorDocument 504 /500.html