2025-11-15 23:46:57 -06:00
|
|
|
{
|
|
|
|
|
description = "Declare a new table and user for postgresql";
|
|
|
|
|
|
|
|
|
|
inputs = {
|
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
outputs = { self, nixpkgs, ...}: {
|
|
|
|
|
nixosModules.postgresql-db = {config, lib, pkgs, ...}:
|
|
|
|
|
let
|
|
|
|
|
opts = config.services.postgresql-db;
|
|
|
|
|
in {
|
2025-11-16 00:06:04 -06:00
|
|
|
options.services.postgresql-db = {
|
2025-11-15 23:46:57 -06:00
|
|
|
enable = lib.mkEnableOption "Postgres make DBs";
|
|
|
|
|
|
|
|
|
|
dataDir = lib.mkOption {
|
|
|
|
|
type = lib.types.path;
|
2025-11-16 00:07:49 -06:00
|
|
|
default = "/var/lib/postgresql";
|
2025-11-15 23:46:57 -06:00
|
|
|
description = "Where to store database data";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
|
|
|
|
type = lib.types.port;
|
|
|
|
|
default = 5432;
|
|
|
|
|
description = "port to host postgresql";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
credentialsFile = lib.mkOption {
|
|
|
|
|
type = lib.types.path;
|
|
|
|
|
description = "File containing postgresql user credentials";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
databases = lib.mkOption {
|
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
|
default = [];
|
|
|
|
|
description = "list of databases to bootstrap";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = lib.mkIf opts.enable {
|
|
|
|
|
users.groups.postgres = {};
|
|
|
|
|
users.users.postgres = {
|
|
|
|
|
isSystemUser = true;
|
|
|
|
|
createHome = true;
|
|
|
|
|
home = opts.dataDir;
|
|
|
|
|
group = "postgres";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.postgresql = {
|
|
|
|
|
enable = true;
|
|
|
|
|
dataDir = opts.dataDir;
|
|
|
|
|
settings.port = opts.port;
|
2025-11-16 01:01:00 -06:00
|
|
|
identMap = ''
|
|
|
|
|
postgres roy postgres
|
|
|
|
|
two roy freakonomics
|
|
|
|
|
'';
|
2025-11-15 23:46:57 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
systemd.services.bootstrap-psql = {
|
2025-11-16 01:01:00 -06:00
|
|
|
description = "Bootstrap psql dbs and users";
|
2025-11-15 23:46:57 -06:00
|
|
|
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
|
|
|
|
|
|
2025-11-16 00:09:43 -06:00
|
|
|
psql_bin=${pkgs.postgresql}/bin/psql
|
2025-11-15 23:46:57 -06:00
|
|
|
|
|
|
|
|
for db in ${lib.escapeShellArgs opts.databases}; do
|
|
|
|
|
db_upper="''${db^^}"
|
|
|
|
|
|
|
|
|
|
user_var="PSQL_''${db_upper}_USER"
|
|
|
|
|
pass_var="PSQL_''${db_upper}_PASSWORD"
|
|
|
|
|
|
|
|
|
|
user_val=$(eval "echo \''${$user_var:-}")
|
|
|
|
|
pass_val=$(eval "echo \''${$pass_var:-}")
|
|
|
|
|
|
|
|
|
|
if [ -z "$user_val" ] || [ -z "$pass_val" ]; then
|
|
|
|
|
echo "Missing credentials for database '$db' ($user_var / $pass_var)" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "Bootstrapping PostgreSQL for database: $db"
|
|
|
|
|
|
2025-11-16 00:53:13 -06:00
|
|
|
# CREATE USER (if not exists)
|
2025-11-15 23:46:57 -06:00
|
|
|
$psql_bin --tuples-only --no-align -c \
|
2025-11-16 18:42:43 -06:00
|
|
|
"SELECT 1 FROM pg_roles WHERE rolname='$user_val'" | grep -q 1 \
|
|
|
|
|
|| $psql_bin -c "CREATE USER ''${user_val} WITH PASSWORD '$pass_val';"
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2025-11-16 00:53:13 -06:00
|
|
|
# CREATE DATABASE (if not exists)
|
2025-11-15 23:46:57 -06:00
|
|
|
$psql_bin --tuples-only --no-align -c \
|
2025-11-16 18:42:43 -06:00
|
|
|
"SELECT 1 FROM pg_database WHERE datname='$db'" | grep -q 1 \
|
|
|
|
|
|| $psql_bin -c "CREATE DATABASE ''${db} OWNER '\$user_val';"
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2025-11-16 00:53:13 -06:00
|
|
|
# Privileges
|
|
|
|
|
$psql_bin -c "ALTER DATABASE ''${db} OWNER TO ''${user_val};"
|
|
|
|
|
$psql_bin -d "$db" -c "GRANT ALL PRIVILEGES ON DATABASE ''${db} TO ''${user_val};"
|
2025-11-15 23:46:57 -06:00
|
|
|
|
2025-11-16 00:53:13 -06:00
|
|
|
echo "Bootstrapped DB ''${db} (user: ''${user_val})"
|
2025-11-15 23:46:57 -06:00
|
|
|
done
|
|
|
|
|
'';
|
2025-11-16 00:53:13 -06:00
|
|
|
};
|
2025-11-15 23:46:57 -06:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|