Start of the move from Ansible to NixOS. Services run as native NixOS services where nixpkgs has a module and as Podman containers otherwise; there is no Docker. Shared: users with their Debian UIDs, SSH with the Ansible role's crypto settings, nftables firewall, WireGuard (ops is the hub), sops-nix secrets per host, the restic backup script with its schedule and retention, mail on unit failure. asxpio-services: Xray, SillyTavern, Minecraft (Paper) and the ACME forwarder natively; XMPP stays in containers because the server is a custom build. asxpio-ops: Postgres, Garage and Forgejo natively. Roles, buckets, keys and grants move here from the storage repo's init scripts. Services that own data wait for /var/lib/.data-in-place, so an install never creates empty state where a restore is about to land. migration/*-data.sh move the Debian-era data into the new layout. hosts/rehearsal turns the two hosts into a test pair with its own keys and dummy secrets. Everything here ran there, Postgres, Garage, Forgejo, Minecraft and SillyTavern on a restored copy of production data. Three differences from the containers showed up and are handled: Garage rejects a 0440 credential file, Geyser needs libcrypto and netlink, SillyTavern ignores the module's listen flags. Production is untouched: both servers still run Debian under Ansible. Assisted-by: Claude:fable-5-2
99 lines
3.1 KiB
Nix
99 lines
3.1 KiB
Nix
# Postgres for the apps. Roles and grants are set on every start from
|
|
# sops, so a password is changed by changing the secret.
|
|
#
|
|
# asxpio owns database asxpio (site, invoices, datacenter bingo admin)
|
|
# bingo read-only on the bingo tables, for datacenterbingo.xyz
|
|
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
let
|
|
pg = config.services.postgresql.package;
|
|
in
|
|
{
|
|
sops.secrets."postgres/asxpio_password" = { };
|
|
sops.secrets."postgres/bingo_password" = { };
|
|
|
|
services.postgresql = {
|
|
enable = true;
|
|
package = pkgs.postgresql_18;
|
|
# App containers connect from Podman's networks; the firewall decides
|
|
# who reaches the port
|
|
enableTCPIP = true;
|
|
authentication = ''
|
|
host all all 10.88.0.0/16 scram-sha-256
|
|
host all all 10.89.0.0/16 scram-sha-256
|
|
'';
|
|
};
|
|
networking.firewall.interfaces."podman*".allowedTCPPorts = [ 5432 ];
|
|
|
|
asxpio.waitForData = [
|
|
"postgresql"
|
|
"postgresql-setup"
|
|
"postgres-roles"
|
|
];
|
|
|
|
systemd.services.postgres-roles = {
|
|
description = "Postgres roles, databases and grants";
|
|
after = [ "postgresql.target" ];
|
|
requires = [ "postgresql.target" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
path = [ pg ];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
User = "postgres";
|
|
LoadCredential = [
|
|
"asxpio:${config.sops.secrets."postgres/asxpio_password".path}"
|
|
"bingo:${config.sops.secrets."postgres/bingo_password".path}"
|
|
];
|
|
};
|
|
script = ''
|
|
set -eu
|
|
role() {
|
|
psql -v ON_ERROR_STOP=1 -v role="$1" -v pw="$(cat "$CREDENTIALS_DIRECTORY/$1")" <<'SQL'
|
|
SELECT format('CREATE ROLE %I LOGIN', :'role')
|
|
WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = :'role') \gexec
|
|
SELECT format('ALTER ROLE %I WITH LOGIN PASSWORD %L', :'role', :'pw') \gexec
|
|
SQL
|
|
}
|
|
role asxpio
|
|
role bingo
|
|
|
|
psql -tAc "SELECT 1 FROM pg_database WHERE datname = 'asxpio'" | grep -q 1 \
|
|
|| psql -c 'CREATE DATABASE asxpio OWNER asxpio'
|
|
psql -d asxpio -c 'CREATE EXTENSION IF NOT EXISTS citext'
|
|
|
|
# Tables that do not exist yet (fresh install, asxpio not migrated)
|
|
# are granted on the next start
|
|
psql -c 'ALTER ROLE bingo SET default_transaction_read_only = on'
|
|
psql -d asxpio -v ON_ERROR_STOP=1 <<'SQL'
|
|
GRANT CONNECT ON DATABASE asxpio TO bingo;
|
|
GRANT USAGE ON SCHEMA public TO bingo;
|
|
DO $$
|
|
DECLARE t text;
|
|
BEGIN
|
|
FOREACH t IN ARRAY ARRAY['bingo_datacenters', 'bingo_incidents', 'bingo_monitor_targets',
|
|
'bingo_monitor_samples', 'bingo_monitor_buckets'] LOOP
|
|
IF to_regclass('public.' || t) IS NOT NULL THEN
|
|
EXECUTE format('GRANT SELECT ON public.%I TO bingo', t);
|
|
ELSE
|
|
RAISE NOTICE 'bingo: % missing, not granted yet', t;
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|
|
SQL
|
|
'';
|
|
};
|
|
|
|
asxpio.backup.hookPackages = [
|
|
pg
|
|
pkgs.util-linux
|
|
];
|
|
asxpio.backup.hooks = [
|
|
''runuser -u postgres -- pg_dumpall > "$STAGE/pg_dumpall.sql"''
|
|
''runuser -u postgres -- pg_dump -Fc asxpio > "$STAGE/asxpio.dump"''
|
|
];
|
|
}
|