tadpole: Add webfinger for gitea

This commit is contained in:
Alexander Heldt
2024-08-30 20:50:03 +02:00
parent 0d5b7fe3f5
commit c4bab62de4
2 changed files with 82 additions and 11 deletions

View File

@@ -15,7 +15,10 @@ in
gitea = { gitea = {
enable = true; enable = true;
domain = "git.ppp.pm"; baseDomain = "ppp.pm";
webfingerEnable = true;
webfingerAccounts = [ "p@ppp.pm" ];
}; };
pppdotpm-site.enable = true; pppdotpm-site.enable = true;

View File

@@ -1,7 +1,7 @@
{ lib, config, ... }: { pkgs, lib, config, ... }:
let let
enable = config.mod.gitea.enable; conf = config.mod.gitea;
domain = config.mod.gitea.domain; gitDomain = "git.${conf.baseDomain}";
nginxEnable = config.mod.nginx.enable; nginxEnable = config.mod.nginx.enable;
in in
@@ -10,15 +10,48 @@ in
mod.gitea = { mod.gitea = {
enable = lib.mkEnableOption "Enable gitea"; enable = lib.mkEnableOption "Enable gitea";
domain = lib.mkOption { baseDomain = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = ""; default = "";
description = "The domain that nginx will use as a virtual host"; description = ''
The base domain that will be used to
- create https://git.<base domain> which will host the frontend of gitea
- host the webfinger
Note: A cert is required for this domain and "git.<base domain>".
'';
};
webfingerEnable = lib.mkEnableOption "Enable webfinger pointing to gitea";
webfingerAccounts = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "The accounts that should be listed";
}; };
}; };
}; };
config = lib.mkIf (enable && nginxEnable) { config = lib.mkIf (conf.enable && nginxEnable) {
assertions = [
{
assertion = conf.baseDomain != "";
message = "Option 'mod.gitea.baseDomain' cannot be empty";
}
{
assertion = builtins.hasAttr gitDomain config.security.acme.certs;
message = "There is no cert configured for ${gitDomain} used by gitea";
}
{
assertion = conf.webfingerEnable && builtins.hasAttr conf.baseDomain config.security.acme.certs;
message = "There is no cert configured for ${conf.baseDomain} used by webfinger";
}
{
assertion = conf.webfingerEnable && conf.webfingerAccounts != [];
message = "Option 'mod.gitea.webfingerAccounts' cannot be empty";
}
];
services.gitea = { services.gitea = {
enable = true; enable = true;
@@ -28,8 +61,8 @@ in
}; };
server = { server = {
DOMAIN = domain; DOMAIN = gitDomain;
ROOT_URL = "https://${domain}"; ROOT_URL = "https://${gitDomain}";
SSH_PORT = 1122; # See `ssh` module SSH_PORT = 1122; # See `ssh` module
}; };
@@ -46,9 +79,44 @@ in
}; };
services.nginx = { services.nginx = {
virtualHosts."${domain}" = { virtualHosts."${conf.baseDomain}" =
let
mkWebfinger = account:
pkgs.writeTextDir (lib.escapeURL "acct:${account}") (lib.generators.toJSON {} {
subject = "acct:${account}";
links = [{
rel = "http://openid.net/specs/connect/1.0/issuer";
href = "https://${gitDomain}";
}];
});
webfingerRoot = pkgs.symlinkJoin {
name = "${gitDomain}-webfinger";
paths = builtins.map mkWebfinger conf.webfingerAccounts;
};
in
lib.mkIf conf.webfingerEnable {
forceSSL = true;
useACMEHost = conf.baseDomain;
locations."/.well-known/webfinger" = {
root = webfingerRoot;
extraConfig = ''
add_header Access-Control-Allow-Origin "*";
default_type "application/jrd+json";
types { application/jrd+json json; }
if ($arg_resource) {
rewrite ^(.*)$ /$arg_resource break;
}
return 400;
'';
};
};
virtualHosts."${gitDomain}" = {
forceSSL = true; forceSSL = true;
useACMEHost = domain; useACMEHost = gitDomain;
locations."/" = { locations."/" = {
proxyPass = "http://0.0.0:3000"; proxyPass = "http://0.0.0:3000";