From b797ad9b7f4ac45f6752b41cd5ab60b7fd397371 Mon Sep 17 00:00:00 2001 From: r17x Date: Sun, 25 Jun 2023 01:49:45 +0700 Subject: [PATCH] feat(pg): add postgresql devenv --- README.md | 1 + pg/flake.lock | 43 ++++++++++++++++++++++++++++ pg/flake.nix | 58 ++++++++++++++++++++++++++++++++++++++ pg/pgutil.nix | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ templates.nix | 5 ++++ 5 files changed, 185 insertions(+) create mode 100644 pg/flake.lock create mode 100644 pg/flake.nix create mode 100644 pg/pgutil.nix diff --git a/README.md b/README.md index de939c7..d918122 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ In this section is require nix installed in your system, here steps to install: | [node14](./node14) | `nodejs@v14`, `yarn@1.22`, `pnpm@5` | | [go](./go) | `go@v1.19`, `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="` diff --git a/pg/flake.lock b/pg/flake.lock new file mode 100644 index 0000000..68cbe47 --- /dev/null +++ b/pg/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1675698036, + "narHash": "sha256-BgsQkQewdlQi8gapJN4phpxkI/FCE/2sORBaFcYbp/A=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1046c7b92e908a1202c0f1ba3fc21d19e1cf1b62", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/pg/flake.nix b/pg/flake.nix new file mode 100644 index 0000000..5eb000b --- /dev/null +++ b/pg/flake.nix @@ -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; + }; + + }); +} + diff --git a/pg/pgutil.nix b/pg/pgutil.nix new file mode 100644 index 0000000..d4020c5 --- /dev/null +++ b/pg/pgutil.nix @@ -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 . 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 + ''; +} diff --git a/templates.nix b/templates.nix index 553ad4e..ab5bfd6 100644 --- a/templates.nix +++ b/templates.nix @@ -19,4 +19,9 @@ description = "React Native development environment"; }; + pg = { + path = ./pg; + description = "PostgreSQL development environment"; + }; + }