config/configuration.nix

766 lines
22 KiB
Nix
Raw Normal View History

2023-04-03 16:50:52 +03:00
{
config,
pkgs,
lib,
...
}: let
2023-02-26 07:44:06 +02:00
gitea_uidgid = 995;
2023-03-01 14:55:57 +02:00
2023-03-01 15:05:58 +02:00
tailscale_subnet = {
2023-04-03 16:50:52 +03:00
cidr = "100.89.176.0/20";
range = "100.89.176.0-100.89.191.255";
2023-03-01 15:05:58 +02:00
};
2023-03-01 14:55:57 +02:00
2023-03-21 15:01:06 +02:00
ips = {
2023-03-25 16:27:37 +02:00
vno1 = "88.223.107.21";
2023-03-21 15:01:06 +02:00
hel1a = "65.21.7.119";
};
2023-01-17 15:43:21 +02:00
ssh_pubkeys = {
2023-01-03 12:31:37 +02:00
motiejus = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC+qpaaD+FCYPcUU1ONbw/ff5j0xXu5DNvp/4qZH/vOYwG13uDdfI5ISYPs8zNaVcFuEDgNxWorVPwDw4p6+1JwRLlhO4J/5tE1w8Gt6C7y76LRWnp0rCdva5vL3xMozxYIWVOAiN131eyirV2FdOaqTwPy4ouNMmBFbibLQwBna89tbFMG/jwR7Cxt1I6UiYOuCXIocI5YUbXlsXoK9gr5yBRoTjl2OfH2itGYHz9xQCswvatmqrnteubAbkb6IUFYz184rnlVntuZLwzM99ezcG4v8/485gWkotTkOgQIrGNKgOA7UNKpQNbrwdPAMugqfSTo6g8fEvy0Q+6OXdxw5X7en2TJE+BLVaXp4pVMdOAzKF0nnssn64sRhsrUtFIjNGmOWBOR2gGokaJcM6x9R72qxucuG5054pSibs32BkPEg6Qzp+Bh77C3vUmC94YLVg6pazHhLroYSP1xQjfOvXyLxXB1s9rwJcO+s4kqmInft2weyhfaFE0Bjcoc+1/dKuQYfPCPSB//4zvktxTXud80zwWzMy91Q4ucRrHTBz3PrhO8ys74aSGnKOiG3ccD3HbaT0Ff4qmtIwHcAjrnNlINAcH/A2mpi0/2xA7T8WpFnvgtkQbcMF0kEKGnNS5ULZXP/LC8BlLXxwPdqTzvKikkTb661j4PhJhinhVwnQ==";
vno1_root = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMiWb7yeSeuFCMZWarKJD6ZSxIlpEHbU++MfpOIy/2kh";
2023-04-03 16:50:52 +03:00
};
2023-01-26 19:05:59 +02:00
backup_paths = {
var_lib = {
mountpoint = "/var/lib";
zfs_name = "rpool/nixos/var/lib";
paths = [
"/var/lib/.snapshot-latest/gitea"
"/var/lib/.snapshot-latest/headscale"
2023-03-21 13:57:51 +02:00
"/var/lib/.snapshot-latest/matrix-synapse"
2023-01-26 19:05:59 +02:00
];
2023-03-30 16:12:18 +03:00
backup_at = "*-*-* 00:11:00";
2023-01-26 19:05:59 +02:00
};
var_log = {
mountpoint = "/var/log";
zfs_name = "rpool/nixos/var/log";
2023-04-03 16:50:52 +03:00
paths = ["/var/log/.snapshot-latest/caddy/"];
2023-01-26 20:57:45 +02:00
patterns = [
2023-03-26 14:13:30 +03:00
"+ /var/log/.snapshot-latest/caddy/access-jakstys.lt.log-*.zst"
2023-01-26 20:57:45 +02:00
"- *"
];
2023-03-30 16:12:18 +03:00
backup_at = "*-*-* 00:10:00";
2023-01-26 19:05:59 +02:00
};
};
2023-03-01 14:55:57 +02:00
turn_cert_dir = "/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/turn.jakstys.lt";
# functions
2023-04-03 16:50:52 +03:00
mountLatest = (
{
mountpoint,
zfs_name,
}: ''
set -euo pipefail
${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest &>/dev/null || :
mkdir -p ${mountpoint}/.snapshot-latest
${pkgs.util-linux}/bin/mount -t zfs $(${pkgs.zfs}/bin/zfs list -H -t snapshot -o name ${zfs_name} | sort | tail -1) ${mountpoint}/.snapshot-latest
2023-03-01 14:55:57 +02:00
''
);
2023-04-03 16:50:52 +03:00
umountLatest = (
{mountpoint, ...}: '' set -euo pipefail
${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest
2023-03-01 14:55:57 +02:00
''
);
2023-01-26 19:05:59 +02:00
in {
2023-04-03 16:50:52 +03:00
imports = [
./hardware-configuration.nix
./zfs.nix
];
2023-01-03 12:31:37 +02:00
2023-04-03 16:50:52 +03:00
nixpkgs.overlays = [
(self: super: {
systemd = super.systemd.overrideAttrs (old: {
patches =
(old.patches or [])
++ [
(super.fetchpatch {
url = "https://github.com/systemd/systemd/commit/e7f64b896201da4a11da158c35865604cf02062f.patch";
sha256 = "sha256-AvBkrD9n5ux1o167yKg1eJK8C300vBS/ks3Gbvy5vjw=";
})
];
});
})
];
2023-01-04 10:06:52 +02:00
boot.initrd.network = {
2023-01-03 12:31:37 +02:00
enable = true;
2023-01-04 10:06:52 +02:00
ssh = {
enable = true;
port = 22;
authorizedKeys = builtins.attrValues ssh_pubkeys;
2023-04-03 16:50:52 +03:00
hostKeys = ["/etc/secrets/initrd/ssh_host_ed25519_key"];
2023-01-04 10:06:52 +02:00
};
2023-01-03 12:31:37 +02:00
};
security = {
sudo = {
wheelNeedsPassword = false;
execWheelOnly = true;
};
2023-01-03 12:31:37 +02:00
};
time.timeZone = "UTC";
2023-01-04 10:06:52 +02:00
users = {
mutableUsers = false;
2023-01-03 12:31:37 +02:00
2023-01-04 10:06:52 +02:00
users = {
git = {
description = "Gitea Service";
home = "/var/lib/gitea";
useDefaultShell = true;
group = "gitea";
isSystemUser = true;
2023-02-26 07:44:06 +02:00
uid = gitea_uidgid;
2023-01-04 10:06:52 +02:00
};
motiejus = {
isNormalUser = true;
2023-04-03 16:50:52 +03:00
extraGroups = ["wheel"];
2023-02-26 07:44:06 +02:00
uid = 1000;
2023-04-03 16:50:52 +03:00
openssh.authorizedKeys.keys = [ssh_pubkeys.motiejus];
2023-01-04 10:06:52 +02:00
};
};
2023-01-03 12:31:37 +02:00
groups.gitea.gid = gitea_uidgid;
};
2023-01-03 12:31:37 +02:00
environment = {
systemPackages = with pkgs; [
jq
git
dig
wget
tree
lsof
file
tmux
htop
#ncdu
nmap
ipset
p7zip
pwgen
parted
sqlite
direnv
vimv-rs
openssl
ripgrep
bsdgames
binutils
moreutils
headscale
mailutils
nixos-option
2023-03-30 16:12:18 +03:00
unixtools.xxd
graphicsmagick
];
variables = {
EDITOR = "nvim";
2023-01-11 09:47:50 +02:00
};
};
programs = {
mtr.enable = true;
mosh.enable = true;
neovim = {
enable = true;
defaultEditor = true;
2023-01-11 09:47:50 +02:00
};
ssh.knownHosts = {
"vno1-oh2.servers.jakst" = {
2023-04-03 16:50:52 +03:00
extraHostNames = ["dl.jakstys.lt" "vno1-oh2.jakstys.lt"];
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHtYsaht57g2sp6UmLHqsCK+fHjiiZ0rmGceFmFt88pY";
};
"hel1-a.servers.jakst" = {
2023-04-03 16:50:52 +03:00
extraHostNames = ["hel1-a.jakstys.lt" "git.jakstys.lt" "vpn.jakstys.lt" "jakstys.lt" "www.jakstys.lt"];
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF6Wd2lKrpP2Gqul10obMo2dc1xKaaLv0I4FAnfIaFKu";
};
"mtwork.motiejus.jakst" = {
2023-04-03 16:50:52 +03:00
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOvNuABV5KXmh6rmS+R50XeJ9/V+Sgpuc1DrlYXW2bQb";
};
"zh2769.rsync.net" = {
2023-04-03 16:50:52 +03:00
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJtclizeBy1Uo3D86HpgD3LONGVH0CJ0NT+YfZlldAJd";
};
"github.com" = {
2023-04-03 16:50:52 +03:00
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
};
"git.sr.ht" = {
2023-04-03 16:50:52 +03:00
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMZvRd4EtM7R+IHVMWmDkVU3VLQTSwQDSAvW0t2Tkj60";
};
2023-02-25 15:55:08 +02:00
};
2023-01-11 09:47:50 +02:00
};
2023-01-03 12:31:37 +02:00
2023-01-04 10:06:52 +02:00
services = {
tailscale.enable = true;
2023-01-04 10:06:52 +02:00
zfs = {
autoScrub.enable = true;
trim.enable = true;
expandOnBoot = "all";
};
2023-01-03 12:31:37 +02:00
openssh = {
enable = true;
passwordAuthentication = false;
permitRootLogin = "no";
extraConfig = ''
AcceptEnv GIT_PROTOCOL
'';
};
locate = {
enable = true;
locate = pkgs.plocate;
localuser = null;
};
sanoid = {
enable = true;
templates.prod = {
hourly = 24;
daily = 7;
autosnap = true;
autoprune = true;
};
2023-04-03 16:50:52 +03:00
datasets =
lib.mapAttrs' (name: value: {
2023-01-26 19:05:59 +02:00
name = value.zfs_name;
2023-04-03 16:50:52 +03:00
value = {use_template = ["prod"];};
})
backup_paths;
extraArgs = ["--verbose"];
2023-01-15 18:54:39 +02:00
};
2023-04-03 16:50:52 +03:00
borgbackup.jobs = lib.mapAttrs' (name: value: let
snapshot = {
mountpoint = value.mountpoint;
zfs_name = value.zfs_name;
};
rwpath = value.mountpoint + "/.snapshot-latest";
in {
name = name;
value =
{
2023-01-26 19:05:59 +02:00
doInit = true;
2023-02-25 15:52:23 +02:00
repo = "zh2769@zh2769.rsync.net:hel1-a.servers.jakst";
2023-01-26 19:05:59 +02:00
encryption = {
mode = "repokey-blake2";
passCommand = "cat /var/src/secrets/borgbackup/password";
};
paths = value.paths;
2023-01-26 20:26:58 +02:00
extraArgs = "--remote-path=borg1";
2023-01-26 19:05:59 +02:00
compression = "auto,lzma";
2023-01-26 20:57:45 +02:00
startAt = value.backup_at;
2023-04-03 16:50:52 +03:00
readWritePaths = [rwpath];
2023-01-26 19:05:59 +02:00
preHook = mountLatest snapshot;
postHook = umountLatest snapshot;
2023-01-26 20:57:45 +02:00
prune.keep = {
within = "1d";
daily = 7;
weekly = 4;
monthly = 3;
};
2023-04-03 16:50:52 +03:00
}
// lib.optionalAttrs (value ? patterns) {
2023-01-26 19:05:59 +02:00
patterns = value.patterns;
2023-01-15 18:54:39 +02:00
};
2023-04-03 16:50:52 +03:00
}) backup_paths;
2023-01-04 10:06:52 +02:00
headscale = {
enable = true;
serverUrl = "https://vpn.jakstys.lt";
openIdConnect = {
issuer = "https://git.jakstys.lt/";
clientId = "1c5fe796-452c-458d-b295-71a9967642fc";
clientSecretFile = "/var/lib/headscale/oidc_client_secret"; # https://github.com/juanfont/headscale/pull/1127
2023-01-04 10:06:52 +02:00
};
settings = {
ip_prefixes = [
2023-03-01 15:05:58 +02:00
tailscale_subnet.cidr
"fd7a:115c:a1e0:59b0::/64"
];
2023-01-04 10:06:52 +02:00
dns_config = {
2023-04-03 16:50:52 +03:00
nameservers = ["1.1.1.1" "8.8.4.4"];
2023-01-04 10:06:52 +02:00
magic_dns = true;
base_domain = "jakst";
};
2023-01-03 12:31:37 +02:00
};
};
2023-01-04 10:06:52 +02:00
gitea = {
enable = true;
user = "git";
database.user = "git";
domain = "git.jakstys.lt";
rootUrl = "https://git.jakstys.lt";
httpAddress = "127.0.0.1";
httpPort = 3000;
settings = {
admin.DISABLE_REGULAR_ORG_CREATION = true;
api.ENABLE_SWAGGER = false;
mirror.ENABLED = false;
other.SHOW_FOOTER_VERSION = false;
packages.ENABLED = false;
2023-02-21 16:29:27 +02:00
repository = {
DEFAULT_REPO_UNITS = "repo.code,repo.releases";
DISABLE_MIGRATIONS = true;
DISABLE_STARS = true;
ENABLE_PUSH_CREATE_USER = true;
};
2023-01-04 10:06:52 +02:00
security.LOGIN_REMEMBER_DAYS = 30;
2023-02-21 16:29:27 +02:00
server = {
ENABLE_GZIP = true;
LANDING_PAGE = "/motiejus";
};
service = {
DISABLE_REGISTRATION = true;
ENABLE_TIMETRACKING = false;
ENABLE_USER_HEATMAP = false;
SHOW_MILESTONES_DASHBOARD_PAGE = false;
COOKIE_SECURE = true;
};
2023-03-01 14:39:48 +02:00
log.LEVEL = "Error";
2023-02-21 16:29:27 +02:00
# TODO: does not work with 1.7.4, getting error
# in the UI when testing the email sending workflow.
#mailer = {
# ENABLED = true;
# MAILER_TYPE = "sendmail";
# FROM = "<noreply@jakstys.lt>";
# SENDMAIL_PATH = "${pkgs.system-sendmail}/bin/sendmail";
#};
2023-01-04 10:06:52 +02:00
"service.explore".DISABLE_USERS_PAGE = true;
};
};
2023-01-03 18:23:12 +02:00
2023-01-04 10:06:52 +02:00
caddy = {
enable = true;
email = "motiejus+acme@jakstys.lt";
virtualHosts."vpn.jakstys.lt".extraConfig = ''
reverse_proxy 127.0.0.1:8080
'';
virtualHosts."git.jakstys.lt".extraConfig = ''
reverse_proxy 127.0.0.1:3000
'';
virtualHosts."turn.jakstys.lt".extraConfig = ''
redir https://jakstys.lt
'';
2023-03-26 14:13:30 +03:00
virtualHosts."www.jakstys.lt".extraConfig = ''
redir https://jakstys.lt
'';
virtualHosts."jakstys.lt" = {
2023-01-17 15:12:08 +02:00
logFormat = ''
2023-04-03 16:50:52 +03:00
output file ${config.services.caddy.logDir}/access-jakstys.lt.log {
roll_disabled
}
2023-01-17 15:12:08 +02:00
'';
extraConfig = ''
header /_/* Cache-Control "public, max-age=31536000, immutable"
root * /var/www/jakstys.lt
file_server {
precompressed br gzip
}
2023-01-17 15:12:08 +02:00
@matrixMatch {
path /.well-known/matrix/client
path /.well-known/matrix/server
}
header @matrixMatch Content-Type application/json
header @matrixMatch Access-Control-Allow-Origin *
header @matrixMatch Cache-Control "public, max-age=3600, immutable"
2023-01-17 15:12:08 +02:00
handle /.well-known/matrix/client {
respond "{\"m.homeserver\": {\"base_url\": \"https://jakstys.lt\"}}" 200
}
handle /.well-known/matrix/server {
respond "{\"m.server\": \"jakstys.lt:443\"}" 200
}
2023-01-17 15:12:08 +02:00
handle /_matrix/* {
2023-03-04 10:10:31 +02:00
encode gzip
2023-03-26 15:40:36 +03:00
reverse_proxy http://127.0.0.1:8008
2023-01-17 15:12:08 +02:00
}
'';
};
2023-01-03 18:39:41 +02:00
};
coturn = {
enable = true;
2023-03-01 13:06:53 +02:00
min-port = 49152;
max-port = 49999;
2023-03-04 10:10:31 +02:00
no-tcp-relay = true;
realm = "turn.jakstys.lt";
2023-03-01 13:00:27 +02:00
cert = "/run/coturn/tls-cert.pem";
pkey = "/run/coturn/tls-key.pem";
2023-03-04 06:18:44 +02:00
static-auth-secret-file = "\${CREDENTIALS_DIRECTORY}/static-auth-secret";
2023-03-01 15:05:58 +02:00
extraConfig = ''
2023-03-04 06:18:44 +02:00
verbose
no-multicast-peers
2023-03-01 15:05:58 +02:00
denied-peer-ip=10.0.0.0-10.255.255.255
denied-peer-ip=192.168.0.0-192.168.255.255
denied-peer-ip=172.16.0.0-172.31.255.255
denied-peer-ip=${tailscale_subnet.range}
'';
};
# TODO: app_service_config_files
2023-03-08 16:03:30 +02:00
matrix-synapse = {
2023-03-21 13:43:34 +02:00
enable = true;
2023-03-08 16:03:30 +02:00
settings = {
server_name = "jakstys.lt";
admin_contact = "motiejus@jakstys.lt";
enable_registration = false;
report_stats = true;
signing_key_path = "/run/matrix-synapse/jakstys.lt.signing.key";
2023-04-03 16:50:52 +03:00
extraConfigFiles = ["/run/matrix-synapse/secrets.yaml"];
2023-03-19 21:13:48 +02:00
log_config = pkgs.writeText "log.config" ''
2023-03-08 16:03:30 +02:00
version: 1
formatters:
precise:
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
formatter: precise
loggers:
synapse.storage.SQL:
level: INFO
root:
level: INFO
handlers: [console]
disable_existing_loggers: false
'';
public_baseurl = "https://jakstys.lt/";
database.name = "sqlite3";
url_preview_enabled = false;
max_upload_size = "50M";
turn_allow_guests = false;
turn_uris = [
"turn:turn.jakstys.lt:3487?transport=udp"
"turn:turn.jakstys.lt:3487?transport=tcp"
"turns:turn.jakstys.lt:5349?transport=udp"
"turns:turn.jakstys.lt:5349?transport=tcp"
];
rc_messages_per_second = 0.2;
rc_message_burst_count = 10.0;
federation_rc_window_size = 1000;
federation_rc_sleep_limit = 10;
federation_rc_sleep_delay = 500;
federation_rc_reject_limit = 50;
federation_rc_concurrent = 3;
2023-03-19 21:13:48 +02:00
allow_profile_lookup_over_federation = false;
thumbnail_sizes = [
2023-04-03 16:50:52 +03:00
{
width = 32;
height = 32;
method = "crop";
}
{
width = 96;
height = 96;
method = "crop";
}
{
width = 320;
height = 240;
method = "scale";
}
{
width = 640;
height = 480;
method = "scale";
}
{
width = 800;
height = 600;
method = "scale";
}
2023-03-19 21:13:48 +02:00
];
user_directory = {
enabled = true;
search_all_users = false;
prefer_local_users = true;
};
allow_device_name_lookup_over_federation = false;
email = {
smtp_host = "127.0.0.1";
smtp_port = 25;
notf_for_new_users = false;
notif_from = "Jakstys %(app)s homeserver <noreply@jakstys.lt>";
};
include_profile_data_on_invite = false;
password_config.enabled = true;
require_auth_for_profile_requests = true;
2023-03-08 16:03:30 +02:00
};
};
postfix = {
enable = true;
enableSmtp = true;
2023-04-03 16:50:52 +03:00
networks = ["127.0.0.1/8" "[::ffff:127.0.0.0]/104" "[::1]/128" tailscale_subnet.cidr];
2023-03-04 10:10:31 +02:00
hostname = "${config.networking.hostName}.${config.networking.domain}";
relayHost = "smtp.sendgrid.net";
relayPort = 587;
mapFiles = {
sasl_passwd = "/var/src/secrets/postfix/sasl_passwd";
};
extraConfig = ''
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
'';
};
logrotate = {
settings = {
2023-03-26 14:13:30 +03:00
"/var/log/caddy/access-jakstys.lt.log" = {
rotate = 60;
frequency = "daily";
dateext = true;
dateyesterday = true;
compress = true;
compresscmd = "${pkgs.zstd}/bin/zstd";
compressext = ".zst";
compressoptions = "--long -19";
uncompresscmd = "${pkgs.zstd}/bin/unzstd";
2023-01-17 15:12:08 +02:00
postrotate = "${pkgs.systemd}/bin/systemctl restart caddy";
};
};
};
2023-03-01 14:39:48 +02:00
sshguard = {
enable = true;
blocktime = 900;
whitelist = [
"192.168.0.0/16"
2023-03-01 15:05:58 +02:00
tailscale_subnet.cidr
2023-03-21 15:01:06 +02:00
ips.vno1
2023-03-01 14:39:48 +02:00
];
};
2023-03-21 15:01:06 +02:00
knot = let
jakstysLTZone = pkgs.writeText "jakstys.lt.zone" ''
$ORIGIN jakstys.lt.
$TTL 86400
@ SOA ns1.jakstys.lt. motiejus.jakstys.lt. (2023032100 86400 86400 86400 86400)
@ NS ns1.jakstys.lt.
@ NS ns2.jakstys.lt.
2023-03-30 16:12:18 +03:00
@ A ${ips.hel1a}
www A ${ips.hel1a}
2023-03-21 15:01:06 +02:00
ns1 A ${ips.vno1}
ns2 A ${ips.hel1a}
beta A ${ips.hel1a}
turn A ${ips.hel1a}
vpn A ${ips.hel1a}
git A ${ips.hel1a}
auth A ${ips.hel1a}
dl A ${ips.vno1}
hel1-a A ${ips.hel1a}
vno1 A ${ips.vno1}
2023-03-21 15:24:39 +02:00
@ MX 10 aspmx.l.google.com.
@ MX 20 alt1.aspmx.l.google.com.
@ MX 20 alt2.aspmx.l.google.com.
@ MX 30 aspmx2.googlemail.com.
@ MX 30 aspmx3.googlemail.com.
2023-04-03 16:50:52 +03:00
'';
2023-03-21 15:01:06 +02:00
in {
enable = true;
extraConfig = ''
2023-04-03 16:50:52 +03:00
server:
listen: 0.0.0.0@53
listen: ::@53
version: 42
zone:
- domain: jakstys.lt
file: ${jakstysLTZone}
semantic-checks: on
2023-03-21 15:01:06 +02:00
'';
};
2023-01-03 12:31:37 +02:00
};
2023-01-04 10:06:52 +02:00
networking = {
hostName = "hel1-a";
2023-02-06 22:23:20 +02:00
domain = "jakstys.lt";
2023-03-04 06:18:44 +02:00
firewall = let
2023-04-03 16:50:52 +03:00
coturn = with config.services.coturn; [
{
from = min-port;
to = max-port;
}
];
2023-03-04 06:18:44 +02:00
in {
2023-03-01 15:05:58 +02:00
allowedTCPPorts = [
2023-03-21 15:01:06 +02:00
53
2023-04-03 16:50:52 +03:00
80
443
3478
5349
5350 # coturn
2023-03-01 15:05:58 +02:00
];
2023-03-26 15:40:36 +03:00
allowedUDPPorts = [
2023-04-03 16:50:52 +03:00
53
443
2023-03-26 15:40:36 +03:00
41641
];
2023-03-04 06:18:44 +02:00
allowedUDPPortRanges = coturn;
2023-03-01 14:17:27 +02:00
logRefusedConnections = false;
2023-03-29 22:21:16 +03:00
checkReversePath = "loose"; # for tailscale
2023-01-04 10:06:52 +02:00
};
2023-01-03 12:31:37 +02:00
};
2023-01-04 10:06:52 +02:00
system = {
2023-03-30 16:12:18 +03:00
# TODO: run the upgrades after the backup service is complete
2023-01-04 10:06:52 +02:00
autoUpgrade.enable = true;
autoUpgrade = {
allowReboot = true;
2023-03-30 16:12:18 +03:00
dates = "01:00";
2023-01-04 10:06:52 +02:00
rebootWindow = {
2023-03-30 16:12:18 +03:00
lower = "01:00";
upper = "03:00";
2023-01-04 10:06:52 +02:00
};
2023-01-03 12:31:37 +02:00
};
};
nix = {
gc = {
2023-02-19 19:23:55 +02:00
automatic = true;
2023-02-19 22:21:14 +02:00
dates = "daily";
options = "--delete-older-than 14d";
};
extraOptions = "experimental-features = nix-command flakes";
2023-02-19 19:23:55 +02:00
};
2023-03-21 13:43:34 +02:00
systemd.tmpfiles.rules = [
"d /run/matrix-synapse 0700 matrix-synapse matrix-synapse -"
];
2023-04-03 16:50:52 +03:00
systemd.services =
{
"make-snapshot-dirs" = let
vals = builtins.attrValues backup_paths;
mountpoints = builtins.catAttrs "mountpoint" vals;
unique_mountpoints = lib.unique mountpoints;
in {
description = "prepare snapshot directories for backups";
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
ExecStart = builtins.map (d: "${pkgs.coreutils}/bin/mkdir -p ${d}/.snapshot-latest") unique_mountpoints;
RemainAfterExit = true;
};
2023-02-25 15:47:49 +02:00
};
2023-04-03 16:50:52 +03:00
coturn = {
preStart = ''
ln -sf ''${CREDENTIALS_DIRECTORY}/tls-key.pem /run/coturn/tls-key.pem
ln -sf ''${CREDENTIALS_DIRECTORY}/tls-cert.pem /run/coturn/tls-cert.pem
'';
unitConfig.ConditionPathExists = [
"${turn_cert_dir}/turn.jakstys.lt.key"
"${turn_cert_dir}/turn.jakstys.lt.crt"
];
serviceConfig.LoadCredential = [
"static-auth-secret:/var/src/secrets/turn/static-auth-secret"
"tls-key.pem:${turn_cert_dir}/turn.jakstys.lt.key"
"tls-cert.pem:${turn_cert_dir}/turn.jakstys.lt.crt"
];
};
2023-04-03 16:50:52 +03:00
matrix-synapse = let
# TODO https://github.com/NixOS/nixpkgs/pull/222336 replace with `preStart`
secretsScript = pkgs.writeShellScript "write-secrets" ''
2023-03-21 15:01:06 +02:00
set -euo pipefail
2023-03-21 13:43:34 +02:00
umask 077
ln -sf ''${CREDENTIALS_DIRECTORY}/jakstys.lt.signing.key /run/matrix-synapse/jakstys.lt.signing.key
cat > /run/matrix-synapse/secrets.yaml <<EOF
registration_shared_secret: "$(cat ''${CREDENTIALS_DIRECTORY}/registration_shared_secret)"
macaroon_secret_key: "$(cat ''${CREDENTIALS_DIRECTORY}/macaroon_secret_key)"
2023-03-21 13:55:52 +02:00
turn_shared_secret: "$(cat ''${CREDENTIALS_DIRECTORY}/turn_shared_secret)"
2023-03-21 13:43:34 +02:00
EOF
2023-04-03 16:50:52 +03:00
'';
in {
serviceConfig.ExecStartPre = ["" secretsScript];
serviceConfig.LoadCredential = [
"jakstys.lt.signing.key:/var/src/secrets/synapse/jakstys.lt.signing.key"
"registration_shared_secret:/var/src/secrets/synapse/registration_shared_secret"
"macaroon_secret_key:/var/src/secrets/synapse/macaroon_secret_key"
"turn_shared_secret:/var/src/secrets/turn/static-auth-secret"
];
2023-03-01 14:55:57 +02:00
};
2023-04-03 16:50:52 +03:00
cert-watcher = {
description = "Restart coturn when tls key/cert changes";
wantedBy = ["multi-user.target"];
unitConfig = {
StartLimitIntervalSec = 10;
StartLimitBurst = 5;
};
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.systemd}/bin/systemctl restart coturn.service";
};
2023-03-01 14:55:57 +02:00
};
2023-04-03 16:50:52 +03:00
# https://northernlightlabs.se/2014-07-05/systemd-status-mail-on-unit-failure.html
"unit-status-mail@" = let
script = pkgs.writeShellScript "unit-status-mail" ''
2023-03-21 15:01:06 +02:00
set -e
2023-02-25 15:47:49 +02:00
MAILTO="motiejus+alerts@jakstys.lt"
UNIT=$1
EXTRA=""
for e in "''${@:2}"; do
EXTRA+="$e"$'\n'
done
2023-02-25 17:23:21 +02:00
UNITSTATUS=$(${pkgs.systemd}/bin/systemctl status "$UNIT")
2023-02-25 15:47:49 +02:00
${pkgs.postfix}/bin/sendmail $MAILTO <<EOF
Subject:Status mail for unit: $UNIT
Status report for unit: $UNIT
$EXTRA
$UNITSTATUS
EOF
echo -e "Status mail sent to: $MAILTO for unit: $UNIT"
2023-04-03 16:50:52 +03:00
'';
in {
description = "Send an email on unit failure";
serviceConfig = {
Type = "simple";
ExecStart = ''${script} "%I" "Hostname: %H" "Machine ID: %m" "Boot ID: %b" '';
};
2023-02-25 15:47:49 +02:00
};
2023-03-01 15:20:28 +02:00
2023-04-03 16:50:52 +03:00
zfs-scrub.unitConfig.OnFailure = "unit-status-mail@zfs-scrub.service";
nixos-upgrade.unitConfig.OnFailure = "unit-status-mail@nixos-upgrade.service";
}
// lib.mapAttrs' (name: value: {
2023-02-25 15:47:49 +02:00
name = "borgbackup-job-${name}";
value = {
2023-02-25 17:20:01 +02:00
unitConfig.OnFailure = "unit-status-mail@borgbackup-job-${name}.service";
2023-02-25 15:47:49 +02:00
};
2023-04-03 16:50:52 +03:00
})
backup_paths;
2023-03-01 14:55:57 +02:00
systemd.paths = {
cert-watcher = {
wantedBy = ["multi-user.target"];
pathConfig = {
PathChanged = "${turn_cert_dir}/turn.jakstys.lt.crt";
Unit = "cert-watcher.service";
};
};
};
2023-01-04 10:06:52 +02:00
# Do not change
system.stateVersion = "22.11";
2023-01-03 12:31:37 +02:00
}