-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.nix
425 lines (376 loc) · 12.4 KB
/
configuration.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
{ pkgs, lib, config, nixpkgs, ... }:
let
enableMetrics = false;
mastoConfig = config.services.mastodon;
in
{
imports = [
./hardware-config.nix
./prometheus.nix
./modules/restic-backups.nix
];
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
system.stateVersion = "22.11";
time.timeZone = "Europe/Berlin";
console.keyMap = "de";
networking = {
hostName = "kuschelhaufen";
domain = "cuties.social";
defaultGateway6 = {
address = "fe80::1";
interface = "ens3";
};
defaultGateway = {
address = "185.232.68.1";
interface = "ens3";
};
interfaces.ens3 = {
# Netcup reccomends disabling DHCP for IPv4
useDHCP = false;
ipv6.addresses = [{
address = "2a03:4000:4e:af1::1";
prefixLength = 64;
}];
ipv4.addresses = [{
address = "185.232.70.80";
prefixLength = 22;
}];
};
nameservers = [
# Clownflare
"2606:4700:4700::1111"
"2606:4700:4700::1001"
"1.1.1.1"
"1.0.0.1"
# Goggle
"2001:4860:4860::8888"
"2001:4860:4860::8844"
"8.8.8.8"
"8.8.4.4"
];
firewall = {
enable = true;
rejectPackets = true; # Makes debugging easier
allowedTCPPorts = [ 80 443 ] ++ config.services.openssh.ports;
logRefusedConnections = false;
};
};
# Advised in the netcup docs
# https://www.netcup-wiki.de/wiki/Zus%C3%A4tzliche_IP_Adresse_konfigurieren
boot.kernel.sysctl = {
"net.ipv6.conf.default.accept_ra" = 0;
"net.ipv6.conf.default.autoconf" = 0;
"net.ipv6.conf.all.accept_ra" = 0;
"net.ipv6.conf.all.autoconf" = 0;
"net.ipv6.conf.ens3.accept_ra" = 0;
"net.ipv6.conf.ens3.autoconf" = 0;
};
environment.systemPackages = with pkgs; [
htop
nano
vim
tmux
rsync
curl
wget
bat
fd
ripgrep
neofetch
git
];
sops = {
defaultSopsFile = secrets/all.yaml;
age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
secrets =
let
mastodon.owner = mastoConfig.user;
in {
"mastodon/smtp_password" = mastodon;
"mastodon/otp_secret" = mastodon;
"mastodon/secret_key" = mastodon;
"mastodon/vapid/private_key" = mastodon;
"mastodon/vapid/public_key" = mastodon;
"kuschelhaufen-at-cuties-social" = {};
"restic-repo-password" = mastodon;
"restic-server-jules" = mastodon;
"root_password".neededForUsers = true;
};
};
nix = {
gc = {
automatic = true;
options = "--delete-older-than 7d";
};
package = pkgs.nixVersions.stable;
settings.auto-optimise-store = lib.mkDefault true;
settings.trusted-users = [ "root" "@wheel" ];
registry.nixpkgs.flake = nixpkgs;
extraOptions = ''
experimental-features = nix-command flakes
'';
};
documentation.nixos.enable = false;
users.motd = ''
_ _
___ _ _| |_(_) ___ ___
/ __| | | | __| |/ _ \/ __|
| (__| |_| | |_| | __/\__ \
\___|\__,_|\__|_|\___||___/
_ _
___ ___ ___(_) __ _| |
/ __|/ _ \ / __| |/ _` | |
_\__ \ (_) | (__| | (_| | |
(_)___/\___/ \___|_|\__,_|_|
.
;. ., ,x:
cd'NX. kX :OK,
;dkkk,Xocl,o0K0
ckkxdkk;,ol.0KKo,
..',,',cloookkd;d:;c,.c:l:c;
'clllllll,',;lkkl,:cllll;llll:c.
.lllll;;;;;;',::::llllllc,:cll,,;'
.:c,llllllllllll:,lllllllllc:,cllllll,
:kkd,lllllllllllll:;lll,llllllllllllllll.
.lkkk,llllllllllllll;:ll:'llllllllllllcl:l
'kkkkk::lll,:llllll:' :cll:';;;;;;;';:cc:,
:kkkkkkocccd,llllc;cll;..'lllllllll,.....'.
kkkkkkkkkkx ..'.
xl. ol
${config.networking.fqdn}
'';
users.mutableUsers = true;
users.users = {
root = {
hashedPasswordFile = config.sops.secrets."root_password".path;
};
isa = {
isNormalUser = true;
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGrvhqC/tZzpLMs/qy+1xNSVi2mfn8LXPIEhh7dcGn9e"
];
};
e1mo = {
isNormalUser = true;
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBfbb4m4o89EumFjE8ichX03CC/mWry0JYaz91HKVJPb e1mo"
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID9x/kL2fFqQSEyFvdEgiM2UKYAZyV1oct9alS6mweVa e1mo (ssh_0x6D617FD0A85BAADA)"
];
};
};
security.sudo = {
enable = true;
wheelNeedsPassword = false;
};
services.openssh = {
enable = true;
settings.PasswordAuthentication = false;
};
security.acme = {
acceptTerms = true;
defaults.email = "[email protected]";
};
services.mastodon =
let
secrets = config.sops.secrets;
in {
enable = true;
localDomain = "cuties.social";
configureNginx = true;
automaticMigrations = true;
streamingProcesses = 3;
secretKeyBaseFile = secrets."mastodon/secret_key".path;
vapidPrivateKeyFile = secrets."mastodon/vapid/private_key".path;
vapidPublicKeyFile = secrets."mastodon/vapid/public_key".path;
otpSecretFile = secrets."mastodon/otp_secret".path;
mediaAutoRemove = {
enable = true;
olderThanDays = 21;
};
elasticsearch = {
port = config.services.elasticsearch.port;
host = "127.0.0.1";
};
smtp = {
createLocally = false;
port = 465;
host = "telesto.host.static.dont-break.it";
user = "[email protected]";
fromAddress = "[email protected]";
authenticate = true;
passwordFile = secrets."mastodon/smtp_password".path;
};
extraConfig = {
MAX_TOOT_CHARS = "5000";
MIN_POLL_OPTIONS = "1";
MAX_POLL_OPTIONS = "10";
MAX_DISPLAY_NAME_CHARS = "100";
MAX_BIO_CHARS = "1000";
MAX_PROFILE_FIELDS = "10";
SMTP_ENABLE_STARTTLS_AUTO = "false";
SMTP_ENABLE_STARTTLS = "never";
SMTP_TLS = "true";
# Reduce absolute spam of (every request^1) mastodon and sidekiq logs
# ^1: https://docs.joinmastodon.org/admin/config/#rails_log_level
RAILS_LOG_LEVEL = "warn";
};
};
systemd.services.mastodon-import-emojis = {
description = "Import custom emojis into mastodon";
after = [ "mastodon-web.service" ];
script = ''
for category in ${pkgs.customEmojis}/*; do
filename=''${category##*/}
category_name="''${filename%%.*}"
category_name_arg="--category ''${category_name}"
if [ "$category_name" = "uncategorized" ]; then
category_name_arg=""
fi
echo "Importing ''${filename} from ''${category} with \"''${category_name_arg}\"";
${mastoConfig.package}/bin/tootctl emoji import ''${category} --overwrite ''${category_name_arg}
done
'';
# mkforce is needed since environment.PATH is already defined for all systemd services.
# However by simply using mastodon-webs entire environment, we already have imagemagic and other potential runtime dependencies already installed
environment = lib.mkForce config.systemd.services.mastodon-web.environment;
serviceConfig = {
Type = "simple";
RemainAfterExit = true;
WorkingDirectory = mastoConfig.package;
User = mastoConfig.user;
Group = mastoConfig.group;
# Comming from
# https://github.com/NixOS/nixpkgs/blob/nixos-22.05/nixos/modules/services/web-apps/mastodon.nix#L45-L86
# State directory and mode
StateDirectory = "mastodon";
StateDirectoryMode = "0750";
# Logs directory and mode
LogsDirectory = "mastodon";
LogsDirectoryMode = "0750";
ProcSubset = "pid";
ProtectProc = "invisible";
# Access write directories
UMask = "0027";
# Capabilities
CapabilityBoundingSet = "";
# Security
NoNewPrivileges = true;
# Sandboxing
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectClock = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectControlGroups = true;
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ];
RestrictNamespaces = true;
LockPersonality = true;
MemoryDenyWriteExecute = false;
RestrictRealtime = true;
RestrictSUIDSGID = true;
RemoveIPC = true;
PrivateMounts = true;
# System Call Filtering
SystemCallArchitectures = "native";
SysCallFilter = [ "@chown" "pipe" "pipe2" ];
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
};
};
services.elasticsearch = {
enable = true;
package = pkgs.elasticsearch7;
extraJavaOptions = [ "-Xms750m" "-Xmx750m" ];
# Do not spam journalctl with "Elasticsearch built-in security features are not enabled." logs on search deploy
# https://stackoverflow.com/a/68050804
extraConf = ''
xpack.security.enabled: false
'';
};
systemd.services."mastodon-search-deploy" = {
environment = lib.mkForce config.systemd.services.mastodon-web.environment;
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "elasticsearch.service" "postgresql.service" ];
requires = [ "elasticsearch.service" "postgresql.service" ];
script = ''
${mastoConfig.package}/bin/tootctl search deploy
'';
serviceConfig = {
Type = "simple";
RemainAfterExit = true;
WorkingDirectory = mastoConfig.package;
User = mastoConfig.user;
Group = mastoConfig.group;
# Comming from
# https://github.com/NixOS/nixpkgs/blob/nixos-22.05/nixos/modules/services/web-apps/mastodon.nix#L45-L86
# State directory and mode
StateDirectory = "mastodon";
StateDirectoryMode = "0750";
# Logs directory and mode
LogsDirectory = "mastodon";
LogsDirectoryMode = "0750";
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
};
};
programs.msmtp = {
enable = true;
setSendmail = false;
accounts = {
default = {
auth = true;
tls = true;
tls_starttls = false;
host = "telesto.host.static.dont-break.it";
port = 465;
user = "[email protected]";
from = "${config.networking.fqdn} <[email protected]>";
passwordeval = "cat ${config.sops.secrets."kuschelhaufen-at-cuties-social".path}";
};
};
};
systemd.services."email-notify@" = {
path = [ pkgs.util-linux ];
serviceConfig = {
ExecStart = ''
${pkgs.runtimeShell} -c "{ echo -n 'Message-ID: <$(uuidgen)@${config.networking.fqdn}>t\nSubject:[${config.networking.fqdn}] Service failed: %i\n\n' & ${pkgs.systemd}/bin/systemctl status %i;} | ${pkgs.msmtp}/bin/msmtp -v [email protected]"
'';
};
};
systemd.services.redis-mastodon.serviceConfig = {
UMask = lib.mkForce "0027"; # 0077 is default in the mastodon module
StateDirectoryMode = lib.mkForce "0750"; # 0700 is default in the mastodon module
};
restic-backups.mastodon = {
user = mastoConfig.user;
passwordFile = config.sops.secrets."restic-repo-password".path;
postgresDatabases = [ mastoConfig.database.name ];
paths = [
"/var/lib/mastodon/public-system/media_attachments" # Hardcoded in the NixOS module
(config.services.redis.servers.mastodon.settings.dir + "/dump.rdb") # Mastodon advised: https://docs.joinmastodon.org/admin/backups/#redis
];
targets = [{
user = "cutiessocial";
passwordFile = config.sops.secrets."restic-server-jules".path;
hostname = "restic.jules.f2k1.de";
}];
timerSpec = "*-*-* 05:11:00";
};
systemd.services.restic-backup-mastodon.serviceConfig.SupplementaryGroups = config.systemd.services.redis-mastodon.serviceConfig.Group;
services.nginx.clientMaxBodySize = "100m";
services.journald.extraConfig = "SystemMaxUse=512M";
services.logrotate.settings.nginx = {
frequency = "daily";
rotate = "14";
};
swapDevices = [ {
device = "/var/lib/swapfile";
size = 2*1024;
} ];
}