feat: psql flake
This commit is contained in:
parent
1b87665ea2
commit
46f856d6f5
4 changed files with 93 additions and 28 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
{
|
{
|
||||||
description = "nix flakes";
|
description = "nix flakes";
|
||||||
|
|
||||||
outputs = {self}: {
|
outputs =
|
||||||
|
{ self }:
|
||||||
|
{
|
||||||
templates = {
|
templates = {
|
||||||
c = {
|
c = {
|
||||||
path = ./c;
|
path = ./c;
|
||||||
|
|
@ -23,6 +25,10 @@
|
||||||
path = ./odin;
|
path = ./odin;
|
||||||
description = "odin template";
|
description = "odin template";
|
||||||
};
|
};
|
||||||
|
psql = {
|
||||||
|
path = ./psql;
|
||||||
|
description = "psql template";
|
||||||
|
};
|
||||||
crystal = {
|
crystal = {
|
||||||
path = ./crystal;
|
path = ./crystal;
|
||||||
description = "crystal template";
|
description = "crystal template";
|
||||||
|
|
|
||||||
1
psql/.envrc
Normal file
1
psql/.envrc
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
use flake
|
||||||
1
psql/.gitignore
vendored
Normal file
1
psql/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
.postgres*
|
||||||
57
psql/flake.nix
Normal file
57
psql/flake.nix
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
{
|
||||||
|
# original via @estebxn
|
||||||
|
description = "Tiny PostgreSQL flake";
|
||||||
|
|
||||||
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
|
||||||
|
outputs =
|
||||||
|
{ nixpkgs, ... }:
|
||||||
|
let
|
||||||
|
system = "x86_64-linux";
|
||||||
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
pg = pkgs.postgresql_16;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
apps.${system}.default = {
|
||||||
|
type = "app";
|
||||||
|
program = toString (
|
||||||
|
pkgs.writeShellScript "run-postgres" ''
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
: ''${PGDATA:=$PWD/.postgres}
|
||||||
|
: ''${PGHOST:=$PWD/.postgres-socket}
|
||||||
|
: ''${PGPORT:=5432}
|
||||||
|
: ''${PGUSER:=postgres}
|
||||||
|
: ''${PGDATABASE:=app}
|
||||||
|
|
||||||
|
mkdir -p "$PGHOST"
|
||||||
|
|
||||||
|
if [ ! -d "$PGDATA" ]; then
|
||||||
|
${pg}/bin/initdb -D "$PGDATA" --auth=trust --username="$PGUSER"
|
||||||
|
cat >> "$PGDATA/postgresql.conf" <<EOF
|
||||||
|
listen_addresses = '127.0.0.1'
|
||||||
|
port = $PGPORT
|
||||||
|
unix_socket_directories = '$PGHOST'
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
${pg}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/postgres.log" start
|
||||||
|
trap '${pg}/bin/pg_ctl -D "$PGDATA" stop' EXIT
|
||||||
|
|
||||||
|
until ${pg}/bin/pg_isready -h "$PGHOST" -p "$PGPORT" >/dev/null 2>&1; do
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
${pg}/bin/psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" postgres \
|
||||||
|
-tc "SELECT 1 FROM pg_database WHERE datname = '$PGDATABASE'" \
|
||||||
|
| grep -q 1 || ${pg}/bin/createdb -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" "$PGDATABASE"
|
||||||
|
|
||||||
|
echo "Postgres is running on port $PGPORT"
|
||||||
|
echo "psql -h $PGHOST -p $PGPORT -U $PGUSER $PGDATABASE"
|
||||||
|
|
||||||
|
while true; do sleep 60; done
|
||||||
|
''
|
||||||
|
);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue