fixup! fix nix develop psql setup

This commit is contained in:
Jonas Rabenstein 2026-08-12 13:37:37 +02:00
commit f4405db550
2 changed files with 197 additions and 128 deletions

96
flake.lock generated Normal file
View file

@ -0,0 +1,96 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1786384358,
"narHash": "sha256-RzPPiWeUtuvymnpuEWsdtzli5w4kjZs49FqEs3/1u+I=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2fcb964de67fcf60b43471c55d5d99e61a9ccb5a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1744536153,
"narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1786507911,
"narHash": "sha256-w5aZRLbiu7H6TqsYXVMdRKg0S4DRaJpxyqxx86AwxVk=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "39db48099ad16834af7e27485a4babf9c28b3897",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

229
flake.nix
View file

@ -7,13 +7,15 @@
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
}; };
outputs = { outputs =
nixpkgs, {
rust-overlay, nixpkgs,
flake-utils, rust-overlay,
... flake-utils,
}: ...
flake-utils.lib.eachDefaultSystem (system: }:
flake-utils.lib.eachDefaultSystem (
system:
let let
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
@ -29,52 +31,62 @@
"clippy" "clippy"
]; ];
}; };
postgresql = pkgs.postgresql_17;
postgresqlPackages = postgresql.pkgs;
in in
{ {
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
packages = with pkgs; [ packages = [
rust rust
# Database # PostgreSQL + matching PostGIS
postgresql postgresql
postgresqlPackages.postgis postgresqlPackages.postgis
sqlx-cli
# SQLx migrations / CLI
pkgs.sqlx-cli
# GIS # GIS
gdal pkgs.gdal
geos pkgs.geos
proj pkgs.proj
# Build dependencies # Rust native dependencies
pkg-config pkgs.pkg-config
openssl pkgs.openssl
]; ];
shellHook = '' shellHook = ''
set -e set -e
# ------------------------------------------------------------ # ============================================================
# Local PostgreSQL # Landkreis-Sprints development environment
# ------------------------------------------------------------ # ============================================================
export PGDATA="$PWD/.pgdata" export PGDATA="$PWD/.pgdata"
export PGHOST="$PWD/.pgsocket" export PGHOST="$PWD/.pgsocket"
export PGPORT="5433"
export PGDATABASE="county_sprints" export PGDATABASE="county_sprints"
export PGUSER="county_sprints" export PGUSER="county_sprints"
export RUST_BACKTRACE=1
if [ -z "''${RUST_LOG:-}" ]; then
export RUST_LOG="info"
fi
mkdir -p "$PGHOST" mkdir -p "$PGHOST"
# ------------------------------------------------------------ # ============================================================
# Initialise PostgreSQL cluster # Initialise PostgreSQL cluster
# ------------------------------------------------------------ # ============================================================
if [ ! -f "$PGDATA/PG_VERSION" ]; then if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo echo
echo "==> Initialising local PostgreSQL cluster" echo "==> Initialising local PostgreSQL cluster"
initdb \ initdb \
--username="$PGUSER" \
--auth=trust \ --auth=trust \
--no-locale \ --no-locale \
--encoding=UTF8 \ --encoding=UTF8 \
@ -82,21 +94,9 @@
>/dev/null >/dev/null
fi fi
# ------------------------------------------------------------ # ============================================================
# Configure PostgreSQL
# ------------------------------------------------------------
# Always write our development settings. This means changing
# the flake also updates an already existing local cluster.
cat > "$PGDATA/postgresql.conf" <<EOF
listen_addresses = '' + "''" + ''
port = 5433
unix_socket_directories = '$PGHOST'
EOF
# ------------------------------------------------------------
# Start PostgreSQL # Start PostgreSQL
# ------------------------------------------------------------ # ============================================================
if ! pg_ctl \ if ! pg_ctl \
-D "$PGDATA" \ -D "$PGDATA" \
@ -108,58 +108,57 @@ EOF
pg_ctl \ pg_ctl \
-D "$PGDATA" \ -D "$PGDATA" \
-o "-k $PGHOST -p $PGPORT" \
-l "$PGDATA/postgresql.log" \ -l "$PGDATA/postgresql.log" \
-o "-k $PGHOST" \
start \ start \
>/dev/null >/dev/null
# Wait until PostgreSQL actually accepts connections.
for i in $(seq 1 30); do
if pg_isready \
-h "$PGHOST" \
-p "$PGPORT" \
>/dev/null 2>&1
then
break
fi
sleep 0.2
done
fi fi
# ------------------------------------------------------------ # ============================================================
# Create application role # Wait until PostgreSQL is ready
# ------------------------------------------------------------ # ============================================================
if ! psql \ postgres_ready=0
-h "$PGHOST" \
-p "$PGPORT" \
-d postgres \
-tAc \
"SELECT 1
FROM pg_roles
WHERE rolname = '$PGUSER'" |
grep -q 1
then
echo
echo "==> Creating PostgreSQL role '$PGUSER'"
psql \ for _ in $(seq 1 50); do
if pg_isready \
-h "$PGHOST" \ -h "$PGHOST" \
-p "$PGPORT" \ -U "$PGUSER" \
-d postgres \ >/dev/null 2>&1
-v ON_ERROR_STOP=1 \ then
-c \ postgres_ready=1
"CREATE ROLE \"$PGUSER\" LOGIN;" break
fi
sleep 0.2
done
if [ "$postgres_ready" != "1" ]; then
echo
echo "ERROR: PostgreSQL could not be started."
echo
echo "PostgreSQL log:"
echo "------------------------------------------------------------"
if [ -f "$PGDATA/postgresql.log" ]; then
cat "$PGDATA/postgresql.log"
else
echo "No PostgreSQL log found."
fi
echo "------------------------------------------------------------"
echo
return 1
fi fi
# ------------------------------------------------------------ # ============================================================
# Create application database # Create application database
# ------------------------------------------------------------ # ============================================================
if ! psql \ if ! psql \
-h "$PGHOST" \ -h "$PGHOST" \
-p "$PGPORT" \ -U "$PGUSER" \
-d postgres \ -d postgres \
-tAc \ -tAc \
"SELECT 1 "SELECT 1
@ -170,84 +169,55 @@ EOF
echo echo
echo "==> Creating PostgreSQL database '$PGDATABASE'" echo "==> Creating PostgreSQL database '$PGDATABASE'"
psql \ createdb \
-h "$PGHOST" \ -h "$PGHOST" \
-p "$PGPORT" \ -U "$PGUSER" \
-d postgres \ "$PGDATABASE"
-v ON_ERROR_STOP=1 \
-c \
"CREATE DATABASE \"$PGDATABASE\" OWNER \"$PGUSER\";"
fi fi
# ------------------------------------------------------------ # ============================================================
# Make sure the application role owns the database # Enable PostGIS
# ------------------------------------------------------------ # ============================================================
psql \
-h "$PGHOST" \
-p "$PGPORT" \
-d postgres \
-v ON_ERROR_STOP=1 \
-c \
"ALTER DATABASE \"$PGDATABASE\" OWNER TO \"$PGUSER\";" \
>/dev/null
# ------------------------------------------------------------
# PostGIS
# ------------------------------------------------------------
echo echo
echo "==> Checking PostGIS" echo "==> Checking PostGIS"
psql \ psql \
-h "$PGHOST" \ -h "$PGHOST" \
-p "$PGPORT" \ -U "$PGUSER" \
-d "$PGDATABASE" \ -d "$PGDATABASE" \
-v ON_ERROR_STOP=1 \ -v ON_ERROR_STOP=1 \
-c \ -c "CREATE EXTENSION IF NOT EXISTS postgis;" \
"CREATE EXTENSION IF NOT EXISTS postgis;" \
>/dev/null >/dev/null
# ------------------------------------------------------------ # ============================================================
# DATABASE_URL for sqlx / application # Application connection string
# ------------------------------------------------------------ # ============================================================
export DATABASE_URL="postgresql://$PGUSER@/$PGDATABASE?host=$PGHOST&port=$PGPORT" export DATABASE_URL="postgresql://$PGUSER@/$PGDATABASE?host=$PGHOST"
# ------------------------------------------------------------ # ============================================================
# Environment # Development information
# ------------------------------------------------------------ # ============================================================
export RUST_BACKTRACE=1
if [ -z "''${RUST_LOG:-}" ]; then
export RUST_LOG="info"
fi
# ------------------------------------------------------------
# Info
# ------------------------------------------------------------
echo echo
echo "==============================================" echo "============================================================"
echo " Landkreis-Sprints development environment" echo " Landkreis-Sprints development environment"
echo "==============================================" echo "============================================================"
echo echo
echo "Rust:" echo "Rust:"
rustc --version echo " $(rustc --version)"
echo echo
echo "PostgreSQL:" echo "PostgreSQL:"
psql \ echo " version: $(psql --version)"
-h "$PGHOST" \ echo " host: $PGHOST"
-p "$PGPORT" \ echo " database: $PGDATABASE"
-d "$PGDATABASE" \ echo " user: $PGUSER"
-tAc "SELECT version();" |
sed 's/^/ /'
echo echo
echo "PostGIS:" echo "PostGIS:"
psql \ psql \
-h "$PGHOST" \ -h "$PGHOST" \
-p "$PGPORT" \ -U "$PGUSER" \
-d "$PGDATABASE" \ -d "$PGDATABASE" \
-tAc "SELECT PostGIS_Full_Version();" | -tAc "SELECT PostGIS_Full_Version();" |
sed 's/^/ /' sed 's/^/ /'
@ -255,11 +225,14 @@ EOF
echo "DATABASE_URL:" echo "DATABASE_URL:"
echo " $DATABASE_URL" echo " $DATABASE_URL"
echo echo
echo "Run:" echo "Commands:"
echo " sqlx migrate run" echo " sqlx migrate run"
echo " cargo check"
echo " cargo test" echo " cargo test"
echo " cargo run" echo " cargo run"
echo echo
echo "============================================================"
echo
''; '';
}; };
} }