feat: psql flake

This commit is contained in:
Elisiei Yehorov 2026-04-21 19:03:23 +02:00
parent 1b87665ea2
commit 46f856d6f5
Signed by: elisiei
GPG key ID: BA1D158DCE3DF089
4 changed files with 93 additions and 28 deletions

View file

@ -1,34 +1,40 @@
{ {
description = "nix flakes"; description = "nix flakes";
outputs = {self}: { outputs =
templates = { { self }:
c = { {
path = ./c; templates = {
description = "c template"; c = {
path = ./c;
description = "c template";
};
go = {
path = ./go;
description = "go template";
};
elixir = {
path = ./elixir;
description = "elixir template";
};
zig = {
path = ./zig;
description = "zig template";
};
odin = {
path = ./odin;
description = "odin template";
};
psql = {
path = ./psql;
description = "psql template";
};
crystal = {
path = ./crystal;
description = "crystal template";
};
}; };
go = {
path = ./go;
description = "go template";
};
elixir = {
path = ./elixir;
description = "elixir template";
};
zig = {
path = ./zig;
description = "zig template";
};
odin = {
path = ./odin;
description = "odin template";
};
crystal = {
path = ./crystal;
description = "crystal template";
};
};
defaultTemplate = self.templates.go; defaultTemplate = self.templates.go;
}; };
} }

1
psql/.envrc Normal file
View file

@ -0,0 +1 @@
use flake

1
psql/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.postgres*

57
psql/flake.nix Normal file
View 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
''
);
};
};
}