-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow developers to use nix-shell to get started (#554)
* Fix: Developers needed to manually create a PostgreSQL and Redis databases Solution: Provide a `shell.nix` file so developers can just run `nix-shell` to start temporary PostgreSQL and Redis servers. The exit signal of the shell is `trap`-ed in order to automatically stop the services on exit. * Fix: nix-shell did not provide Python dependencies * Doc: Document usage of nix-shell * Fix: There was no CI on nix-shell * fixup! git depth * fixup! matrix run on macos too * fixup! bump action versions * fixup! CI fixes * fixup! CI fixes * fixup! CI fixes * fixup! CI fixes * WIP: Try fixing greenlet on macos * Add nix installation link Co-authored-by: Hugo Herter <[email protected]> --------- Co-authored-by: Mike Hukiewitz <[email protected]>
- Loading branch information
1 parent
e90725f
commit e7877d5
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Test nix-shell | ||
|
||
on: | ||
push: | ||
branches: | ||
- '*' | ||
|
||
jobs: | ||
nix-shell: | ||
strategy: | ||
matrix: | ||
os: [ macos-latest, ubuntu-latest ] | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
# Fetch the whole history for all tags and branches (required for aleph.__version__) | ||
fetch-depth: 0 | ||
|
||
- name: Setup empty config file | ||
run: touch config.yml | ||
|
||
- name: Set up Nix | ||
uses: cachix/install-nix-action@v25 | ||
with: | ||
# Use channel nixos-23.11 for Linux and nixpkgs-23.11-darwin for macOS | ||
nix_path: nixpkgs=channel:${{ matrix.os == 'macos-latest' && 'nixpkgs-23.11-darwin' || 'nixos-23.11' }} | ||
|
||
- name: Run tests | ||
run: nix-shell --run "pytest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ pkgs ? import <nixpkgs> {} }: | ||
|
||
pkgs.mkShell { | ||
buildInputs = [ | ||
pkgs.postgresql | ||
pkgs.redis | ||
pkgs.kubo | ||
|
||
pkgs.python311 | ||
pkgs.python311Packages.virtualenv | ||
pkgs.python311Packages.pip | ||
pkgs.python311Packages.setuptools | ||
|
||
pkgs.python311Packages.secp256k1 | ||
pkgs.python311Packages.fastecdsa | ||
pkgs.python311Packages.greenlet | ||
]; | ||
|
||
shellHook = '' | ||
echo "Setting up PostgreSQL environment..." | ||
export PGDATA=$(mktemp -d) | ||
PG_SOCKET_DIR=$(mktemp -d) | ||
echo "Initializing database..." | ||
initdb $PGDATA | ||
echo "Starting PostgreSQL with custom socket directory..." | ||
pg_ctl -D $PGDATA -o "-k $PG_SOCKET_DIR" -l logfile start | ||
# Wait a bit for the server to start | ||
sleep 1 | ||
# Create the 'aleph' role and a database | ||
createuser -h $PG_SOCKET_DIR aleph | ||
createdb -h $PG_SOCKET_DIR aleph -O aleph | ||
# Create a temporary directory for Redis | ||
export REDIS_DATA_DIR=$(mktemp -d) | ||
redis-server --daemonize yes --dir $REDIS_DATA_DIR --bind 127.0.0.1 --port 6379 | ||
echo "Redis server started. Data directory is $REDIS_DATA_DIR" | ||
echo "Starting IPFS Kubo..." | ||
export IPFS_PATH=$(mktemp -d) | ||
ipfs init | ||
ipfs daemon & | ||
echo "IPFS Kubo started. Data directory is $IPFS_PATH" | ||
echo | ||
echo "PostgreSQL started. Data directory is $PGDATA, Socket directory is $PG_SOCKET_DIR" | ||
echo "Redis started. Data directory is $REDIS_DATA_DIR" | ||
echo "Use 'psql -h $PG_SOCKET_DIR' to connect to the database." | ||
echo "Use 'redis-cli -p 6379' to connect to the Redis server." | ||
echo "To stop PostgreSQL: 'pg_ctl -D $PGDATA stop'" | ||
echo "To manually stop Redis: 'redis-cli -p 6379 shutdown'" | ||
# Trap the EXIT signal to stop services when exiting the shell | ||
trap 'echo "Stopping PostgreSQL..."; pg_ctl -D "$PGDATA" stop; echo "Stopping Redis..."; redis-cli -p 6379 shutdown; echo "Stopping IPFS Kubo..."; ipfs shutdown; deactivate' EXIT | ||
# Create a virtual environment in the current directory if it doesn't exist | ||
if [ ! -d "venv" ]; then | ||
python3 -m virtualenv venv | ||
fi | ||
# Install the required Python packages | ||
./venv/bin/pip install -e .\[testing\] | ||
# Activate the virtual environment | ||
source venv/bin/activate | ||
''; | ||
} |