]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/mermet/pleroma.nix
mermet: pleroma: fix restart
[sourcephile-nix.git] / hosts / mermet / pleroma.nix
1 { pkgs, lib, config, ... }:
2 let
3 domain = "autogeree.net";
4 srv = "pleroma";
5 owner = "${srv}-${domain}";
6 db = "${srv}-${domain}";
7 port = 4000;
8 inherit (config.services) postgresql;
9 inherit (config.users) groups;
10
11 # pleroma_ctl instance gen
12 # https://git.pleroma.social/pleroma/pleroma/blob/develop/config/config.exs
13 pleroma-conf = ''
14 import Config
15
16 config :pleroma, Pleroma.Web.Endpoint,
17 url: [host: "${srv}.${domain}", scheme: "https", port: 443],
18 http: [ip: {127, 0, 0, 1}, port: ${toString port}]
19
20 config :pleroma, :http_security,
21 sts: true
22
23 config :pleroma, Pleroma.Web.WebFinger, domain: "${domain}"
24
25 # RELEASE_COOKIE="/var/lib/pleroma/.cookie" \
26 # pleroma_ctl user new $user $user+pleroma@autogeree.net --password "$password" --moderator --admin -y
27 config :pleroma, :instance,
28 name: "${domain}",
29 email: "root+${srv}@${domain}",
30 notify_email: "root+${srv}@${domain}",
31 limit: 5000,
32 registrations_open: false,
33 invites_enabled: true,
34 description: "Pleroma: An efficient and flexible fediverse server",
35 short_description: "",
36 background_image: "/images/city.jpg",
37 instance_thumbnail: "/instance/thumbnail.jpeg",
38 max_pinned_statuses: 4
39
40 config :pleroma, :media_proxy,
41 enabled: false,
42 redirect_on_failure: true
43 #base_url: "https://cache.pleroma.social"
44
45 config :pleroma, :markup,
46 allow_inline_images: true,
47 allow_headings: true,
48 allow_tables: true
49
50 # pleroma_ctl email test --to julm+pleroma@autogeree.net
51 config :pleroma, Pleroma.Emails.Mailer, [
52 adapter: Swoosh.Adapters.Sendmail,
53 enabled: true,
54 cmd_path: "/run/wrappers/bin/sendmail",
55 cmd_args: ""
56 ]
57
58 config :pleroma, Pleroma.Repo,
59 adapter: Ecto.Adapters.Postgres,
60 username: "${owner}",
61 socket_dir: "/run/postgresql",
62 database: "${db}",
63 pool_size: 10,
64 prepare: :named,
65 parameters: [
66 plan_cache_mode: "force_custom_plan"
67 ]
68
69 config :pleroma, :database, rum_enabled: false
70 config :pleroma, :instance, static_dir: "/var/lib/${srv}/static"
71 config :pleroma, Pleroma.Uploaders.Local, uploads: "/var/lib/${srv}/uploads"
72 config :pleroma, configurable_from_database: false
73 config :pleroma, Pleroma.Upload, filters: [
74 Pleroma.Upload.Filter.Exiftool.StripLocation,
75 Pleroma.Upload.Filter.Exiftool.ReadDescription
76 ]
77
78 # https://docs-develop.pleroma.social/backend/configuration/howto_proxy/
79 #config :pleroma, :http, proxy_url: {:socks5, :localhost, 9050}
80 config :pleroma, :mrf,
81 policies: [
82 Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy,
83 Pleroma.Web.ActivityPub.MRF.TagPolicy,
84 Pleroma.Web.ActivityPub.MRF.SimplePolicy
85 ]
86
87 config :pleroma, :media_proxy,
88 enabled: true,
89 proxy_opts: [
90 redirect_on_failure: true
91 ];
92 '';
93 in
94 {
95 services = {
96 pleroma = {
97 enable = true;
98 configs = [ pleroma-conf ];
99 secretConfigFile = "/run/credentials/${srv}.service/config.exs";
100 };
101 nginx = {
102 enable = true;
103 upstreams.${srv} = {
104 servers."127.0.0.1:${toString port}" = {
105 max_fails = 5;
106 fail_timeout = "60s";
107 };
108 extraConfig = ''
109 '';
110 };
111 proxyCachePath."${domain}/${srv}/proxy" = {
112 enable = true;
113 inactive = "720m";
114 keysZoneName = "${domain}/${srv}/proxy";
115 keysZoneSize = "10m";
116 levels = "1:2";
117 maxSize = "10g";
118 useTempPath = false;
119 };
120
121 virtualHosts.${domain} = {
122 locations."/.well-known/host-meta" = {
123 return = "301 https://${srv}.${domain}$request_uri";
124 };
125 };
126 virtualHosts."${srv}.${domain}" = {
127 forceSSL = true;
128 useACMEHost = domain;
129 extraConfig = ''
130 access_log /var/log/nginx/${domain}/${srv}/access.log json buffer=32k;
131 error_log /var/log/nginx/${domain}/${srv}/error.log;
132 '';
133 locations."/" = {
134 proxyPass = "http://${srv}";
135 extraConfig = ''
136 add_header 'Access-Control-Allow-Origin' '*' always;
137 add_header 'Access-Control-Allow-Methods' 'POST, PUT, DELETE, GET, PATCH, OPTIONS' always;
138 add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Idempotency-Key' always;
139 add_header 'Access-Control-Expose-Headers' 'Link, X-RateLimit-Reset, X-RateLimit-Limit, X-RateLimit-Remaining, X-Request-Id' always;
140 if ($request_method = OPTIONS) {
141 return 204;
142 }
143 add_header Referrer-Policy same-origin;
144 add_header X-Content-Type-Options nosniff;
145 add_header X-Download-Options noopen;
146 add_header X-Frame-Options DENY;
147 add_header X-Permitted-Cross-Domain-Policies none;
148 add_header X-XSS-Protection "1; mode=block";
149 client_max_body_size 16m;
150 proxy_connect_timeout 90;
151 proxy_http_version 1.1;
152 proxy_read_timeout 90;
153 proxy_redirect off;
154 proxy_send_timeout 90;
155 proxy_set_header Connection "upgrade";
156 proxy_set_header Upgrade $http_upgrade;
157 '';
158 };
159 locations."/proxy" = {
160 proxyPass = "http://${srv}";
161 extraConfig = ''
162 proxy_cache ${domain}/${srv}/proxy;
163 proxy_cache_lock on;
164 proxy_ignore_client_abort on;
165 '';
166 };
167 };
168 };
169 postgresql = {
170 identMap = ''
171 # MAPNAME SYSTEM-USERNAME PG-USERNAME
172 user root ${owner}
173 user ${srv} ${owner}
174 '';
175 };
176 sanoid.datasets."rpool/var/lib/${srv}" = {
177 use_template = [ "snap" ];
178 daily = 31;
179 monthly = 3;
180 recursive = true;
181 };
182 };
183 systemd.services = {
184 nginx = {
185 serviceConfig = {
186 LogsDirectory = lib.mkForce [ "nginx/${domain}/${srv}" ];
187 };
188 };
189 pleroma = {
190 path = [
191 pkgs.exiftool
192 # For RELEASE_COOKIE
193 pkgs.hexdump
194 # For rand()
195 pkgs.gawk
196 ];
197 environment.RELEASE_VM_ARGS = pkgs.writeText "vm.args" ''
198 # Disable the busy-waiting.
199 # https://docs-develop.pleroma.social/backend/configuration/optimizing_beam/#virtual-machine-andor-few-cpu-cores
200 +sbwt none
201 +sbwtdcpu none
202 +sbwtdio none
203 '';
204 unitConfig = {
205 StartLimitBurst = 5;
206 StartLimitIntervalSec = "600s";
207 };
208 serviceConfig = {
209 LoadCredentialEncrypted = [ "config.exs:${./pleroma/config.exs.cred}" ];
210 SupplementaryGroups = [ groups."postgres".name ];
211 TimeoutStopSec = "10s";
212 Restart = "on-failure";
213 RestartSec = "10s";
214 # For sendmail
215 NoNewPrivileges = lib.mkForce false;
216 };
217 };
218 postgresql = {
219 postStart = lib.mkAfter ''
220 connection_limit=64 \
221 encoding=UTF8 \
222 lc_collate=fr_FR.UTF-8 \
223 lc_type=fr_FR.UTF-8 \
224 owner="${owner}" \
225 pass="" \
226 pg_createdb "${db}" >/dev/null
227 pg_adduser "${db}" "${owner}" >/dev/null
228
229 $PSQL -d "${db}" -AqtX --set ON_ERROR_STOP=1 -f - <<EOF
230 --Extensions made by ecto.migrate that need superuser access
231 CREATE EXTENSION IF NOT EXISTS citext;
232 CREATE EXTENSION IF NOT EXISTS pg_trgm;
233 CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
234 EOF
235 '';
236 };
237 };
238 }