From 46f856d6f59c33c9f03f15da76c7ee2eb81f2feb Mon Sep 17 00:00:00 2001 From: Elisiei Yehorov Date: Tue, 21 Apr 2026 19:03:23 +0200 Subject: [PATCH] feat: psql flake --- flake.nix | 62 +++++++++++++++++++++++++++---------------------- psql/.envrc | 1 + psql/.gitignore | 1 + psql/flake.nix | 57 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 28 deletions(-) create mode 100644 psql/.envrc create mode 100644 psql/.gitignore create mode 100644 psql/flake.nix diff --git a/flake.nix b/flake.nix index 76b000c..8057947 100644 --- a/flake.nix +++ b/flake.nix @@ -1,34 +1,40 @@ { description = "nix flakes"; - outputs = {self}: { - templates = { - c = { - path = ./c; - description = "c template"; + outputs = + { self }: + { + templates = { + 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; + }; } diff --git a/psql/.envrc b/psql/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/psql/.envrc @@ -0,0 +1 @@ +use flake diff --git a/psql/.gitignore b/psql/.gitignore new file mode 100644 index 0000000..483bdef --- /dev/null +++ b/psql/.gitignore @@ -0,0 +1 @@ +.postgres* diff --git a/psql/flake.nix b/psql/flake.nix new file mode 100644 index 0000000..5db4936 --- /dev/null +++ b/psql/flake.nix @@ -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" </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 + '' + ); + }; + }; +}