Server Database — Managed PostgreSQL and Redis
Purpose
The database submodule provides a managed interface for PostgreSQL and Redis across the server infrastructure.
It centralises database configuration on the Database Coordinator (config.server.databasePrimaryHost)
while allowing client services to declaratively request databases.
It automates provisioning of PostgreSQL databases and roles, management of Redis database IDs via static mappings,
synchronisation of service lifecycle with database availability using the DB
Guardian, and automated password handling via SOPS secrets.
Entry Point
The module is implemented across several files in modules/nixos/server/database/:
- Main file: default.nix: Core options and connection management.
- Supporting file: postgres.nix: PostgreSQL-specific provisioning and secrets.
- Supporting file: redis.nix: Redis-specific ID mappings and security.
- Supporting file: guardian.nix: Lifecycle synchronisation and the DB Guardian.
Options
server.database.dependentServices
| Type | list of string |
| Default | [ ] |
List of systemd service names that depend on databases. These services will be automatically bound to the db-databases.target and will stop/start when databases become unavailable/available.
server.database.guardian.allowedClientCommonNames
| Type | list of string |
| Default | [ "db-guardian-coordinator" ] |
Client certificate common names the guardian accepts. Guardian leaves are issued only by the DB Guardian intermediate CA, so this list decides which of those client identities may drive the database availability target.
server.database.host
| Type | string |
| Default | if isThisIOPrimaryHost then "localhost" else config.server.ioPrimaryHost |
The hostname or IP address to use when connecting to managed databases.
This is “localhost” when running on the host,
and
server.database.postgres
| Type | attribute set of (submodule) |
| Default | { } |
This option has no description.
server.database.postgres.<name>.database
| Type | string |
| Default | "‹name›" |
This option has no description.
server.database.postgres.<name>.host
| Type | string |
| Default | config.server.database.host |
This option has no description.
server.database.postgres.<name>.password
| Type | submodule |
| Default | { } |
This option has no description.
server.database.postgres.<name>.password.group
| Type | null or string |
| Default | null |
This option has no description.
server.database.postgres.<name>.password.owner
| Type | null or string |
| Default | null |
This option has no description.
server.database.postgres.<name>.password.path
| Type | absolute path |
| Default | config.sops.secrets."POSTGRES/${ toUpper config.server.database.postgres.${name}.database |> builtins.replaceStrings [ "-" ] [ "_" ] }_PASSWORD".path; |
This option has no description.
server.database.postgres.<name>.port
| Type | signed integer |
| Default | config.server.database.postgres.‹name›.port |
This option has no description.
server.database.postgres.<name>.user
| Type | string |
| Default | "‹name›" |
This option has no description.
server.database.redis
| Type | attribute set of (submodule) |
| Default | { } |
This option has no description.
server.database.redis.<name>.database_id
| Type | signed integer |
| Default | staticDbIdMappings.‹name› or (-1) |
This option has no description.
server.database.redis.<name>.host
| Type | string |
| Default | config.server.database.host |
This option has no description.
server.database.redis.<name>.port
| Type | signed integer |
| Default | (getIOPrimaryHostAttr "services.redis.servers")."".port |
This option has no description.
server.database.redis.<name>.prefix
| Type | string |
| Default | "‹name›" |
This option has no description.
server.database.postgres
| Type | attribute set of (submodule) |
| Default | { } |
This option has no description.
server.database.postgres.<name>.database
| Type | string |
| Default | "‹name›" |
This option has no description.
server.database.postgres.<name>.host
| Type | string |
| Default | config.server.database.host |
This option has no description.
server.database.postgres.<name>.password
| Type | submodule |
| Default | { } |
This option has no description.
server.database.postgres.<name>.password.group
| Type | null or string |
| Default | null |
This option has no description.
server.database.postgres.<name>.password.owner
| Type | null or string |
| Default | null |
This option has no description.
server.database.postgres.<name>.password.path
| Type | absolute path |
| Default | config.sops.secrets."POSTGRES/${ toUpper config.server.database.postgres.${name}.database |> builtins.replaceStrings [ "-" ] [ "_" ] }_PASSWORD".path; |
This option has no description.
server.database.postgres.<name>.port
| Type | signed integer |
| Default | config.server.database.postgres.‹name›.port |
This option has no description.
server.database.postgres.<name>.user
| Type | string |
| Default | "‹name›" |
This option has no description.
server.database.redis
| Type | attribute set of (submodule) |
| Default | { } |
This option has no description.
server.database.redis.<name>.database_id
| Type | signed integer |
| Default | staticDbIdMappings.‹name› or (-1) |
This option has no description.
server.database.redis.<name>.host
| Type | string |
| Default | config.server.database.host |
This option has no description.
server.database.redis.<name>.port
| Type | signed integer |
| Default | (getIOPrimaryHostAttr "services.redis.servers")."".port |
This option has no description.
server.database.redis.<name>.prefix
| Type | string |
| Default | "‹name›" |
This option has no description.
Architecture / Services / Scope
Connection Management
The server.database.host option determines how services connect to databases.
On the Database Coordinator (config.server.databasePrimaryHost), it defaults to localhost.
On all other hosts, it defaults to the value of config.server.databasePrimaryHost.
PostgreSQL Management
When a service defines a database in server.database.postgres:
- Automatic Provisioning: The Database Coordinator automatically creates the database and a role with the same name.
- Password Management: A SOPS secret is expected at
POSTGRES/<DB_NAME_UPPER>_PASSWORD. Database names containing hyphens (-) replace them with underscores (_) when constructing the secret path. The system automatically sets this password for the role during thepostgresql-setupservice. - Aggregated Configuration: The Database Coordinator collects all PostgreSQL requirements from across the entire flake to ensure all necessary extensions and initial scripts are loaded.
Redis Management
Redis management uses a similar aggregation pattern:
- Database IDs: Because Redis uses numeric IDs (0-15),
the system uses the shared mapping file
data/redis/mappings.jsonto ensure consistent ID assignment across the fleet. - Password Management: A shared password for the primary Redis instance is managed via
REDIS/PASSWORDin SOPS. - Tooling: Use the
update-redis-mappingscommand to update the shared mapping file when adding new Redis clients.
DB Guardian Coordination
Lifecycle management is handled by the DB Guardian. Guardian communication is mutually authenticated TLS: the coordinator presents a client certificate, each guardian presents a per-host server certificate, and both sides trust only the DB Guardian Intermediate CA.
- On Clients: Services that use these database modules are automatically bound to
db-databases.target. This ensures they only start when the remote databases are reachable and stop before the databases go offline. Reachability and readiness checks are handled by the sharedwait-for-db-toolspackage. - On Database Coordinator: The
db-database-coordinatorservice manages thedrainandundrainsignals sent to clients during system startup and shutdown. It is bound to PostgreSQL and Redis, restarts with either database, and retries a failedundrainevery 30 seconds until every client is reachable. While active,db-database-coordinator-reconcile.timerre-sendsundrainevery 5 minutes so clients that stop their target later are recovered automatically.
Secrets
POSTGRES/<DB_NAME_UPPER>_PASSWORD: Per-database role password, provisioned duringpostgresql-setup.REDIS/PASSWORD: Shared password for the primary Redis instance.db-guardian/CA_CERT: Trust anchor for both sides, stored as a binary SOPS file underhosts/server/<host>/db-guardian-tls/. It exists on every host that runs a guardian and on the database primary, and holds the DB Guardian intermediate rather than the fleet root.db-guardian/SERVER_CERT,db-guardian/SERVER_KEY: Per-guardian-host server material, under the same directory. Rotating either restartsdb-guardian.serviceon that host.db-guardian/CLIENT_CERT,db-guardian/CLIENT_KEY: Coordinator material, under the database primary’s host directory. Rotating either restartsdb-database-coordinator.serviceon the primary.
Operational Notes / Assumptions
- The host designated as the Database Coordinator (
config.server.databasePrimaryHost) is responsible for running the actual database engines. It aggregates all database requirements from every host in the flake and applies them locally. - Client services request databases by declaring entries in
server.database.postgresorserver.database.redis; the submodule wires up connection info, secrets, and lifecycle binding.