{ lib, pkgs, config, myconfig, mylibs, ... }:
let
mastodon = pkgs.callPackage ./mastodon.nix {
inherit (mylibs) fetchedGithub yarn2nixPackage;
env = myconfig.env.tools.mastodon;
};
root = "/run/current-system/webapps/tools_mastodon";
cfg = config.services.myWebsites.tools.mastodon;
in {
options.services.myWebsites.tools.mastodon = {
enable = lib.mkEnableOption "enable mastodon's website";
};
config = lib.mkIf cfg.enable {
deployment.keys = mastodon.keys;
ids.uids.mastodon = myconfig.env.tools.mastodon.user.uid;
ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid;
users.users.mastodon = {
name = "mastodon";
uid = config.ids.uids.mastodon;
group = "mastodon";
description = "Mastodon user";
home = mastodon.varDir;
useDefaultShell = true;
};
users.groups.mastodon.gid = config.ids.gids.mastodon;
systemd.services.mastodon-streaming = {
description = "Mastodon Streaming";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "mastodon-web.service" ];
environment.NODE_ENV = "production";
environment.SOCKET = mastodon.nodeSocket;
path = [ pkgs.nodejs pkgs.bashInteractive ];
script = ''
exec npm run start
'';
postStart = ''
while [ ! -S $SOCKET ]; do
sleep 0.5
done
chmod a+w $SOCKET
'';
postStop = ''
rm $SOCKET
'';
serviceConfig = {
User = "mastodon";
EnvironmentFile = "/run/keys/webapps/tools-mastodon";
PrivateTmp = true;
Restart = "always";
TimeoutSec = 15;
Type = "simple";
WorkingDirectory = mastodon.railsRoot;
};
unitConfig.RequiresMountsFor = mastodon.varDir;
};
systemd.services.mastodon-web = {
description = "Mastodon Web app";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment.RAILS_ENV = "production";
environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
environment.SOCKET = mastodon.railsSocket;
path = [ mastodon.gems mastodon.gems.ruby pkgs.file ];
preStart = ''
./bin/bundle exec rails db:migrate
'';
script = ''
exec ./bin/bundle exec puma -C config/puma.rb
'';
serviceConfig = {
User = "mastodon";
EnvironmentFile = "/run/keys/webapps/tools-mastodon";
PrivateTmp = true;
Restart = "always";
TimeoutSec = 60;
Type = "simple";
WorkingDirectory = mastodon.railsRoot;
};
unitConfig.RequiresMountsFor = mastodon.varDir;
};
systemd.services.mastodon-sidekiq = {
description = "Mastodon Sidekiq";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "mastodon-web.service" ];
environment.RAILS_ENV="production";
environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
environment.DB_POOL="5";
path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ];
script = ''
exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
'';
serviceConfig = {
User = "mastodon";
EnvironmentFile = "/run/keys/webapps/tools-mastodon";
PrivateTmp = true;
Restart = "always";
TimeoutSec = 15;
Type = "simple";
WorkingDirectory = mastodon.railsRoot;
};
unitConfig.RequiresMountsFor = mastodon.varDir;
};
system.activationScripts.mastodon = {
deps = [ "users" ];
text = ''
install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir} ${mastodon.varDir}/tmp/cache
'';
};
services.myWebsites.tools.modules = [
"headers" "proxy" "proxy_wstunnel" "proxy_http"
];
security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
system.extraSystemBuilderCmds = ''
mkdir -p $out/webapps
ln -s ${mastodon.railsRoot}/public/ $out/webapps/tools_mastodon
'';
services.myWebsites.tools.vhostConfs.mastodon = {
certName = "eldiron";
hosts = ["mastodon.immae.eu" ];
root = root;
extraConfig = [ ''
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Strict-Transport-Security "max-age=31536000"
Header always set Cache-Control "public, max-age=31536000, immutable"
Require all granted
ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"
RewriteEngine On
ProxyPass /500.html !
ProxyPass /sw.js !
ProxyPass /embed.js !
ProxyPass /robots.txt !
ProxyPass /manifest.json !
ProxyPass /browserconfig.xml !
ProxyPass /mask-icon.svg !
ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !
RewriteRule ^/api/v1/streaming/(.+)$ unix://${mastodon.nodeSocket}|http://mastodon.immae.eu/api/v1/streaming/$1 [P,NE,QSA,L]
RewriteRule ^/api/v1/streaming/$ unix://${mastodon.nodeSocket}|ws://mastodon.immae.eu/ [P,NE,QSA,L]
ProxyPass / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
ProxyPassReverse / unix://${mastodon.railsSocket}|http://mastodon.immae.eu/
Alias /system ${mastodon.varDir}
Require all granted
Options -MultiViews
Require all granted
Options -MultiViews +FollowSymlinks
ErrorDocument 500 /500.html
ErrorDocument 501 /500.html
ErrorDocument 502 /500.html
ErrorDocument 503 /500.html
ErrorDocument 504 /500.html
'' ];
};
};
}