]>
Commit | Line | Data |
---|---|---|
1 | { lib, pkgs, config, myconfig, mylibs, ... }: | |
2 | let | |
3 | mastodon = pkgs.callPackage ./mastodon.nix { | |
4 | inherit (mylibs) fetchedGithub yarn2nixPackage; | |
5 | env = myconfig.env.tools.mastodon; | |
6 | }; | |
7 | ||
8 | root = "/run/current-system/webapps/tools_mastodon"; | |
9 | cfg = config.services.myWebsites.tools.mastodon; | |
10 | in { | |
11 | options.services.myWebsites.tools.mastodon = { | |
12 | enable = lib.mkEnableOption "enable mastodon's website"; | |
13 | }; | |
14 | ||
15 | config = lib.mkIf cfg.enable { | |
16 | mySecrets.keys = mastodon.keys; | |
17 | ids.uids.mastodon = myconfig.env.tools.mastodon.user.uid; | |
18 | ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid; | |
19 | ||
20 | users.users.mastodon = { | |
21 | name = "mastodon"; | |
22 | uid = config.ids.uids.mastodon; | |
23 | group = "mastodon"; | |
24 | description = "Mastodon user"; | |
25 | home = mastodon.varDir; | |
26 | useDefaultShell = true; | |
27 | }; | |
28 | ||
29 | users.groups.mastodon.gid = config.ids.gids.mastodon; | |
30 | ||
31 | systemd.services.mastodon-streaming = { | |
32 | description = "Mastodon Streaming"; | |
33 | wantedBy = [ "multi-user.target" ]; | |
34 | after = [ "network.target" "mastodon-web.service" ]; | |
35 | ||
36 | environment.NODE_ENV = "production"; | |
37 | environment.SOCKET = mastodon.nodeSocket; | |
38 | ||
39 | path = [ pkgs.nodejs pkgs.bashInteractive ]; | |
40 | ||
41 | script = '' | |
42 | exec npm run start | |
43 | ''; | |
44 | ||
45 | postStart = '' | |
46 | while [ ! -S $SOCKET ]; do | |
47 | sleep 0.5 | |
48 | done | |
49 | chmod a+w $SOCKET | |
50 | ''; | |
51 | ||
52 | postStop = '' | |
53 | rm $SOCKET | |
54 | ''; | |
55 | ||
56 | serviceConfig = { | |
57 | User = "mastodon"; | |
58 | EnvironmentFile = "/var/secrets/webapps/tools-mastodon"; | |
59 | PrivateTmp = true; | |
60 | Restart = "always"; | |
61 | TimeoutSec = 15; | |
62 | Type = "simple"; | |
63 | WorkingDirectory = mastodon.railsRoot; | |
64 | }; | |
65 | ||
66 | unitConfig.RequiresMountsFor = mastodon.varDir; | |
67 | }; | |
68 | ||
69 | systemd.services.mastodon-web = { | |
70 | description = "Mastodon Web app"; | |
71 | wantedBy = [ "multi-user.target" ]; | |
72 | after = [ "network.target" ]; | |
73 | ||
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; | |
78 | ||
79 | path = [ mastodon.gems mastodon.gems.ruby pkgs.file ]; | |
80 | ||
81 | preStart = '' | |
82 | ./bin/bundle exec rails db:migrate | |
83 | ''; | |
84 | ||
85 | script = '' | |
86 | exec ./bin/bundle exec puma -C config/puma.rb | |
87 | ''; | |
88 | ||
89 | serviceConfig = { | |
90 | User = "mastodon"; | |
91 | EnvironmentFile = "/var/secrets/webapps/tools-mastodon"; | |
92 | PrivateTmp = true; | |
93 | Restart = "always"; | |
94 | TimeoutSec = 60; | |
95 | Type = "simple"; | |
96 | WorkingDirectory = mastodon.railsRoot; | |
97 | }; | |
98 | ||
99 | unitConfig.RequiresMountsFor = mastodon.varDir; | |
100 | }; | |
101 | ||
102 | systemd.services.mastodon-sidekiq = { | |
103 | description = "Mastodon Sidekiq"; | |
104 | wantedBy = [ "multi-user.target" ]; | |
105 | after = [ "network.target" "mastodon-web.service" ]; | |
106 | ||
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"; | |
111 | ||
112 | path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ]; | |
113 | ||
114 | script = '' | |
115 | exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push | |
116 | ''; | |
117 | ||
118 | serviceConfig = { | |
119 | User = "mastodon"; | |
120 | EnvironmentFile = "/var/secrets/webapps/tools-mastodon"; | |
121 | PrivateTmp = true; | |
122 | Restart = "always"; | |
123 | TimeoutSec = 15; | |
124 | Type = "simple"; | |
125 | WorkingDirectory = mastodon.railsRoot; | |
126 | }; | |
127 | ||
128 | unitConfig.RequiresMountsFor = mastodon.varDir; | |
129 | }; | |
130 | ||
131 | system.activationScripts.mastodon = { | |
132 | deps = [ "users" ]; | |
133 | text = '' | |
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 | |
136 | ''; | |
137 | }; | |
138 | ||
139 | services.myWebsites.tools.modules = [ | |
140 | "headers" "proxy" "proxy_wstunnel" "proxy_http" | |
141 | ]; | |
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 | |
146 | ''; | |
147 | services.myWebsites.tools.vhostConfs.mastodon = { | |
148 | certName = "eldiron"; | |
149 | hosts = ["mastodon.immae.eu" ]; | |
150 | root = root; | |
151 | extraConfig = [ '' | |
152 | Header always set Referrer-Policy "strict-origin-when-cross-origin" | |
153 | Header always set Strict-Transport-Security "max-age=31536000" | |
154 | ||
155 | <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)> | |
156 | Header always set Cache-Control "public, max-age=31536000, immutable" | |
157 | Require all granted | |
158 | </LocationMatch> | |
159 | ||
160 | ProxyPreserveHost On | |
161 | RequestHeader set X-Forwarded-Proto "https" | |
162 | ||
163 | RewriteEngine On | |
164 | ||
165 | ProxyPass /500.html ! | |
166 | ProxyPass /sw.js ! | |
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) ! | |
174 | ||
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/ | |
179 | ||
180 | Alias /system ${mastodon.varDir} | |
181 | ||
182 | <Directory ${mastodon.varDir}> | |
183 | Require all granted | |
184 | Options -MultiViews | |
185 | </Directory> | |
186 | ||
187 | <Directory ${root}> | |
188 | Require all granted | |
189 | Options -MultiViews +FollowSymlinks | |
190 | </Directory> | |
191 | ||
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 | |
197 | '' ]; | |
198 | }; | |
199 | }; | |
200 | } |