diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..78a370f --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix index 9b586a7..8486e85 100644 --- a/flake.nix +++ b/flake.nix @@ -7,13 +7,15 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { - nixpkgs, - rust-overlay, - flake-utils, - ... - }: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { + nixpkgs, + rust-overlay, + flake-utils, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; @@ -29,52 +31,62 @@ "clippy" ]; }; + + postgresql = pkgs.postgresql_17; + postgresqlPackages = postgresql.pkgs; in { devShells.default = pkgs.mkShell { - packages = with pkgs; [ + packages = [ rust - # Database + # PostgreSQL + matching PostGIS postgresql postgresqlPackages.postgis - sqlx-cli + + # SQLx migrations / CLI + pkgs.sqlx-cli # GIS - gdal - geos - proj + pkgs.gdal + pkgs.geos + pkgs.proj - # Build dependencies - pkg-config - openssl + # Rust native dependencies + pkgs.pkg-config + pkgs.openssl ]; shellHook = '' set -e - # ------------------------------------------------------------ - # Local PostgreSQL - # ------------------------------------------------------------ + # ============================================================ + # Landkreis-Sprints development environment + # ============================================================ export PGDATA="$PWD/.pgdata" export PGHOST="$PWD/.pgsocket" - export PGPORT="5433" - export PGDATABASE="county_sprints" export PGUSER="county_sprints" + export RUST_BACKTRACE=1 + + if [ -z "''${RUST_LOG:-}" ]; then + export RUST_LOG="info" + fi + mkdir -p "$PGHOST" - # ------------------------------------------------------------ + # ============================================================ # Initialise PostgreSQL cluster - # ------------------------------------------------------------ + # ============================================================ if [ ! -f "$PGDATA/PG_VERSION" ]; then echo echo "==> Initialising local PostgreSQL cluster" initdb \ + --username="$PGUSER" \ --auth=trust \ --no-locale \ --encoding=UTF8 \ @@ -82,21 +94,9 @@ >/dev/null fi - # ------------------------------------------------------------ - # Configure PostgreSQL - # ------------------------------------------------------------ - - # Always write our development settings. This means changing - # the flake also updates an already existing local cluster. - cat > "$PGDATA/postgresql.conf" </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 - # ------------------------------------------------------------ - # Create application role - # ------------------------------------------------------------ + # ============================================================ + # Wait until PostgreSQL is ready + # ============================================================ - if ! psql \ - -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'" + postgres_ready=0 - psql \ + for _ in $(seq 1 50); do + if pg_isready \ -h "$PGHOST" \ - -p "$PGPORT" \ - -d postgres \ - -v ON_ERROR_STOP=1 \ - -c \ - "CREATE ROLE \"$PGUSER\" LOGIN;" + -U "$PGUSER" \ + >/dev/null 2>&1 + then + postgres_ready=1 + 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 - # ------------------------------------------------------------ + # ============================================================ # Create application database - # ------------------------------------------------------------ + # ============================================================ if ! psql \ -h "$PGHOST" \ - -p "$PGPORT" \ + -U "$PGUSER" \ -d postgres \ -tAc \ "SELECT 1 @@ -170,84 +169,55 @@ EOF echo echo "==> Creating PostgreSQL database '$PGDATABASE'" - psql \ + createdb \ -h "$PGHOST" \ - -p "$PGPORT" \ - -d postgres \ - -v ON_ERROR_STOP=1 \ - -c \ - "CREATE DATABASE \"$PGDATABASE\" OWNER \"$PGUSER\";" + -U "$PGUSER" \ + "$PGDATABASE" fi - # ------------------------------------------------------------ - # Make sure the application role owns the database - # ------------------------------------------------------------ - - psql \ - -h "$PGHOST" \ - -p "$PGPORT" \ - -d postgres \ - -v ON_ERROR_STOP=1 \ - -c \ - "ALTER DATABASE \"$PGDATABASE\" OWNER TO \"$PGUSER\";" \ - >/dev/null - - # ------------------------------------------------------------ - # PostGIS - # ------------------------------------------------------------ + # ============================================================ + # Enable PostGIS + # ============================================================ echo echo "==> Checking PostGIS" psql \ -h "$PGHOST" \ - -p "$PGPORT" \ + -U "$PGUSER" \ -d "$PGDATABASE" \ -v ON_ERROR_STOP=1 \ - -c \ - "CREATE EXTENSION IF NOT EXISTS postgis;" \ + -c "CREATE EXTENSION IF NOT EXISTS postgis;" \ >/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 - # ------------------------------------------------------------ - - export RUST_BACKTRACE=1 - - if [ -z "''${RUST_LOG:-}" ]; then - export RUST_LOG="info" - fi - - # ------------------------------------------------------------ - # Info - # ------------------------------------------------------------ + # ============================================================ + # Development information + # ============================================================ echo - echo "==============================================" + echo "============================================================" echo " Landkreis-Sprints development environment" - echo "==============================================" + echo "============================================================" echo echo "Rust:" - rustc --version + echo " $(rustc --version)" echo echo "PostgreSQL:" - psql \ - -h "$PGHOST" \ - -p "$PGPORT" \ - -d "$PGDATABASE" \ - -tAc "SELECT version();" | - sed 's/^/ /' + echo " version: $(psql --version)" + echo " host: $PGHOST" + echo " database: $PGDATABASE" + echo " user: $PGUSER" echo echo "PostGIS:" psql \ -h "$PGHOST" \ - -p "$PGPORT" \ + -U "$PGUSER" \ -d "$PGDATABASE" \ -tAc "SELECT PostGIS_Full_Version();" | sed 's/^/ /' @@ -255,11 +225,14 @@ EOF echo "DATABASE_URL:" echo " $DATABASE_URL" echo - echo "Run:" + echo "Commands:" echo " sqlx migrate run" + echo " cargo check" echo " cargo test" echo " cargo run" echo + echo "============================================================" + echo ''; }; }