Skip to content

Commit dea406f

Browse files
committed
feat(pg): add postgresql devenv
1 parent b2ccbe9 commit dea406f

5 files changed

Lines changed: 185 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ In this section is require nix installed in your system, here steps to install:
5454
| [node14](./node14) | `nodejs@v14`, `yarn@1.22`, `pnpm@5` |
5555
| [go](./go) | `go@v1.19`, `gotools`, `golangci-lint` |
5656
| [react-native](./react-native) | [See Details](./react-native/flake.nix#L192-L212) |
57+
| [pg](./pg) | `postgresql 14` |
5758

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

pg/flake.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pg/flake.nix

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
description = "A Nix-flake-based PostgreSQL development environment";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
6+
utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, utils }:
10+
{
11+
overlay = final: prev:
12+
let pgutil = prev.callPackage ./pgutil.nix { }; in
13+
{
14+
inherit (pgutil) init_pg create_pg_db start_pg stop_pg;
15+
};
16+
17+
} // utils.lib.eachDefaultSystem (system:
18+
let
19+
postgresqlVersion = 14;
20+
overlays = [
21+
(final: prev: {
22+
postgresql = prev."postgresql_${toString postgresqlVersion}";
23+
})
24+
25+
self.overlay
26+
];
27+
pkgs = import nixpkgs { inherit overlays system; };
28+
in
29+
{
30+
devShells.default = pkgs.mkShell {
31+
buildInputs = with pkgs; [
32+
# postgresql 14 (specified by overlay)
33+
postgresql
34+
35+
init_pg
36+
create_pg_db
37+
start_pg
38+
stop_pg
39+
];
40+
41+
# UNCOMMENT & FILL the value of this env variable if you used this flake template in your project.
42+
# PGUSER = "";
43+
# PGPASSWORD = "";
44+
# PGHOST = "";
45+
# PGPORT = "";
46+
47+
shellHook = ''
48+
psql --version
49+
'';
50+
};
51+
52+
packages = {
53+
inherit (pkgs) init_pg create_pg_db start_pg stop_pg;
54+
};
55+
56+
});
57+
}
58+

pg/pgutil.nix

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Special Thanks to:
2+
# * @Rizary - https://github.com/Rizary
3+
# * nix-community - https://github.com/nix-community/todomvc-nix/blob/master/nix/database/pgutil.nix
4+
5+
{ writeScriptBin, stdenv }:
6+
# Change .pgdata location to $HOME/.pgdata to anticipate permission error
7+
{
8+
init_pg = writeScriptBin "init-pg"
9+
''
10+
#!${stdenv.shell}
11+
pg_pid=""
12+
set -euo pipefail
13+
# TODO: explain what's happening here
14+
LOCAL_PGHOST=$PGHOST
15+
LOCAL_PGPORT=$PGPORT
16+
LOCAL_PGUSER=$PGUSER
17+
LOCAL_PGPASSWORD=$PGPASSWORD
18+
19+
initdb -D $HOME/.pgdata
20+
echo "unix_socket_directories = '$(mktemp -d)'" >> $HOME/.pgdata/postgresql.conf
21+
22+
start-pg
23+
create-pg-db default
24+
unset PGUSER PGPASSWORD
25+
26+
psql postgres -w -c "CREATE ROLE $LOCAL_PGUSER WITH LOGIN PASSWORD '$LOCAL_PGPASSWORD'"
27+
28+
'';
29+
30+
create_pg_db = writeScriptBin "create-pg-db"
31+
''
32+
#!${stdenv.shell}
33+
if [ -z "$1" ]; then
34+
echo "Error: create-pg-db <DATABASE NAME>. Please provide the required argument."
35+
exit 1
36+
fi
37+
38+
pg_pid=""
39+
set -euo pipefail
40+
LOCAL_PGDATABASE=$1
41+
LOCAL_PGHOST=$PGHOST
42+
LOCAL_PGPORT=$PGPORT
43+
LOCAL_PGUSER=$PGUSER
44+
LOCAL_PGPASSWORD=$PGPASSWORD
45+
unset PGUSER PGPASSWORD
46+
47+
psql postgres -w -c "CREATE DATABASE $LOCAL_PGDATABASE"
48+
psql postgres -w -c "GRANT ALL PRIVILEGES ON DATABASE $LOCAL_PGDATABASE TO $LOCAL_PGUSER"
49+
'';
50+
51+
start_pg = writeScriptBin "start-pg"
52+
''
53+
#!${stdenv.shell}
54+
pg_pid=""
55+
set -euo pipefail
56+
LOCAL_PGHOST=$PGHOST
57+
LOCAL_PGPORT=$PGPORT
58+
LOCAL_PGUSER=$PGUSER
59+
LOCAL_PGPASSWORD=$PGPASSWORD
60+
unset PGUSER PGPASSWORD
61+
# TODO: port
62+
pg_ctl -D "$HOME/.pgdata" -w start || (echo pg_ctl failed; exit 1)
63+
64+
until psql postgres -c "SELECT 1" > /dev/null 2>&1 ; do
65+
echo waiting for pg
66+
sleep 0.5
67+
done
68+
'';
69+
70+
stop_pg = writeScriptBin "stop-pg"
71+
''
72+
#!${stdenv.shell}
73+
pg_pid=""
74+
set -euo pipefail
75+
76+
pg_ctl -D $HOME/.pgdata -w -m immediate stop
77+
'';
78+
}

templates.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919
description = "React Native development environment";
2020
};
2121

22+
pg = {
23+
path = ./pg;
24+
description = "PostgreSQL development environment";
25+
};
26+
2227
}

0 commit comments

Comments
 (0)