Files
nix-config/hosts/idols-aquamarine/postgresql.nix
T
ryan4yin | 二花 21d64e7add security: hardening pass 1 (AppArmor, firewall default, loopback metrics, k3s perms, SSH X11) (#266)
* security: make firewall secure-by-default, disable explicitly on servers

Flip the base firewall default to ON so new hosts are protected by default. Servers keep the firewall off (trusted internal LAN, WAN protected at the router) via explicit overrides in modules/nixos/server/{server,server-aarch64}.nix. Behavior-preserving for all 18 current hosts; adds a security-firewall eval test guarding the per-host state.

* test: fix security-firewall eval test to scalar per-host values

* security: enable AppArmor (complain) on all Linux hosts

* security: bind aquamarine metrics exporters to loopback

* security: restrict k3s kubeconfig file mode to 600

* security: disable SSH X11 forwarding on servers, keep on desktops

* security: add conservative systemd hardening to aquamarine services

* docs(apparmor): correct FHS-alias comment (nixpkgs has no broad FHS layer)

nixpkgs only creates /run/current-system, /usr/bin/env and /bin/sh; it does not
provide a broad FHS->store alias tree, so FHS-oriented abstractions are incomplete
on NixOS. They are safe only because the profiles run in complain mode.

* fix(aquamarine): scrape same-host exporters over loopback

The v2ray/postgres/sftpgo exporters were bound to 127.0.0.1 but VictoriaMetrics
scraped them via the host's routable IP, so those scrapes refused connections.
Point the three same-host scrape targets at 127.0.0.1 (VM runs on the same host).
node-exporter keeps the host IP since it binds 0.0.0.0.

* Revert "security: add conservative systemd hardening to aquamarine services"

This reverts commit f9cf99dadb.
2026-08-27 00:13:01 +08:00

132 lines
4.2 KiB
Nix

{
config,
pkgs,
lib,
myvars,
...
}:
let
inherit (myvars) username;
user = "postgres"; # postgresql's default system user
package = pkgs.postgresql_16;
dataDir = "/data/apps/postgresql/${package.psqlSchema}";
in
{
# Create Directories
# https://www.freedesktop.org/software/systemd/man/latest/tmpfiles.d.html#Type
systemd.tmpfiles.rules = [
"d /data/apps/postgresql 0700 ${user} ${user}"
"d ${dataDir} 0700 ${user} ${user}"
];
# https://wiki.nixos.org/wiki/PostgreSQL
# https://search.nixos.org/options?channel=unstable&query=services.postgresql.
# https://www.postgresql.org/docs/
services.postgresql = {
enable = true;
inherit package dataDir;
# https://www.postgresql.org/docs/16/jit.html
# JIT compilation is beneficial primarily for long-running CPU-bound queries.
# Frequently these will be analytical queries. For short queries the added overhead
# of performing JIT compilation will often be higher than the time it can save.
enableJIT = true;
enableTCPIP = true;
# Ensures that the specified databases exist.
ensureDatabases = [
"playground" # for testing
];
ensureUsers = [
{
name = "playground";
ensureDBOwnership = true;
}
];
initdbArgs = [
"--data-checksums"
"--allow-group-access"
];
extraPlugins =
ps: with ps; [
# postgis
# pg_repack
];
# https://www.postgresql.org/docs/16/runtime-config.html
settings = {
port = 5432;
# connections
max_connections = 100;
# logging
log_connections = true;
log_statement = "all";
logging_collector = true;
log_disconnections = true;
log_destination = lib.mkForce "syslog";
# ssl
ssl = true;
ssl_cert_file = "${../../certs/ecc-server.crt}";
ssl_key_file = config.age.secrets."postgres-ecc-server.key".path;
ssl_min_protocol_version = "TLSv1.3";
ssl_ecdh_curve = "secp384r1";
# Using custom DH parameters reduces the exposure
# dhparam -out dhparams.pem 2048
# ssl_dh_params_file = "";
# memory
shared_buffers = "128MB";
huge_pages = "try";
};
# Map the systemUser to the DBUser
# allow root & myself to log in via psql -U postgres without any additional authentication.
identMap = ''
# ArbitraryMapName systemUser DBUser
superuser_map root postgres
superuser_map postgres postgres
superuser_map postgres-exporter postgres
superuser_map ${username} postgres
# Let other names login as themselves
superuser_map /^(.*)$ \1
'';
# https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
authentication = lib.mkForce ''
# TYPE DATABASE USER ADDRESS METHOD OPTIONS
# "local" is for Unix domain socket connections only
local all all peer map=superuser_map
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
# Other Remote Access - allow access only the database with the same name as the user
host sameuser all 0.0.0.0/0 scram-sha-256
'';
# initialScript =
# pkgs.writeText "backend-initScript" ''
# '';
};
services.prometheus.exporters.postgres = {
enable = true;
# loopback only: scraped by VictoriaMetrics on the same host
listenAddress = "127.0.0.1";
port = 9187;
user = "postgres-exporter";
group = "postgres-exporter";
dataSourceName = "user=postgres database=postgres host=/run/postgresql sslmode=verify-full";
};
}