dotfiles/modules/nixos/services/forgejo/default.nix

204 lines
5.1 KiB
Nix
Raw Normal View History

2025-10-01 19:51:55 -05:00
{
2025-11-09 02:42:22 -06:00
config,
2025-10-01 19:51:55 -05:00
flake,
...
}:
let
2025-11-04 03:30:52 -06:00
inherit (flake.config.people) user0;
2025-11-09 02:42:22 -06:00
inherit (flake.config.services) instances;
serviceCfg = flake.config.services.instances.forgejo;
smtpCfg = flake.config.services.instances.smtp;
hostCfg = flake.config.services.instances.web;
host = serviceCfg.domains.url0;
dns0 = instances.web.dns.provider0;
dns0Path = "dns/${dns0}";
2025-10-01 19:51:55 -05:00
in
{
2025-11-09 02:42:22 -06:00
users.users.caddy.extraGroups = [ "acme" ];
2025-11-04 03:30:52 -06:00
2025-11-09 02:42:22 -06:00
security.acme.certs."${host}" = {
dnsProvider = dns0;
environmentFile = config.sops.secrets.${dns0Path}.path;
group = "caddy";
};
2025-11-04 03:30:52 -06:00
2025-11-09 02:42:22 -06:00
microvm.vms.forgejo = {
autostart = true;
restartIfChanged = true;
config = {
system.stateVersion = "24.05";
time.timeZone = "America/Winnipeg";
users.users.root.openssh.authorizedKeys.keys = flake.config.people.users.${user0}.sshKeys;
services = {
forgejo = {
enable = true;
database.type = "postgres";
lfs.enable = true;
secrets = {
mailer.PASSWD = "/run/secrets/${serviceCfg.name}-smtp";
database.PASSWD = "/run/secrets/${serviceCfg.name}-database";
};
dump = {
interval = "5:00";
type = "zip";
file = "forgejo-backup";
enable = true;
};
settings = {
server = {
DOMAIN = host;
ROOT_URL = "https://${host}/";
HTTP_PORT = serviceCfg.ports.port0;
2025-11-04 03:30:52 -06:00
};
2025-11-09 02:42:22 -06:00
# If you need to start from scratch, don't forget to turn this off again
service.DISABLE_REGISTRATION = false;
actions = {
ENABLED = true;
DEFAULT_ACTIONS_URL = "github";
};
mirror = {
ENABLED = true;
2025-11-04 03:30:52 -06:00
};
2025-11-09 02:42:22 -06:00
mailer = {
ENABLED = true;
SMTP_ADDR = smtpCfg.hostname;
FROM = smtpCfg.email.address1;
USER = smtpCfg.email.address1;
PROTOCOL = "${smtpCfg.name}+${smtpCfg.records.record1}";
SMTP_PORT = smtpCfg.ports.port1;
SEND_AS_PLAIN_TEXT = true;
USE_CLIENT_CERT = false;
};
};
};
2025-11-04 03:30:52 -06:00
2025-11-09 02:42:22 -06:00
openssh = {
enable = true;
settings = {
PasswordAuthentication = false;
PermitRootLogin = "prohibit-password";
};
};
};
2025-11-04 03:30:52 -06:00
2025-11-09 02:42:22 -06:00
networking.firewall.allowedTCPPorts = [
22 # SSH
25 # SMTP
139 # SMTP
587 # SMTP
2525 # SMTP
serviceCfg.ports.port0
];
fileSystems."/tmp" = {
device = "tmpfs";
fsType = "tmpfs";
options = [
"size=4G"
"mode=1777"
];
};
systemd = {
network = {
enable = true;
networks."20-lan" = {
matchConfig.Name = "enp0s5";
addresses = [ { Address = "${serviceCfg.interface.ip}/24"; } ];
routes = [
{
Destination = "${hostCfg.localhost.address1}/0";
Gateway = serviceCfg.interface.gate;
}
];
dns = [
"1.1.1.1"
"8.8.8.8"
];
2025-11-04 03:30:52 -06:00
};
2025-11-09 02:42:22 -06:00
};
tmpfiles.rules = [
"d ${serviceCfg.varPaths.path0} 0755 ${serviceCfg.name} ${serviceCfg.name} -"
];
};
systemd.services.systemd-networkd.wantedBy = [ "multi-user.target" ];
microvm = {
vcpu = 2;
mem = 3072;
hypervisor = "qemu";
interfaces = [
{
type = "tap";
id = serviceCfg.interface.id;
mac = serviceCfg.interface.mac;
}
{
type = "user";
id = serviceCfg.interface.idUser;
mac = serviceCfg.interface.macUser;
}
];
forwardPorts = [
{
from = "host";
host.port = serviceCfg.interface.ssh;
guest.port = 22;
}
];
shares = [
{
mountPoint = "/nix/.ro-store";
proto = "virtiofs";
source = "/nix/store";
tag = "read_only_nix_store";
}
{
mountPoint = "/var/lib/${serviceCfg.name}";
proto = "virtiofs";
source = serviceCfg.mntPaths.path0;
tag = "${serviceCfg.name}_data";
}
{
mountPoint = "/run/secrets";
proto = "virtiofs";
source = "/run/secrets";
tag = "host_secrets";
}
];
2025-10-01 19:51:55 -05:00
};
};
};
2025-11-04 03:30:52 -06:00
2025-11-06 04:08:29 -06:00
systemd.tmpfiles.rules = [
2025-11-09 02:42:22 -06:00
"d ${serviceCfg.mntPaths.path0} 0755 root root -"
2025-11-06 04:08:29 -06:00
];
2025-11-04 03:30:52 -06:00
services.caddy.virtualHosts."${host}" = {
extraConfig = ''
2025-11-09 02:42:22 -06:00
reverse_proxy ${serviceCfg.interface.ip}:${toString serviceCfg.ports.port0} {
header_up X-Real-IP {remote_host}
}
2025-11-04 03:30:52 -06:00
2025-11-09 02:42:22 -06:00
tls ${serviceCfg.ssl.cert} ${serviceCfg.ssl.key}
encode zstd gzip
2025-11-04 03:30:52 -06:00
'';
};
2025-11-09 02:42:22 -06:00
sops.secrets = {
"${serviceCfg.name}-smtp" = {
owner = "root";
mode = "0600";
2025-10-01 19:51:55 -05:00
};
2025-11-09 02:42:22 -06:00
"${serviceCfg.name}-database" = {
owner = "root";
mode = "0600";
};
};
2025-10-01 19:51:55 -05:00
}