Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

Typelist 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

Typelist 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

Typestring
Defaultif 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 when connecting from other hosts.


server.database.postgres

Typeattribute set of (submodule)
Default{ }

This option has no description.


server.database.postgres.<name>.database

Typestring
Default"‹name›"

This option has no description.


server.database.postgres.<name>.host

Typestring
Defaultconfig.server.database.host

This option has no description.


server.database.postgres.<name>.password

Typesubmodule
Default{ }

This option has no description.


server.database.postgres.<name>.password.group

Typenull or string
Defaultnull

This option has no description.


server.database.postgres.<name>.password.owner

Typenull or string
Defaultnull

This option has no description.


server.database.postgres.<name>.password.path

Typeabsolute path
Defaultconfig.sops.secrets."POSTGRES/${ toUpper config.server.database.postgres.${name}.database |> builtins.replaceStrings [ "-" ] [ "_" ] }_PASSWORD".path;

This option has no description.


server.database.postgres.<name>.port

Typesigned integer
Defaultconfig.server.database.postgres.‹name›.port

This option has no description.


server.database.postgres.<name>.user

Typestring
Default"‹name›"

This option has no description.


server.database.redis

Typeattribute set of (submodule)
Default{ }

This option has no description.


server.database.redis.<name>.database_id

Typesigned integer
DefaultstaticDbIdMappings.‹name› or (-1)

This option has no description.


server.database.redis.<name>.host

Typestring
Defaultconfig.server.database.host

This option has no description.


server.database.redis.<name>.port

Typesigned integer
Default(getIOPrimaryHostAttr "services.redis.servers")."".port

This option has no description.


server.database.redis.<name>.prefix

Typestring
Default"‹name›"

This option has no description.


server.database.postgres

Typeattribute set of (submodule)
Default{ }

This option has no description.


server.database.postgres.<name>.database

Typestring
Default"‹name›"

This option has no description.


server.database.postgres.<name>.host

Typestring
Defaultconfig.server.database.host

This option has no description.


server.database.postgres.<name>.password

Typesubmodule
Default{ }

This option has no description.


server.database.postgres.<name>.password.group

Typenull or string
Defaultnull

This option has no description.


server.database.postgres.<name>.password.owner

Typenull or string
Defaultnull

This option has no description.


server.database.postgres.<name>.password.path

Typeabsolute path
Defaultconfig.sops.secrets."POSTGRES/${ toUpper config.server.database.postgres.${name}.database |> builtins.replaceStrings [ "-" ] [ "_" ] }_PASSWORD".path;

This option has no description.


server.database.postgres.<name>.port

Typesigned integer
Defaultconfig.server.database.postgres.‹name›.port

This option has no description.


server.database.postgres.<name>.user

Typestring
Default"‹name›"

This option has no description.


server.database.redis

Typeattribute set of (submodule)
Default{ }

This option has no description.


server.database.redis.<name>.database_id

Typesigned integer
DefaultstaticDbIdMappings.‹name› or (-1)

This option has no description.


server.database.redis.<name>.host

Typestring
Defaultconfig.server.database.host

This option has no description.


server.database.redis.<name>.port

Typesigned integer
Default(getIOPrimaryHostAttr "services.redis.servers")."".port

This option has no description.


server.database.redis.<name>.prefix

Typestring
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 the postgresql-setup service.
  • 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.json to ensure consistent ID assignment across the fleet.
  • Password Management: A shared password for the primary Redis instance is managed via REDIS/PASSWORD in SOPS.
  • Tooling: Use the update-redis-mappings command 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 shared wait-for-db-tools package.
  • On Database Coordinator: The db-database-coordinator service manages the drain and undrain signals sent to clients during system startup and shutdown. It is bound to PostgreSQL and Redis, restarts with either database, and retries a failed undrain every 30 seconds until every client is reachable. While active, db-database-coordinator-reconcile.timer re-sends undrain every 5 minutes so clients that stop their target later are recovered automatically.

Secrets

  • POSTGRES/<DB_NAME_UPPER>_PASSWORD: Per-database role password, provisioned during postgresql-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 under hosts/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 restarts db-guardian.service on that host.
  • db-guardian/CLIENT_CERT, db-guardian/CLIENT_KEY: Coordinator material, under the database primary’s host directory. Rotating either restarts db-database-coordinator.service on 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.postgres or server.database.redis; the submodule wires up connection info, secrets, and lifecycle binding.

References