nixos/modules/services/reverseproxy.nix

290 lines
9.0 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
lib,
config,
...
}:
let
cfg = config.crans.reverseProxy;
mainTld = "org";
otherTld = [
"fr"
"eu"
];
inherit (lib)
literalExpression
mkEnableOption
mkIf
mkOption
types
;
mkHostName = als: tld: if als == "_crans-org" then "crans.${tld}" else "${als}.crans.${tld}";
in
{
options.crans.reverseProxy = {
enable = mkEnableOption "Configuration du reverseproxy.";
virtualHosts = mkOption {
type = types.attrsOf (
types.submodule {
options = {
serverAliases = mkOption {
type = types.listOf types.str;
default = [ ];
example = [
"everything"
"voyager"
];
description = ''
Déclaration des alias.
'';
};
proxyPass = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Indique la destination à proxy.
'';
example = "172.16.10.128:8000";
};
serveLocalFiles = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Chemin vers un dossier à exposer statiquement.
'';
example = "/var/local/adopter-un-manchot";
};
globalRedirect = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Si définie, toutes les requêtes sont redirigées (via 301) sur cet hôte.
'';
example = "perso.crans.org/club";
};
anubisConfig = mkOption {
type = types.nullOr (types.listOf types.attrs);
default = null;
description = ''
Configuration pour anubis
'';
};
httpOnly = mkOption {
type = types.bool;
default = false;
description = ''
Interdit les connexions en https
'';
example = "true";
};
proxyWebsockets = mkOption {
type = types.bool;
default = false;
description = ''
Activer les websockets
'';
example = "true";
};
};
}
);
default = { };
example = literalExpression ''
{
"framadate" = {
host = "176.16.10.128:8000";
serverAliases = [
"everything"
"voyager"
]
};
};
'';
description = "Déclaration des machines.";
};
};
config = mkIf cfg.enable {
systemd.services = lib.mapAttrs (vhostName: vhostConfig: {
wantedBy = [ "multi-user.target" ];
}) cfg.virtualHosts;
# On fait un certificat commun pour tous les hosts
security.acme.certs."all.crans.all" = {
domain = "*.crans.${mainTld}";
dnsProvider = "rfc2136";
# Contient le serveur à contacter avec le protocole
# et le mot de passe
environmentFile = config.age.secrets.acme-env.path;
extraDomainNames =
lib.concatMap (tld: [
"*.crans.${tld}"
"crans.${tld}"
]) otherTld
++ [
"crans.${mainTld}"
];
group = config.services.nginx.group;
};
# Création des dossiers de logs
system.activationScripts = {
nginx-logs = {
text =
lib.concatMapStrings (f: "mkdir -p /var/log/nginx/${f}/\n") (
[ "anubis" ] ++ (lib.map (hn: "host/${hn}") (builtins.attrNames cfg.virtualHosts))
)
+ "chown -R ${config.services.nginx.user}:${config.services.nginx.group} /var/log/nginx/\n";
};
};
# On active Anubis
crans.anubis = {
enable = true;
instances = lib.mapAttrs (vhostName: vhostConfig: {
hostname = mkHostName vhostName mainTld;
bind = "/run/anubis/anubis-${vhostName}/socket.sock";
bots = vhostConfig.anubisConfig;
}) (lib.filterAttrs (_: c: c.anubisConfig != null) cfg.virtualHosts);
};
services = {
logrotate.settings =
let
# https://github.com/NixOS/nixpkgs/blob/nixos-26.05/nixos/modules/services/web-servers/nginx/default.nix
base = {
su = "${config.services.nginx.user} ${config.services.nginx.group}";
postrotate = "[ ! -f /var/run/nginx/nginx.pid ] || kill -USR1 `cat /var/run/nginx/nginx.pid`";
frequency = "daily";
ifempty = true;
compress = true;
delaycompress = true;
dateyesterday = true;
};
in
{
nginx-anubis = base // {
files = [ "/var/log/nginx/anubis/*.log" ];
rotate = 7; # une semaine de log pour débugguer
};
nginx-hosts = base // {
files = [ "/var/log/nginx/host/*/*.log" ];
rotate = 173; # 6 mois de logs
dateext = true;
dateformat = "%Y-%m-%d";
};
};
nginx =
let
configVhost =
vhostName: vhostConfig:
let
hostname = mkHostName vhostName mainTld;
sslConf = {
forceSSL = !vhostConfig.httpOnly;
rejectSSL = vhostConfig.httpOnly;
useACMEHost = mkIf (!vhostConfig.httpOnly) "all.crans.all";
acmeRoot = null;
};
logHostConf = ''
access_log /var/log/nginx/host/${vhostName}/access.log;
error_log /var/log/nginx/host/${vhostName}/error.log;
'';
# Les alias : vhostName × otherTld U serverAliases × allTld
aliases =
lib.foldr
(
tld: acc:
acc
++ (lib.foldr (alias: acc: acc ++ [ (mkHostName alias tld) ]) [
(mkHostName vhostName tld)
] vhostConfig.serverAliases)
)
(lib.foldr (alias: acc: acc ++ [ (mkHostName alias mainTld) ]) [ ] vhostConfig.serverAliases)
otherTld;
in
{
# Configuration du service à proxy.
"${vhostName}" = sslConf // {
serverName = hostname;
# Site statiques et alias
root = vhostConfig.serveLocalFiles;
globalRedirect = vhostConfig.globalRedirect;
locations = {
# Entrée site, via proxy
"/" = mkIf (vhostConfig.proxyPass != null) {
proxyPass = "http://${vhostConfig.proxyPass}";
proxyWebsockets = vhostConfig.proxyWebsockets;
extraConfig = lib.optionalString (vhostConfig.anubisConfig != null) ''
auth_request /.within.website/x/cmd/anubis/api/check;
error_page 401 = @redirectToAnubis;
'';
};
# Anubis
"/.within.website/" = mkIf (vhostConfig.anubisConfig != null) {
# https://anubis.techaro.lol/docs/admin/configuration/subrequest-auth/
proxyPass = "http://unix:/run/anubis/anubis-${vhostName}/socket.sock";
extraConfig = ''
proxy_pass_request_body off;
proxy_set_header content-length "";
add_header 'X-Robots-Tag' 'noindex,nofollow' always;
auth_request off;
access_log /var/log/nginx/anubis/access.log;
error_log /var/log/nginx/anubis/error.log;
'';
};
"@redirectToAnubis" = mkIf (vhostConfig.anubisConfig != null) {
extraConfig = ''
return 307 /.within.website/?redir=$scheme://$host$request_uri;
auth_request off;
access_log /var/log/nginx/anubis/access.log;
error_log /var/log/nginx/anubis/error.log;
'';
};
};
extraConfig = ''
set_real_ip_from 172.16.0.0/16;
set_real_ip_from fd00::/56;
real_ip_header X-Real-Ip;
''
+ logHostConf;
};
}
# Les alias
// lib.mergeAttrsList (
lib.map (alias: {
"${vhostName}-alias-${alias}" = sslConf // {
serverName = alias;
globalRedirect = lib.defaultTo hostname vhostConfig.globalRedirect;
extraConfig = logHostConf;
};
}) aliases
);
in
{
enable = true;
virtualHosts = lib.concatMapAttrs configVhost cfg.virtualHosts;
};
};
};
}