-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.nix
59 lines (52 loc) · 1.35 KB
/
shell.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{ pkgs ? import <nixpkgs> {} }:
let
in pkgs.mkShell {
packages = with pkgs; [
nodejs-18_x
nodePackages.npm
postgresql_14
hey
];
# env variables used by posgresql and its cli (psql)
PGDATA = "${toString ./.}/.pg";
PGUSER = "postgres";
PGDATABASE = "analytics";
# custom postgres config
postgresConf =
pkgs.writeText "postgresql.conf"
''
# Add Custom Settings
log_min_messages = warning
log_min_error_statement = error
log_min_duration_statement = 100 # ms
log_connections = on
log_disconnections = on
log_duration = on
log_timezone = 'UTC'
log_statement = 'all'
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
logging_collector = on
log_min_error_statement = error
unix_socket_directories = '$PGDATA'
'';
# run the postgresql
shellHook = ''
echo "Using postgresql."
# Setup: other env variables
export PGHOST="$PGDATA"
# Setup: DB
[ ! -d $PGDATA ] && pg_ctl initdb -o "-U $PGUSER" && cat "$postgresConf" >> $PGDATA/postgresql.conf
pg_ctl -o "-k $PGDATA" start
createdb $PGDATABASE
psql < schema.sql
function end {
echo "Shutting down the database..."
pg_ctl stop
echo "Removing directories..."
rm -rf $PGDATA
}
trap end EXIT
alias pg="psql -U $PGUSER"
'';
}