Files
homelab-config/homelab-services/postgresql-db/flake.nix
T

162 lines
5.8 KiB
Nix
Raw Normal View History

{
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 = {
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";
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;
2025-11-18 23:03:01 -06:00
description = ''
File containing postgresql user credentials.
Only the Passwords. Names of Users just follow the pattern:
<DB_Name>_produser
<DB_Name>_devuser
Password Format:
PSQL_<DB>_PASSWORD=password
PSQL_<DB>_DEV_PASSWORD=dev_password
2025-11-18 23:03:01 -06:00
'';
};
databases = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
2025-11-18 23:03:01 -06:00
description = "list of databases to bootstrap. Will expand into DB, and DB-DEV for each item. And each Database will recieve it's own user. The user credentials must be in the correct format in the credentials file.";
};
};
config = lib.mkIf opts.enable {
users.groups.postgres = {};
users.users.postgres = {
isSystemUser = true;
createHome = true;
home = opts.dataDir;
group = "postgres";
};
services.postgresql = {
enable = true;
2025-11-19 01:18:07 -06:00
enableTCPIP = true;
dataDir = opts.dataDir;
settings.port = opts.port;
2025-11-16 01:01:00 -06:00
identMap = ''
postgres roy postgres
'';
2025-11-18 23:03:01 -06:00
# allow remote connections to dev databases
2025-11-23 19:01:32 -06:00
authentication = pkgs.lib.mkOverride 10 ''
local all postgres peer map=postgres
local all all peer
# Dev can be connected to via the LAN, Prod only via localhost
${lib.concatStringsSep " " (map (db: "host ${db}_dev ${db}_devuser samenet md5\n") opts.databases) }
${lib.concatStringsSep " " (map (db: "local ${db} ${db}_produser md5\n") opts.databases) }
2025-11-18 23:03:01 -06:00
'';
};
systemd.services.bootstrap-psql = {
2025-11-19 02:02:16 -06:00
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
2025-11-16 00:09:43 -06:00
psql_bin=${pkgs.postgresql}/bin/psql
for db in ${lib.escapeShellArgs opts.databases}; do
db_upper="''${db^^}"
2025-11-18 23:03:01 -06:00
dev_user_var="PSQL_''${db_upper}_DEV_USER"
dev_pass_var="PSQL_''${db_upper}_DEV_PASSWORD"
user_val="$db"_produser
pass_val=$(eval "echo \''${$pass_var:-}")
dev_user_val="$db"_devuser
2025-11-18 23:03:01 -06:00
dev_pass_val=$(eval "echo \''${$dev_pass_var:-}")
if [ -z "$user_val" ] || [ -z "$pass_val" ]; then
echo "Missing credentials for database '$db' ($user_var / $pass_var)" >&2
exit 1
fi
2025-11-18 23:03:01 -06:00
if [ -z "$dev_user_val" ] || [ -z "$dev_pass_val" ]; then
echo "Missing credentials for database '$db'_dev ($dev_user_var / $dev_pass_var)" >&2
exit 1
fi
echo "Bootstrapping PostgreSQL for database: $db"
2025-11-19 01:12:28 -06:00
# Create databases if not exists
2025-11-19 01:34:09 -06:00
if $psql_bin -c "\l" | grep -ci ""$db" "; then
2025-11-19 01:12:28 -06:00
echo "$db already exists, skipping creation."
else
echo "Creating database $db"
$psql_bin -c "CREATE DATABASE "$db";"
fi
2025-11-19 01:34:09 -06:00
if $psql_bin -c "\l" | grep -ci "$db"_dev; then
2025-11-19 01:12:28 -06:00
echo ""$db"_dev already exists, skipping creation."
else
echo "Creating database "$db"_dev"
2025-11-19 01:30:22 -06:00
$psql_bin -c "CREATE DATABASE "$db"_dev;"
2025-11-19 01:12:28 -06:00
fi
2025-11-18 23:40:30 -06:00
# Create users if not exists
2025-11-19 01:34:09 -06:00
if $psql_bin -c "\du" | grep -ci "$user_val"; then
2025-11-19 01:12:28 -06:00
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 -c "CREATE ROLE "$user_val" WITH LOGIN PASSWORD '$pass_val';"
fi
2025-11-18 23:40:30 -06:00
2025-11-19 01:34:09 -06:00
if $psql_bin -c "\du" | grep -ci "$dev_user_val"; then
2025-11-19 01:12:28 -06:00
echo "$dev_user_val already exists, skipping creation. WARN: password may not be correct. Delete user and allow to be recreated for assurity"
else
echo "Creating $dev_user_val"
$psql_bin -c "CREATE ROLE "$dev_user_val" WITH LOGIN PASSWORD '$dev_pass_val';"
fi
# Give users privileges on databases (always)
2025-11-19 00:37:33 -06:00
$psql_bin -c "GRANT ALL PRIVILEGES ON DATABASE "$db" TO "$user_val";"
$psql_bin -c "GRANT ALL PRIVILEGES ON DATABASE "$db"_dev TO "$dev_user_val";"
2025-11-18 23:40:30 -06:00
done
'';
2025-11-16 00:53:13 -06:00
};
};
2025-11-19 02:45:45 -06:00
2025-11-23 19:03:19 -06:00
networking.firewall.allowedTCPPorts = lib.mkIf opts.enable [ opts.port] ;
};
};
};
}