Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Merge main into PR branch
if: github.event_name == 'pull_request'
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git fetch origin main
if ! git merge origin/main; then
echo "Merge failed due to conflicts. Please rebase your branch on main."
exit 1
fi

- name: Install Nix
uses: cachix/install-nix-action@v31

- name: Setup Nix cache
uses: nix-community/cache-nix-action@v7
with:
primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
restore-prefixes-first-match: nix-${{ runner.os }}-
# don't keep caches from previous CI runs around. newest main should always be enough
purge: true
purge-prefixes: nix-${{ runner.os }}-
purge-created: 0
purge-primary-key: never

- name: Run checks
run: nix flake check --log-format bar-with-logs
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 86 additions & 50 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
wrappers = {
url = "git+file:///home/trr/dev/RTUnreal/wrappers";
url = "github:lassulus/wrappers";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay.url = "github:oxalica/rust-overlay";
Expand Down Expand Up @@ -31,55 +31,16 @@
pkgs,
lib,
system,
self',
...
}:
let

devState = "$(pwd)/.devState";
pgPort = 5432;

postgres-process = pkgs.writeShellApplication {
name = "postgress-process";
runtimeInputs = [
pkgs.postgresql
];
text = ''
PGDATA="${devState}/postgres/db"
export PGDATA
echo "PGDATA: $PGDATA"
echo "PGHOST: $PGHOST"


copyConfig() {
# TODO: this should be run every startup
cat > "$PGDATA/postgresql.conf" <<EOF
port = ${toString pgPort}
listen_addresses = '''
unix_socket_directories='$PGHOST'
EOF
}
if [[ ! -d "$PGDATA" ]]; then
echo ""
echo "Database missing, regenerating"
echo ""

mkdir -p "$PGDATA"
initdb --locale=C --encoding=UTF8

copyConfig
pg_ctl -D "$PGDATA" -w start

psql --dbname postgres <<'EOF'
CREATE ROLE "app" WITH LOGIN PASSWORD 'app';
CREATE DATABASE "app" OWNER "app";
EOF
pg_ctl -D "$PGDATA" -m fast -w stop
else
copyConfig
fi
exec postgres -D "$PGDATA"
'';
};
pgUser = "app";
pgPW = "app";
pgDB = "app";

pcWrapped =
(inputs.wrappers.wrapperModules.process-compose.apply {
Expand All @@ -97,11 +58,86 @@
processes = {
adminer = {
command = "exec ${lib.getExe pkgs.php} -S 127.0.0.1:8100 -t ${pkgs.adminer} ${pkgs.adminer}/adminer.php";
depends_on.postgres.condition = "process_healthy";
};

postgres = {
command = "${lib.getExe postgres-process}";
};
postgres =
let
PGDATA = "${devState}/postgres/db";

command = pkgs.writeShellApplication {
name = "postgress-process";
runtimeInputs = [
pkgs.postgresql
];
text = ''
PGDATA="${PGDATA}"
export PGDATA
echo "PGDATA: $PGDATA"
echo "PGHOST: $PGHOST"


copyConfig() {
cat > "$PGDATA/postgresql.conf" <<EOF
port = ${toString pgPort}
listen_addresses = '''
unix_socket_directories='$PGHOST'
EOF
}
if [[ ! -d "$PGDATA" ]]; then
echo ""
echo "Database missing, regenerating"
echo ""

mkdir -p "$PGDATA"
initdb --locale=C --encoding=UTF8

copyConfig
pg_ctl -D "$PGDATA" -w start

psql --dbname postgres <<'EOF'
CREATE ROLE "${pgUser}" WITH LOGIN PASSWORD '${pgPW}';
CREATE DATABASE "${pgDB}" OWNER "${pgUser}";
EOF
pg_ctl -D "$PGDATA" -m fast -w stop
else
copyConfig
fi
exec postgres -D "$PGDATA"
'';
};

check = pkgs.writeShellApplication {
name = "postgres-check";
runtimeInputs = [
pkgs.postgresql
];
text = ''
PGDATA="${PGDATA}"
export PGDATA

if (shopt -s nullopt dotglob; f=("$PGHOST"/*); ((''${#f[@]}))); then
pg_isready -d template1 && \
psql -c "SELECT 1" template1 > /dev/null 2>&1
else
echo "Waiting for PostgreSQL initialization to complete..." 2>&1
exit 1
fi
'';
};
in
{
command = lib.getExe command;
readiness_probe = {
exec.command = lib.getExe check;
initial_delay_seconds = 2;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
# SIGINT requests a fast shutdown: https://www.postgresql.org/docs/current/server-shutdown.html
shutdown.signal = 2;
};
};
};
}).wrapper;
Expand Down Expand Up @@ -151,11 +187,11 @@
];

shellHook = ''
export PGHOST="${devState}/postgres-socket"
export PGHOST="${devState}/postgres/socket"
mkdir -p "$PGHOST"
export DATABASE_URL="postgresql://app:app@$(${lib.getExe pkgs.python311} -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe='''))" "$PGHOST")/app"
export DATABASE_URL="postgresql://${pgUser}:${pgPW}@$(${lib.getExe pkgs.python311} -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe='''))" "$PGHOST")/${pgDB}"

echo "Adminer url: http://127.0.0.1:8100/?pgsql=$(${lib.getExe pkgs.python311} -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe='''))" "$PGHOST")"
echo "Adminer url: http://127.0.0.1:8100/?pgsql=$(${lib.getExe pkgs.python311} -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe='''))" "$PGHOST")&username=${pgUser}&db=${pgDB}"
'';
};
};
Expand Down
Loading