diff --git a/.github/workflows/nix-ci.yml b/.github/workflows/nix-ci.yml new file mode 100644 index 000000000..f4b27a758 --- /dev/null +++ b/.github/workflows/nix-ci.yml @@ -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" diff --git a/README.md b/README.md index 20cd46236..4ecb03ce7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/shell.nix b/shell.nix new file mode 100644 index 000000000..ea01f7b35 --- /dev/null +++ b/shell.nix @@ -0,0 +1,69 @@ +{ pkgs ? import {} }: + +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 + ''; +}