-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>"` | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters