Skip to content

Commit

Permalink
feat(pg): add postgresql devenv
Browse files Browse the repository at this point in the history
  • Loading branch information
r17x committed Jul 6, 2023
1 parent c9899c2 commit b797ad9
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ In this section is require nix installed in your system, here steps to install:
| [node14](./node14) | `nodejs@v14`, `[email protected]`, `pnpm@5` |
| [go](./go) | `[email protected]`, `gotools`, `golangci-lint` |
| [react-native](./react-native) | [See Details](./react-native/flake.nix#L192-L212) |
| [pg](./pg) | `postgresql 14` |

* using as development environment: `nix develop "github:efishery/dvt?dir=<NAME>"`

Expand Down
43 changes: 43 additions & 0 deletions pg/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions pg/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
description = "A Nix-flake-based PostgreSQL development environment";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, utils }:
{
overlay = final: prev:
let pgutil = prev.callPackage ./pgutil.nix { }; in
{
inherit (pgutil) init_pg create_pg_db start_pg stop_pg;
};

} // utils.lib.eachDefaultSystem (system:
let
postgresqlVersion = 14;
overlays = [
(final: prev: {
postgresql = prev."postgresql_${toString postgresqlVersion}";
})

self.overlay
];
pkgs = import nixpkgs { inherit overlays system; };
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
# postgresql 14 (specified by overlay)
postgresql

init_pg
create_pg_db
start_pg
stop_pg
];

# UNCOMMENT & FILL the value of this env variable if you used this flake template in your project.
# PGUSER = "";
# PGPASSWORD = "";
# PGHOST = "";
# PGPORT = "";

shellHook = ''
psql --version
'';
};

packages = {
inherit (pkgs) init_pg create_pg_db start_pg stop_pg;
};

});
}

78 changes: 78 additions & 0 deletions pg/pgutil.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Special Thanks to:
# * @Rizary - https://github.com/Rizary
# * nix-community - https://github.com/nix-community/todomvc-nix/blob/master/nix/database/pgutil.nix

{ writeScriptBin, stdenv }:
# Change .pgdata location to $HOME/.pgdata to anticipate permission error
{
init_pg = writeScriptBin "init-pg"
''
#!${stdenv.shell}
pg_pid=""
set -euo pipefail
# TODO: explain what's happening here
LOCAL_PGHOST=$PGHOST
LOCAL_PGPORT=$PGPORT
LOCAL_PGUSER=$PGUSER
LOCAL_PGPASSWORD=$PGPASSWORD
initdb -D $HOME/.pgdata
echo "unix_socket_directories = '$(mktemp -d)'" >> $HOME/.pgdata/postgresql.conf
start-pg
create-pg-db default
unset PGUSER PGPASSWORD
psql postgres -w -c "CREATE ROLE $LOCAL_PGUSER WITH LOGIN PASSWORD '$LOCAL_PGPASSWORD'"
'';

create_pg_db = writeScriptBin "create-pg-db"
''
#!${stdenv.shell}
if [ -z "$1" ]; then
echo "Error: create-pg-db <DATABASE NAME>. Please provide the required argument."
exit 1
fi
pg_pid=""
set -euo pipefail
LOCAL_PGDATABASE=$1
LOCAL_PGHOST=$PGHOST
LOCAL_PGPORT=$PGPORT
LOCAL_PGUSER=$PGUSER
LOCAL_PGPASSWORD=$PGPASSWORD
unset PGUSER PGPASSWORD
psql postgres -w -c "CREATE DATABASE $LOCAL_PGDATABASE"
psql postgres -w -c "GRANT ALL PRIVILEGES ON DATABASE $LOCAL_PGDATABASE TO $LOCAL_PGUSER"
'';

start_pg = writeScriptBin "start-pg"
''
#!${stdenv.shell}
pg_pid=""
set -euo pipefail
LOCAL_PGHOST=$PGHOST
LOCAL_PGPORT=$PGPORT
LOCAL_PGUSER=$PGUSER
LOCAL_PGPASSWORD=$PGPASSWORD
unset PGUSER PGPASSWORD
# TODO: port
pg_ctl -D "$HOME/.pgdata" -w start || (echo pg_ctl failed; exit 1)
until psql postgres -c "SELECT 1" > /dev/null 2>&1 ; do
echo waiting for pg
sleep 0.5
done
'';

stop_pg = writeScriptBin "stop-pg"
''
#!${stdenv.shell}
pg_pid=""
set -euo pipefail
pg_ctl -D $HOME/.pgdata -w -m immediate stop
'';
}
5 changes: 5 additions & 0 deletions templates.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
description = "React Native development environment";
};

pg = {
path = ./pg;
description = "PostgreSQL development environment";
};

}

0 comments on commit b797ad9

Please sign in to comment.