fix(nginx): add ACME webroot + fix multi-line CSP headers

- Set security.acme.certs.*.webroot for Let's Encrypt challenges
- Consolidate multi-line Content-Security-Policy to single line
- Fixes build error: exactly one of dnsProvider/webroot/listenHTTP/s3Bucket is required

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ashisgreat22 2026-03-20 22:16:51 +00:00
parent e36a67b7a0
commit 3598d5f2bf
2 changed files with 266 additions and 309 deletions

View file

@ -1,141 +1,126 @@
# Forgejo Module # Forgejo Module
# Provides: Self-hosted Git service (Fork of Gitea) # Provides: Self-hosted Git service (Fork of Gitea)
# #
# Usage: # Usage:
# myModules.forgejo = { # myModules.forgejo = {
# enable = true; # enable = true;
# domain = "git.example.com"; # domain = "git.example.com";
# }; # };
{ {
config, config,
lib, lib,
pkgs, pkgs,
... ...
}: }:
let let
cfg = config.myModules.forgejo; cfg = config.myModules.forgejo;
in in
{ {
options.myModules.forgejo = { options.myModules.forgejo = {
enable = lib.mkEnableOption "Forgejo Git service"; enable = lib.mkEnableOption "Forgejo Git service";
port = lib.mkOption { port = lib.mkOption {
type = lib.types.port; type = lib.types.port;
default = 3002; default = 3002;
description = "Internal port to run Forgejo on"; description = "Internal port to run Forgejo on";
}; };
domain = lib.mkOption { domain = lib.mkOption {
type = lib.types.str;
example = "git.example.com";
description = "Public domain name for Forgejo";
};
disableRegistration = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Disable public user registration";
};
runner = {
enable = lib.mkEnableOption "Forgejo Actions Runner";
name = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = config.networking.hostName; example = "git.example.com";
description = "Name of the runner"; description = "Public domain name for Forgejo";
}; };
tokenFile = lib.mkOption {
type = lib.types.path;
description = "Path to the token file (containing TOKEN=...)";
};
labels = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [
"native:host"
"ubuntu-latest:docker://node:20-bullseye"
"debian-latest:docker://node:20-bullseye"
];
description = "Labels for the runner";
};
};
};
config = lib.mkIf cfg.enable { disableRegistration = lib.mkOption {
services.forgejo = { type = lib.types.bool;
enable = true; default = true;
database.type = "postgres"; description = "Disable public user registration";
customDir = toString ../custom; };
settings = { runner = {
server = { enable = lib.mkEnableOption "Forgejo Actions Runner";
DOMAIN = cfg.domain; name = lib.mkOption {
ROOT_URL = "https://${cfg.domain}/"; type = lib.types.str;
HTTP_ADDR = "127.0.0.1"; default = config.networking.hostName;
HTTP_PORT = cfg.port; description = "Name of the runner";
SSH_PORT = 2222;
START_SSH_SERVER = true;
SSH_LISTEN_ADDR = "0.0.0.0";
# SSH Hardening
SSH_SERVER_KEY_EXCHANGES = "sntrup761x25519-sha512,curve25519-sha256,curve25519-sha256@libssh.org";
SSH_SERVER_CIPHERS = "chacha20-poly1305@openssh.com,aes256-gcm@openssh.com";
SSH_SERVER_MACS = "hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com";
}; };
service = { tokenFile = lib.mkOption {
DISABLE_REGISTRATION = cfg.disableRegistration; type = lib.types.path;
description = "Path to the token file (containing TOKEN=...)";
}; };
session = { labels = lib.mkOption {
COOKIE_SECURE = true; type = lib.types.listOf lib.types.str;
}; default = [
security = { "native:host"
PASSWORD_COMPLEXITY = "lower,upper,digit,spec"; "ubuntu-latest:docker://node:20-bullseye"
MIN_PASSWORD_LENGTH = 12; "debian-latest:docker://node:20-bullseye"
}; ];
"ui.meta" = { description = "Labels for the runner";
AUTHOR = "Penal Colony";
DESCRIPTION = "The apparatus inscribes your code. Every commit is judged.";
};
"ui" = {
DEFAULT_THEME = "forgejo-auto";
APP_NAME = "The Harrow";
}; };
}; };
}; };
# Nginx Reverse Proxy config = lib.mkIf cfg.enable {
myModules.nginx.domains."${cfg.domain}" = { services.forgejo = {
port = cfg.port;
extraConfig = ''
client_max_body_size 512M;
'';
# Relaxed CSP for Forgejo — needs inline styles for code highlighting
contentSecurityPolicy = "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self' wss://${cfg.domain}; frame-ancestors 'self'";
};
# Open SSH port for Git
networking.firewall.allowedTCPPorts = [ 2222 ];
# Backups (Add Forgejo data to restic if backup module is enabled)
myModules.backup.paths = [
config.services.forgejo.stateDir
];
# Actions Runner
services.gitea-actions-runner = lib.mkIf cfg.runner.enable {
package = pkgs.forgejo-runner;
instances.default = {
enable = true; enable = true;
name = cfg.runner.name; database.type = "postgres";
url = "https://${cfg.domain}";
tokenFile = cfg.runner.tokenFile;
labels = cfg.runner.labels;
settings = { settings = {
container = { server = {
network = "bridge"; DOMAIN = cfg.domain;
ROOT_URL = "https://${cfg.domain}/";
HTTP_ADDR = "127.0.0.1";
HTTP_PORT = cfg.port;
SSH_PORT = 2222;
START_SSH_SERVER = true;
SSH_LISTEN_ADDR = "0.0.0.0";
SSH_SERVER_KEY_EXCHANGES = "sntrup761x25519-sha512,curve25519-sha256,curve25519-sha256@libssh.org";
SSH_SERVER_CIPHERS = "chacha20-poly1305@openssh.com,aes256-gcm@openssh.com";
SSH_SERVER_MACS = "hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com";
};
service = {
DISABLE_REGISTRATION = cfg.disableRegistration;
};
session = {
COOKIE_SECURE = true;
};
security = {
PASSWORD_COMPLEXITY = "lower,upper,digit,spec";
MIN_PASSWORD_LENGTH = 12;
};
};
};
myModules.nginx.domains."${cfg.domain}" = {
port = cfg.port;
extraConfig = ''
client_max_body_size 512M;
'';
contentSecurityPolicy = "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self' wss://${cfg.domain}; frame-ancestors 'self'";
};
networking.firewall.allowedTCPPorts = [ 2222 ];
myModules.backup.paths = [
config.services.forgejo.stateDir
];
services.gitea-actions-runner = lib.mkIf cfg.runner.enable {
package = pkgs.forgejo-runner;
instances.default = {
enable = true;
name = cfg.runner.name;
url = "https://${cfg.domain}";
tokenFile = cfg.runner.tokenFile;
labels = cfg.runner.labels;
settings = {
container = {
network = "bridge";
};
}; };
}; };
}; };
}; };
}; }
}

View file

@ -1,213 +1,185 @@
# Nginx Reverse Proxy Module # Nginx Reverse Proxy Module
# Provides: Nginx with automatic Let's Encrypt certificates, security headers, and rate limiting # Provides: Nginx with automatic Let's Encrypt certificates, security headers, and rate limiting
#
# Usage:
# myModules.nginx = {
# enable = true;
# email = "your@email.com";
# domains = {
# "search.example.com" = {
# port = 8888;
# };
# };
# };
{ {
config, config,
lib, lib,
pkgs, pkgs,
... ...
}: }:
let let
cfg = config.myModules.nginx; cfg = config.myModules.nginx;
in in
{ {
options.myModules.nginx = { options.myModules.nginx = {
enable = lib.mkEnableOption "Nginx reverse proxy with Let's Encrypt"; enable = lib.mkEnableOption "Nginx reverse proxy with Let's Encrypt";
email = lib.mkOption { email = lib.mkOption {
type = lib.types.str;
example = "admin@example.com";
description = "Email address for Let's Encrypt registration";
};
rateLimit = {
enable = lib.mkEnableOption "Nginx rate limiting";
zone = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "10m"; example = "admin@example.com";
description = "Size of the shared memory zone for rate limiting"; description = "Email address for Let's Encrypt registration";
}; };
requests = lib.mkOption { rateLimit = {
type = lib.types.int; enable = lib.mkEnableOption "Nginx rate limiting";
default = 10;
description = "Number of requests allowed per second (burst applies on top)"; zone = lib.mkOption {
type = lib.types.str;
default = "10m";
description = "Size of the shared memory zone for rate limiting";
};
requests = lib.mkOption {
type = lib.types.int;
default = 10;
description = "Number of requests allowed per second";
};
burst = lib.mkOption {
type = lib.types.int;
default = 20;
description = "Maximum burst of requests allowed beyond the rate";
};
}; };
burst = lib.mkOption { domains = lib.mkOption {
type = lib.types.int; type = lib.types.attrsOf (lib.types.submodule {
default = 20; options = {
description = "Maximum burst of requests allowed beyond the rate"; port = lib.mkOption {
}; type = lib.types.port;
}; description = "Local port to proxy to";
domains = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options = {
port = lib.mkOption {
type = lib.types.port;
description = "Local port to proxy to";
};
extraConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = "Extra Nginx config for this location";
};
contentSecurityPolicy = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self'; frame-ancestors 'self'";
description = "Content-Security-Policy header value. Set to null to omit.";
};
internalOnly = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Restrict access to Tailscale network and localhost only";
};
rateLimit = {
enable = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Enable rate limiting for this vhost. Defaults to global rateLimit.enable.";
}; };
requests = lib.mkOption { extraConfig = lib.mkOption {
type = lib.types.nullOr lib.types.int; type = lib.types.lines;
default = null; default = "";
description = "Requests per second for this vhost. Defaults to global rateLimit.requests."; description = "Extra Nginx config for this location";
}; };
burst = lib.mkOption { contentSecurityPolicy = lib.mkOption {
type = lib.types.nullOr lib.types.int; type = lib.types.nullOr lib.types.str;
default = null; default = "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self'; frame-ancestors 'self'";
description = "Burst size for this vhost. Defaults to global rateLimit.burst."; description = "Content-Security-Policy header value. Set to null to omit.";
}; };
};
extraLocations = lib.mkOption { internalOnly = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule { type = lib.types.bool;
options = { default = false;
proxyPass = lib.mkOption { description = "Restrict access to Tailscale network and localhost only";
type = lib.types.str; };
description = "Proxy target URL";
}; rateLimit = {
extraConfig = lib.mkOption { enable = lib.mkOption {
type = lib.types.lines; type = lib.types.nullOr lib.types.bool;
default = ""; default = null;
description = "Extra Nginx config for this location"; description = "Enable rate limiting for this vhost.";
};
}; };
});
default = { }; requests = lib.mkOption {
description = "Additional location blocks to add to this virtual host"; type = lib.types.nullOr lib.types.int;
default = null;
description = "Number of requests allowed per second for this vhost.";
};
burst = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Burst size for this vhost.";
};
};
extraLocations = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options = {
proxyPass = lib.mkOption {
type = lib.types.str;
description = "Proxy target URL";
};
extraConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = "Extra Nginx config for this location";
};
};
});
default = { };
description = "Additional location blocks";
};
}; };
}; });
}); default = { };
default = { }; description = "Domains to configure with their proxy targets";
description = "Domains to configure with their proxy targets"; };
};
};
config = lib.mkIf cfg.enable {
# Open HTTP/HTTPS ports
networking.firewall.allowedTCPPorts = [ 80 443 ];
# ACME (Let's Encrypt) configuration
security.acme = {
acceptTerms = true;
defaults.email = cfg.email;
certs = lib.mapAttrs' (domain: opts: {
name = domain;
value = { };
}) cfg.domains;
}; };
# Nginx configuration config = lib.mkIf cfg.enable {
services.nginx = { networking.firewall.allowedTCPPorts = [ 80 443 ];
enable = true;
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
# Rate limiting zones (one per domain for per-domain limits) security.acme = {
commonHttpConfig = lib.optionalString cfg.rateLimit.enable '' acceptTerms = true;
# Global rate limiting zone defaults.email = cfg.email;
limit_req_zone $binary_remote_addr zone=global:10m rate=${toString cfg.rateLimit.requests}r/s; certs = lib.mapAttrs' (domain: opts: {
# Limit connection flooding name = domain;
limit_conn_zone $binary_remote_addr zone=connlimit:10m; value.webroot = "/var/lib/acme/acme-challenge";
''; }) cfg.domains;
};
virtualHosts = lib.mapAttrs' (domain: opts: { services.nginx = {
name = domain; enable = true;
value = { recommendedGzipSettings = true;
enableACME = true; recommendedOptimisation = true;
forceSSL = true; recommendedProxySettings = true;
recommendedTlsSettings = true;
# Security headers applied per-vhost commonHttpConfig = lib.optionalString cfg.rateLimit.enable ''
extraConfig = '' limit_req_zone $binary_remote_addr zone=global:10m rate=${toString cfg.rateLimit.requests}r/s;
${lib.optionalString opts.internalOnly '' limit_conn_zone $binary_remote_addr zone=connlimit:10m;
# Restrict access to Tailscale network '';
allow 100.64.0.0/10;
allow 127.0.0.0/8;
deny all;
''}
# Strict Transport Security — 6 months, include subdomains, preload-ready
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always;
# Prevent MIME type sniffing virtualHosts = lib.mapAttrs' (domain: opts: {
add_header X-Content-Type-Options "nosniff" always; name = domain;
value = {
useACMEHost = domain;
forceSSL = true;
# Restrict browser features extraConfig = ''
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always; ${lib.optionalString opts.internalOnly ''
allow 100.64.0.0/10;
allow 127.0.0.0/8;
deny all;
''}
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Cross-Origin-Resource-Policy "same-origin" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
'' + lib.optionalString (opts.contentSecurityPolicy != null) ''
add_header Content-Security-Policy "${opts.contentSecurityPolicy}" always;
'';
# Cross-origin isolation locations = {
add_header Cross-Origin-Resource-Policy "same-origin" always; "/" = {
add_header Cross-Origin-Opener-Policy "same-origin" always; proxyPass = "http://127.0.0.1:${toString opts.port}";
extraConfig = opts.extraConfig + lib.optionalString (if opts.rateLimit.enable != null then
opts.rateLimit.enable else cfg.rateLimit.enable) ''
limit_req zone=global burst=${toString (if opts.rateLimit.burst != null then opts.rateLimit.burst
else cfg.rateLimit.burst)} nodelay;
limit_conn connlimit 30;
limit_req_status 429;
'';
};
} // lib.mapAttrs' (locPath: locOpts: {
name = locPath;
value = {
proxyPass = locOpts.proxyPass;
extraConfig = locOpts.extraConfig;
};
}) opts.extraLocations;
};
}) cfg.domains;
};
# Content Security Policy (configurable per-domain) users.users.nginx.extraGroups = [ "acme" ];
'' + lib.optionalString (opts.contentSecurityPolicy != null) ''
add_header Content-Security-Policy "${opts.contentSecurityPolicy}" always;
'';
locations = {
"/" = {
proxyPass = "http://127.0.0.1:${toString opts.port}";
extraConfig = opts.extraConfig + lib.optionalString (if opts.rateLimit.enable != null then opts.rateLimit.enable else cfg.rateLimit.enable) ''
# Rate limiting
limit_req zone=global burst=${toString (if opts.rateLimit.burst != null then opts.rateLimit.burst else cfg.rateLimit.burst)} nodelay;
limit_conn connlimit 30;
limit_req_status 429;
'';
};
} // lib.mapAttrs' (locPath: locOpts: {
name = locPath;
value = {
proxyPass = locOpts.proxyPass;
extraConfig = locOpts.extraConfig;
};
}) opts.extraLocations;
};
}) cfg.domains;
}; };
}
# Ensure nginx user can access ACME certs
users.users.nginx.extraGroups = [ "acme" ];
};
}