2025-11-15 23:46:57 -06:00
|
|
|
{
|
|
|
|
|
description = "Declare a new table and user for postgresql";
|
|
|
|
|
|
|
|
|
|
inputs = {
|
2026-06-20 16:34:54 -05:00
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
|
2025-11-15 23:46:57 -06:00
|
|
|
};
|
|
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
outputs =
|
2026-06-20 16:34:54 -05:00
|
|
|
{ ... }:
|
2026-05-20 00:00:37 -05:00
|
|
|
{
|
|
|
|
|
nixosModules.postgresql-db =
|
|
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
let
|
|
|
|
|
opts = config.services.postgresql-db;
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options.services.postgresql-db = {
|
|
|
|
|
enable = lib.mkEnableOption "Postgres make DBs";
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
dataDir = lib.mkOption {
|
|
|
|
|
type = lib.types.path;
|
|
|
|
|
default = "/var/lib/postgresql";
|
|
|
|
|
description = "Where to store database data";
|
|
|
|
|
};
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
port = lib.mkOption {
|
|
|
|
|
type = lib.types.port;
|
|
|
|
|
default = 5432;
|
|
|
|
|
description = "port to host postgresql";
|
|
|
|
|
};
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
credentialsFile = lib.mkOption {
|
|
|
|
|
type = lib.types.path;
|
|
|
|
|
description = ''
|
|
|
|
|
File containing postgresql user credentials.
|
|
|
|
|
Only the Passwords. Names of Users just follow the pattern:
|
|
|
|
|
<DB_Name>_produser
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
Password Format:
|
|
|
|
|
PSQL_<DB>_PASSWORD=password
|
|
|
|
|
'';
|
|
|
|
|
};
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
databases = lib.mkOption {
|
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
|
default = [ ];
|
2026-09-12 15:02:09 -05:00
|
|
|
description = ''
|
|
|
|
|
List of databases to bootstrap.
|
|
|
|
|
Each Database will recieve it's own user.
|
|
|
|
|
The user credentials must be in the correct format in the credentials file.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ipMasks = lib.mkOption {
|
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
|
default = [ ];
|
|
|
|
|
description = ''
|
|
|
|
|
List of ipMasks that psql will accept connections from
|
|
|
|
|
'';
|
2025-11-16 00:53:13 -06:00
|
|
|
};
|
2025-11-15 23:46:57 -06:00
|
|
|
};
|
2025-11-19 02:45:45 -06:00
|
|
|
|
2026-05-20 00:00:37 -05:00
|
|
|
config = lib.mkIf opts.enable {
|
|
|
|
|
users.groups.postgres = { };
|
|
|
|
|
users.users.postgres = {
|
|
|
|
|
isSystemUser = true;
|
|
|
|
|
createHome = true;
|
|
|
|
|
home = opts.dataDir;
|
|
|
|
|
group = "postgres";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.postgresql = {
|
|
|
|
|
enable = true;
|
|
|
|
|
enableTCPIP = true;
|
|
|
|
|
dataDir = opts.dataDir;
|
|
|
|
|
settings.port = opts.port;
|
2026-08-24 15:53:07 -05:00
|
|
|
settings.password_encryption = "scram-sha-256";
|
2026-05-20 00:00:37 -05:00
|
|
|
identMap = ''
|
|
|
|
|
postgres roy postgres
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
authentication = pkgs.lib.mkOverride 10 ''
|
|
|
|
|
local all postgres peer map=postgres
|
|
|
|
|
local all all peer
|
|
|
|
|
|
2026-09-12 15:02:09 -05:00
|
|
|
# Prod can be connected via local machine and declared masks
|
|
|
|
|
${lib.concatStringsSep "" (
|
2026-08-24 15:49:12 -05:00
|
|
|
map (db: "host ${db} ${db}_produser 127.0.0.1/32 scram-sha-256\n") opts.databases
|
2026-08-23 14:14:22 -05:00
|
|
|
)}
|
2026-09-12 15:02:09 -05:00
|
|
|
${lib.concatStringsSep "" (
|
2026-08-24 15:49:12 -05:00
|
|
|
map (db: "host ${db} ${db}_produser ::1/128 scram-sha-256\n") opts.databases
|
2026-05-20 00:00:37 -05:00
|
|
|
)}
|
2026-09-12 15:02:09 -05:00
|
|
|
# configurable list of subnets allowed to connect (for example k3s pods subnet)
|
|
|
|
|
# will allow connection to all declared ips. fine for now
|
|
|
|
|
${lib.concatStringsSep "" (
|
|
|
|
|
map (
|
|
|
|
|
db:
|
|
|
|
|
lib.concatStringsSep "" (
|
|
|
|
|
map (mask: "host ${db} ${db}_produser ${opts.ipMasks} scram-sha-256\n") opts.ipMasks
|
|
|
|
|
)
|
|
|
|
|
) opts.databases
|
|
|
|
|
)}
|
2026-05-20 00:00:37 -05:00
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
systemd.services.bootstrap-psql = {
|
|
|
|
|
description = "Bootstrap psql databases and users";
|
|
|
|
|
after = [ "postgresql.service" ];
|
|
|
|
|
requires = [ "postgresql.service" ];
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
User = "postgres";
|
|
|
|
|
Group = "postgres";
|
|
|
|
|
EnvironmentFile = opts.credentialsFile;
|
|
|
|
|
|
|
|
|
|
ExecStart = pkgs.writeShellScript "bootstrap-psql" ''
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
psql_bin=${pkgs.postgresql}/bin/psql
|
|
|
|
|
|
|
|
|
|
for db in ${lib.escapeShellArgs opts.databases}; do
|
|
|
|
|
db_upper="''${db^^}"
|
|
|
|
|
|
|
|
|
|
pass_var="PSQL_''${db_upper}_PASSWORD"
|
|
|
|
|
dev_pass_var="PSQL_''${db_upper}_DEV_PASSWORD"
|
|
|
|
|
|
|
|
|
|
user_val="$db"_produser
|
|
|
|
|
pass_val=$(eval "echo \''${$pass_var:-}")
|
|
|
|
|
|
|
|
|
|
if [ -z "$pass_val" ]; then
|
|
|
|
|
echo "Missing password credentials for database '$db'" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "Bootstrapping PostgreSQL for database: $db"
|
|
|
|
|
|
|
|
|
|
# Create users if not exists
|
|
|
|
|
if $psql_bin --port=${toString opts.port} -c "\du" | grep -ci "$user_val"; then
|
|
|
|
|
echo "$user_val already exists, skipping creation. WARN: password may not be correct. Delete user and allow to be recreated for assurity"
|
|
|
|
|
else
|
|
|
|
|
echo "Creating $user_val"
|
|
|
|
|
$psql_bin --port=${toString opts.port} -c "CREATE ROLE "$user_val" WITH LOGIN PASSWORD '$pass_val';"
|
|
|
|
|
fi
|
|
|
|
|
|
2026-08-23 16:40:35 -05:00
|
|
|
# Create databases if not exists
|
|
|
|
|
if $psql_bin --port=${toString opts.port} -c "\l" | grep -ci ""$db" "; then
|
|
|
|
|
echo "$db already exists, skipping creation."
|
|
|
|
|
else
|
|
|
|
|
echo "Creating database $db"
|
|
|
|
|
$psql_bin --port=${toString opts.port} -c "CREATE DATABASE "$db" WITH OWNER "$user_val";"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Grant ownership (idempotency for weird states)
|
2026-05-20 00:00:37 -05:00
|
|
|
$psql_bin --port=${toString opts.port} -c "ALTER DATABASE "$db" OWNER TO "$user_val";"
|
|
|
|
|
|
|
|
|
|
done
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = lib.mkIf opts.enable [ opts.port ];
|
|
|
|
|
};
|
|
|
|
|
};
|
2025-11-15 23:46:57 -06:00
|
|
|
};
|
|
|
|
|
}
|