From 5ace56e9d85fbf1a93748a48dc523a5da59aa522 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 8 Jan 2021 00:04:47 +0100 Subject: [PATCH] Add PostgreSQL environment --- env/postgresql.nix | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 env/postgresql.nix diff --git a/env/postgresql.nix b/env/postgresql.nix new file mode 100644 index 0000000..370e72c --- /dev/null +++ b/env/postgresql.nix @@ -0,0 +1,28 @@ +{ pkgs ? import { } }: + +with pkgs; + +let + sql_file = "data/db.sql"; + data_dir = "$(pwd)/.pgdata"; + +in mkShell { + buildInputs = [ postgresql ]; + + shellHook = '' + trap "kill 0" EXIT + export PGDATA="${data_dir}" + export PGHOST="${data_dir}" + + if [ ! -d ${data_dir} ]; then + initdb --auth-local=trust --no-locale --encoding=UTF8 + fi + + if ! pg_ctl status; then + pg_ctl start -o "--unix_socket_directories=${data_dir} --listen_addresses='''" + fi + + psql -d postgres -f ${sql_file} + alias psql='psql -d postgres' + ''; +}