-
Notifications
You must be signed in to change notification settings - Fork 2
chore: replace docker-compose with nix process-compose for dev deps #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| flake = false; | ||
| }; | ||
| crane.url = "github:ipetkov/crane"; | ||
| process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; | ||
| }; | ||
| outputs = { | ||
| self, | ||
|
|
@@ -23,6 +24,7 @@ | |
| rust-overlay, | ||
| advisory-db, | ||
| crane, | ||
| process-compose-flake, | ||
| }: | ||
| flake-utils.lib.eachDefaultSystem | ||
| (system: let | ||
|
|
@@ -66,29 +68,157 @@ | |
| mdbook | ||
| bacon | ||
| postgresql | ||
| docker-compose | ||
| ytt | ||
| podman | ||
| podman-compose | ||
| process-compose | ||
| curl | ||
| ]; | ||
|
|
||
| pgPort = 5432; | ||
| pgUser = "user"; | ||
| pgPassword = "password"; | ||
| pgDatabase = "pg"; | ||
|
|
||
| devEnvVars = rec { | ||
| PGDATABASE = "pg"; | ||
| PGUSER = "user"; | ||
| PGPASSWORD = "password"; | ||
| PGDATABASE = pgDatabase; | ||
| PGUSER = pgUser; | ||
| PGPASSWORD = pgPassword; | ||
| PGHOST = "127.0.0.1"; | ||
| DATABASE_URL = "postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}:5432/pg?sslmode=disable"; | ||
| PGPORT = toString pgPort; | ||
| DATABASE_URL = "postgres://${pgUser}:${pgPassword}@127.0.0.1:${toString pgPort}/${pgDatabase}?sslmode=disable"; | ||
| PG_CON = "${DATABASE_URL}"; | ||
| }; | ||
|
|
||
| podman-runner = pkgs.callPackage ./nix/podman-runner.nix {}; | ||
| # ── Postgres start helper ────────────────────────────────────────── | ||
| pg-start = pkgs.writeShellApplication { | ||
| name = "pg-start"; | ||
| runtimeInputs = [pkgs.postgresql pkgs.coreutils]; | ||
| text = '' | ||
| NAME="$1" PORT="$2" PGUSER="$3" DB="$4" | ||
| PGDATA="$PWD/.nix-deps/$NAME" | ||
|
|
||
| mkdir -p "$PWD/.nix-deps" | ||
|
|
||
| if [ ! -f "$PGDATA/PG_VERSION" ]; then | ||
| echo "[$NAME] Initializing data directory at $PGDATA..." | ||
| mkdir -p "$PGDATA" | ||
| initdb -D "$PGDATA" --username="$PGUSER" --auth=trust --no-locale -E UTF8 | ||
| { | ||
| echo "port = $PORT" | ||
| echo "unix_socket_directories = '/tmp'" | ||
| echo "listen_addresses = '127.0.0.1'" | ||
| } >> "$PGDATA/postgresql.conf" | ||
| fi | ||
|
|
||
| if [ -f "$PGDATA/postmaster.pid" ]; then | ||
| pg_ctl -D "$PGDATA" stop -m immediate 2>/dev/null || rm -f "$PGDATA/postmaster.pid" | ||
| fi | ||
|
|
||
| postgres -D "$PGDATA" -p "$PORT" -k /tmp & | ||
| PG_PID=$! | ||
| trap 'kill $PG_PID 2>/dev/null; wait $PG_PID 2>/dev/null' EXIT | ||
|
|
||
| while ! pg_isready -p "$PORT" -U "$PGUSER" -h 127.0.0.1 -q 2>/dev/null; do | ||
| sleep 0.1 | ||
| done | ||
|
|
||
| if [ "$DB" != "$PGUSER" ]; then | ||
| createdb -p "$PORT" -U "$PGUSER" -h 127.0.0.1 "$DB" 2>/dev/null || { | ||
| if psql -p "$PORT" -U "$PGUSER" -h 127.0.0.1 -lqt | cut -d \| -f 1 | grep -qw "$DB"; then | ||
| echo "[$NAME] Database '$DB' already exists" | ||
| else | ||
| echo "[$NAME] ERROR: Failed to create database '$DB'" >&2 | ||
| exit 1 | ||
| fi | ||
| } | ||
| fi | ||
|
|
||
| echo "[$NAME] Ready on port $PORT (database: $DB)" | ||
| wait $PG_PID | ||
| ''; | ||
| }; | ||
|
|
||
| setupDbDev = pkgs.writeShellApplication { | ||
| name = "setup-db-dev"; | ||
| runtimeInputs = [pkgs.sqlx-cli]; | ||
| text = '' | ||
| export DATABASE_URL="${devEnvVars.DATABASE_URL}" | ||
| exec sqlx migrate run | ||
| ''; | ||
| }; | ||
|
|
||
| # ── process-compose: core-pg + setup-db ──────────────────────────── | ||
| pcLib = import process-compose-flake.lib {inherit pkgs;}; | ||
|
|
||
| mkPg = { | ||
| name, | ||
| port, | ||
| user, | ||
| db, | ||
| }: { | ||
| command = "${ | ||
| pkgs.writeShellApplication { | ||
| name = "start-${name}"; | ||
| runtimeInputs = [pg-start]; | ||
| text = '' | ||
| exec pg-start ${name} ${toString port} ${user} ${db} | ||
| ''; | ||
| } | ||
| }/bin/start-${name}"; | ||
| readiness_probe = { | ||
| exec.command = "${ | ||
| pkgs.writeShellApplication { | ||
| name = "ready-${name}"; | ||
| runtimeInputs = [pkgs.postgresql]; | ||
| text = '' | ||
| exec psql -p ${toString port} -U ${user} -h 127.0.0.1 -d ${db} -c 'SELECT 1' -t -q | ||
| ''; | ||
| } | ||
| }/bin/ready-${name}"; | ||
| initial_delay_seconds = 1; | ||
| period_seconds = 1; | ||
| failure_threshold = 60; | ||
| }; | ||
| shutdown = { | ||
| signal = 2; | ||
| timeout_seconds = 10; | ||
| }; | ||
| }; | ||
|
|
||
| baseProcesses = { | ||
| core-pg = mkPg { | ||
| name = "core-pg"; | ||
| port = pgPort; | ||
| user = pgUser; | ||
| db = pgDatabase; | ||
| }; | ||
| setup-db = { | ||
| command = "${setupDbDev}/bin/setup-db-dev"; | ||
| depends_on.core-pg.condition = "process_healthy"; | ||
| availability.exit_on_end = false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this makes DB migrations racy. This makes |
||
| shutdown = { | ||
| signal = 2; | ||
| timeout_seconds = 10; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| nix-deps-base = pcLib.makeProcessCompose { | ||
| name = "nix-deps-base"; | ||
| modules = [ | ||
| { | ||
| settings = { | ||
| log_level = "info"; | ||
| log_location = ".nix-deps/process-compose.log"; | ||
| processes = baseProcesses; | ||
| }; | ||
| } | ||
| ]; | ||
| }; | ||
|
|
||
| # ── CI test runner ──────────────────────────────────────────────── | ||
| nextest-runner = pkgs.writeShellScriptBin "nextest-runner" '' | ||
| set -e | ||
|
|
||
| export PATH="${pkgs.lib.makeBinPath [ | ||
| podman-runner.podman-compose-runner | ||
| pkgs.wait4x | ||
| pkgs.sqlx-cli | ||
| pkgs.cargo-nextest | ||
| pkgs.coreutils | ||
|
|
@@ -104,22 +234,19 @@ | |
| export PGUSER="${devEnvVars.PGUSER}" | ||
| export PGPASSWORD="${devEnvVars.PGPASSWORD}" | ||
| export PGHOST="${devEnvVars.PGHOST}" | ||
| export PGPORT="${devEnvVars.PGPORT}" | ||
|
|
||
| cleanup() { | ||
| echo "Stopping deps..." | ||
| ${podman-runner.podman-compose-runner}/bin/podman-compose-runner down || true | ||
| ${nix-deps-base}/bin/nix-deps-base down 2>/dev/null || true | ||
| } | ||
|
|
||
| trap cleanup EXIT | ||
|
|
||
| echo "Starting PostgreSQL..." | ||
| ${podman-runner.podman-compose-runner}/bin/podman-compose-runner up -d | ||
| mkdir -p .nix-deps | ||
|
|
||
| echo "Waiting for PostgreSQL to be ready..." | ||
| ${pkgs.wait4x}/bin/wait4x postgresql "$DATABASE_URL" --timeout 120s | ||
|
|
||
| echo "Running database migrations..." | ||
| ${pkgs.sqlx-cli}/bin/sqlx migrate run | ||
| echo "Starting PostgreSQL via process-compose..." | ||
| ${nix-deps-base}/bin/nix-deps-base up -D | ||
| ${nix-deps-base}/bin/nix-deps-base project is-ready --wait | ||
|
|
||
| echo "Running mdbook tests..." | ||
| rm -rf ''${CARGO_TARGET_DIR:-./target}/mdbook-test | ||
|
|
@@ -141,6 +268,13 @@ | |
| with pkgs; { | ||
| packages = { | ||
| nextest = nextest-runner; | ||
| setup-db-dev = setupDbDev; | ||
| inherit nix-deps-base; | ||
| }; | ||
|
|
||
| apps.setup-db-dev = flake-utils.lib.mkApp { | ||
| drv = setupDbDev; | ||
| name = "setup-db-dev"; | ||
| }; | ||
|
|
||
| checks = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we still need
yttin the dev shell.ci/repipestill usesytt, but after this changenix developno longer includes the tool needed to update the Concourse pipeline