2021-01-31 17:46:46 +01:00
|
|
|
# A low-ressource, full-featured git forge.
|
|
|
|
{ config, lib, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.my.services.gitea;
|
|
|
|
in
|
|
|
|
{
|
2021-02-03 12:09:54 +01:00
|
|
|
options.my.services.gitea = with lib; {
|
|
|
|
enable = mkEnableOption "Gitea";
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
2021-02-18 01:18:01 +01:00
|
|
|
default = 3042;
|
2021-02-03 12:09:54 +01:00
|
|
|
example = 8080;
|
|
|
|
description = "Internal port";
|
|
|
|
};
|
2022-04-08 20:49:48 +02:00
|
|
|
mail = {
|
|
|
|
enable = mkEnableOption {
|
|
|
|
description = "mailer configuration";
|
|
|
|
};
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
2024-03-05 15:16:22 +01:00
|
|
|
example = "smtp.example.com";
|
2022-04-08 20:49:48 +02:00
|
|
|
description = "Host for the mail account";
|
|
|
|
};
|
2024-03-05 15:16:22 +01:00
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 465;
|
|
|
|
example = 587;
|
|
|
|
description = "Port for the mail account";
|
|
|
|
};
|
2022-04-08 20:49:48 +02:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "gitea@example.com";
|
|
|
|
description = "User for the mail account";
|
|
|
|
};
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "/run/secrets/gitea-mail-password.txt";
|
|
|
|
description = "Password for the mail account";
|
|
|
|
};
|
2024-03-05 15:16:22 +01:00
|
|
|
protocol = mkOption {
|
2022-04-08 20:49:48 +02:00
|
|
|
type = types.str;
|
2024-03-05 15:16:22 +01:00
|
|
|
default = "smtps";
|
2022-04-08 20:49:48 +02:00
|
|
|
example = "smtp";
|
2024-03-05 15:16:22 +01:00
|
|
|
description = "Protocol for connection";
|
2022-04-08 20:49:48 +02:00
|
|
|
};
|
|
|
|
};
|
2021-01-31 17:46:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2021-08-24 23:05:10 +02:00
|
|
|
services.gitea =
|
|
|
|
let
|
2022-04-08 20:49:48 +02:00
|
|
|
inherit (config.networking) domain;
|
2023-03-16 21:31:19 +01:00
|
|
|
giteaDomain = "git.${domain}";
|
2021-08-24 23:05:10 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
enable = true;
|
2021-02-03 12:09:54 +01:00
|
|
|
|
2021-08-24 23:05:10 +02:00
|
|
|
appName = "Ambroisie's forge";
|
2021-02-03 12:09:54 +01:00
|
|
|
|
2021-08-24 23:05:10 +02:00
|
|
|
user = "git";
|
2024-03-05 13:44:08 +01:00
|
|
|
group = "git";
|
|
|
|
|
2021-08-24 23:05:10 +02:00
|
|
|
lfs.enable = true;
|
2021-02-03 12:09:54 +01:00
|
|
|
|
2021-08-24 23:05:10 +02:00
|
|
|
useWizard = false;
|
2021-02-03 12:09:54 +01:00
|
|
|
|
2021-08-24 23:05:10 +02:00
|
|
|
database = {
|
|
|
|
type = "postgres"; # Automatic setup
|
|
|
|
user = "git"; # User needs to be the same as gitea user
|
2023-11-21 00:15:02 +01:00
|
|
|
name = "git"; # Name must be the same as user for `ensureDBOwnership`
|
2021-08-24 23:05:10 +02:00
|
|
|
};
|
2021-02-07 21:43:15 +01:00
|
|
|
|
2021-08-24 23:05:10 +02:00
|
|
|
# NixOS module uses `gitea dump` to backup repositories and the database,
|
|
|
|
# but it produces a single .zip file that's not very backup friendly.
|
|
|
|
# I configure my backup system manually below.
|
|
|
|
dump.enable = false;
|
2022-04-08 20:49:48 +02:00
|
|
|
|
|
|
|
mailerPasswordFile = lib.mkIf cfg.mail.enable cfg.mail.passwordFile;
|
|
|
|
|
|
|
|
settings = {
|
2023-04-23 17:23:36 +02:00
|
|
|
server = {
|
|
|
|
HTTP_PORT = cfg.port;
|
|
|
|
DOMAIN = giteaDomain;
|
|
|
|
ROOT_URL = "https://${giteaDomain}";
|
|
|
|
};
|
|
|
|
|
2022-04-08 20:49:48 +02:00
|
|
|
mailer = lib.mkIf cfg.mail.enable {
|
|
|
|
ENABLED = true;
|
2024-03-05 15:16:22 +01:00
|
|
|
SMTP_ADDR = cfg.mail.host;
|
|
|
|
SMTP_PORT = cfg.mail.port;
|
2024-03-05 15:20:57 +01:00
|
|
|
FROM = "Gitea <${cfg.mail.user}>";
|
2022-04-08 20:49:48 +02:00
|
|
|
USER = cfg.mail.user;
|
2024-03-05 15:16:22 +01:00
|
|
|
PROTOCOL = cfg.mail.protocol;
|
2022-04-08 20:49:48 +02:00
|
|
|
};
|
2022-08-31 17:19:58 +02:00
|
|
|
|
|
|
|
service = {
|
|
|
|
DISABLE_REGISTRATION = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
session = {
|
|
|
|
# only send cookies via HTTPS
|
|
|
|
COOKIE_SECURE = true;
|
|
|
|
};
|
2022-04-08 20:49:48 +02:00
|
|
|
};
|
2021-08-24 23:05:10 +02:00
|
|
|
};
|
2021-01-31 17:46:46 +01:00
|
|
|
|
2021-02-03 12:09:54 +01:00
|
|
|
users.users.git = {
|
|
|
|
description = "Gitea Service";
|
|
|
|
home = config.services.gitea.stateDir;
|
|
|
|
useDefaultShell = true;
|
|
|
|
group = "git";
|
|
|
|
isSystemUser = true;
|
2021-01-31 17:46:46 +01:00
|
|
|
};
|
2021-02-03 12:09:54 +01:00
|
|
|
users.groups.git = { };
|
2021-01-31 17:46:46 +01:00
|
|
|
|
2023-12-25 19:25:08 +01:00
|
|
|
my.services.nginx.virtualHosts = {
|
2023-03-16 21:31:19 +01:00
|
|
|
# Proxy to Gitea
|
2023-12-25 19:25:08 +01:00
|
|
|
git = {
|
2021-08-24 23:05:10 +02:00
|
|
|
inherit (cfg) port;
|
2023-12-25 19:25:08 +01:00
|
|
|
};
|
2023-03-16 21:31:19 +01:00
|
|
|
# Redirect `gitea.` to actual forge subdomain
|
2023-12-25 19:25:08 +01:00
|
|
|
gitea = {
|
2023-04-23 17:23:36 +02:00
|
|
|
redirect = config.services.gitea.settings.server.ROOT_URL;
|
2023-12-25 19:25:08 +01:00
|
|
|
};
|
|
|
|
};
|
2021-02-07 21:43:15 +01:00
|
|
|
|
|
|
|
my.services.backup = {
|
|
|
|
paths = [
|
|
|
|
config.services.gitea.lfs.contentDir
|
|
|
|
config.services.gitea.repositoryRoot
|
|
|
|
];
|
|
|
|
};
|
2023-02-19 18:36:41 +01:00
|
|
|
|
2021-11-03 14:43:28 +01:00
|
|
|
my.system.persist.directories = [
|
|
|
|
config.services.gitea.lfs.contentDir
|
|
|
|
config.services.gitea.repositoryRoot
|
|
|
|
];
|
|
|
|
|
2023-02-19 18:36:41 +01:00
|
|
|
services.fail2ban.jails = {
|
|
|
|
gitea = ''
|
|
|
|
enabled = true
|
|
|
|
filter = gitea
|
|
|
|
action = iptables-allports
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc = {
|
|
|
|
"fail2ban/filter.d/gitea.conf".text = ''
|
|
|
|
[Definition]
|
|
|
|
failregex = ^.*(Failed authentication attempt|invalid credentials|Attempted access of unknown user).* from <HOST>$
|
|
|
|
journalmatch = _SYSTEMD_UNIT=gitea.service
|
|
|
|
'';
|
|
|
|
};
|
2021-01-31 17:46:46 +01:00
|
|
|
};
|
|
|
|
}
|