Skip to content

Commit

Permalink
Allow developers to use nix-shell to get started (#554)
Browse files Browse the repository at this point in the history
* 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
hoh and MHHukiewitz authored Feb 23, 2024
1 parent e90725f commit e7877d5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/nix-ci.yml
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"
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ pip install -e .[testing,docs]

You're ready to go!

### Developer setup using Nix

We started to add Nix as an easy way to setup a development environment.
This is still a work in progress and not all dependencies are covered yet.

To use it, you need to [have Nix installed on your system](https://nixos.org/download.html). Then you can run:

```bash
nix-shell
```
This will provide you with a shell with PostgreSQL, Redis, and IPFS running.

## Software used

The Aleph CCN is written in Python and requires Python v3.8+. It will not work with older versions of Python.
Expand Down
69 changes: 69 additions & 0 deletions shell.nix
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
'';
}

0 comments on commit e7877d5

Please sign in to comment.