bonfire-app/docs/HACKING.md

287 lines
13 KiB
Markdown
Raw Permalink Normal View History

2021-05-22 14:36:40 +00:00
# Development guide
2023-02-15 02:03:01 +00:00
_These instructions are for hacking on Bonfire. If you wish to deploy in production, please refer to our deployment guide instead._
2021-05-22 14:36:40 +00:00
Hello, potential contributor! :-)
2022-06-25 09:00:44 +00:00
This is a work in progress guide to getting up and running as a developer. Please ask questions in the issue tracker if something is not clear and we'll try to improve it.
2021-05-22 14:36:40 +00:00
Happy hacking!
2022-06-25 07:47:12 +00:00
## Status: beta - have fun and provide feedback 🙏
2021-05-22 14:36:40 +00:00
2022-06-25 09:00:44 +00:00
Bonfire is currently beta software. While it's fun to play with it, we would not recommend running any production instances (meaning not using it for your primary fediverse identity) yet because it's not quite ready for that today.
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
## Download
2022-02-28 11:14:01 +00:00
Either way, you need to first clone this repository and change into the directory and then do some configuration:
```sh
$ git clone https://github.com/bonfire-networks/bonfire-app bonfire
$ cd bonfire
```
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
## Configure
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
### Pick a flavour
2021-05-22 16:18:42 +00:00
Bonfire is a flexible platform that powers a variety of social networks. The first thing you have to choose is which app (or "flavour") you want to hack on:
2021-05-22 16:18:42 +00:00
- `classic` ("Bonfire Social", a basic social network that interoperates with the fediverse)
2024-04-14 09:13:11 +00:00
- `community` (for topics and groups)
- `open-science` (for next-gen scientific communities)
- `coordination` (for coordinating around tasks and projects)
2023-03-23 23:49:51 +00:00
- `cooperation` (for building cooperative economic networks)
2022-02-28 11:14:01 +00:00
2022-06-20 02:30:53 +00:00
Note that at the current time, the core team are focusing most of their efforts on the classic flavour and this is where we recommend you start.
2022-02-28 11:14:01 +00:00
2022-10-30 19:41:25 +00:00
You first need to install [just](https://github.com/casey/just#packages) which is a handy tool (a `make` alternative) to run commands defined in `./justfile`.
2022-01-09 10:19:16 +00:00
2023-03-23 23:49:51 +00:00
So for example if you want to run the `classic` flavour, with a fully-docker-managed setup (see the other options available below), run:
2022-01-09 10:19:16 +00:00
2023-03-23 23:49:51 +00:00
- `export FLAVOUR=classic WITH_DOCKER=total`
You may also want to put this in the appropriate place in your system so your choice of flavour is remembered for next time (eg. `~/.bashrc` or `~/.zshrc`)
- Make sure docker daemon is running if you chose a docker managed setup.
2023-03-23 23:49:51 +00:00
- Then run `just config` to initialise some config.
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
### Configure
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
- Then edit the config (especially the secrets) for the current flavour in `./.env`
2021-05-22 14:36:40 +00:00
### Option A - the entry way (fully managed via docker-compose, recommended when you're first exploring)
- Dependencies:
2021-11-13 07:42:51 +00:00
- Recent versions of Docker & [docker-compose](https://docs.docker.com/compose/install/)
2021-05-22 14:36:40 +00:00
2023-03-23 23:49:51 +00:00
- Make sure you've set the environment variable to indicate your choice: `export WITH_DOCKER=total`
2022-05-23 02:20:39 +00:00
- Make sure you've edited your .env file (see above) before getting started and proceed to Hello world!
2021-05-22 14:36:40 +00:00
2022-05-13 21:06:41 +00:00
### Option B - the easy way (with bare-metal elixir, and docker-managed tooling, database & search index, recommended for active development)
2021-05-22 14:36:40 +00:00
> Note: you can use a tool like [mise](https://github.com/jdx/mise) or asdf to setup the environment (run `mise install` in the root directory).
2023-08-20 20:32:45 +00:00
2021-05-22 14:36:40 +00:00
- Dependencies:
2023-08-12 11:29:01 +00:00
- Recent versions of [Elixir](https://elixir-lang.org/install.html) (1.15+) and OTP/erlang (25+)
- [yarn](https://yarnpkg.com)
2022-05-13 21:06:41 +00:00
- Recent versions of Docker & [docker-compose](https://docs.docker.com/compose/install/)
2023-03-23 23:49:51 +00:00
- Make sure you've set the env to indicate your choice: `export WITH_DOCKER=easy`
2022-05-13 21:06:41 +00:00
2022-05-23 02:20:39 +00:00
- Make sure you've edited your .env file (see above) before getting started and proceed to Hello world!
2022-05-13 21:06:41 +00:00
2022-05-13 21:19:55 +00:00
### Option C - the partial way (with bare-metal elixir and tooling, and docker-managed database & search index)
2022-05-13 21:06:41 +00:00
> Note: you can use a tool like [mise](https://github.com/jdx/mise) or asdf to setup the environment (run `mise install` in the root directory).
2023-08-20 20:32:45 +00:00
2022-05-13 21:06:41 +00:00
- Dependencies:
2023-08-12 11:29:01 +00:00
- Recent versions of [Elixir](https://elixir-lang.org/install.html) (1.15+) and OTP/erlang (25+)
2021-11-13 07:42:51 +00:00
- Recent versions of [Rust](https://www.rust-lang.org/tools/install) and Cargo
- [yarn](https://yarnpkg.com)
2021-11-13 07:42:51 +00:00
- Recent versions of Docker & [docker-compose](https://docs.docker.com/compose/install/)
2021-05-22 14:36:40 +00:00
2023-03-23 23:49:51 +00:00
- Make sure you've set the environment variable to indicate your choice: `export WITH_DOCKER=partial`
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
- Make sure you've edited your .env file (see above) before getting started and proceed to Hello world!
2021-05-22 14:36:40 +00:00
2022-05-13 21:06:41 +00:00
### Option D - the bare metal (if you don't use docker)
2021-05-22 14:36:40 +00:00
> Note: you can use a tool like [mise](https://github.com/jdx/mise) or asdf to setup the environment (run `mise install` in the root directory). You will still need to install Postgres and Meili seperately though.
2023-08-20 20:32:45 +00:00
2021-05-22 14:36:40 +00:00
- Dependencies:
2023-08-12 11:29:01 +00:00
- Recent versions of [Elixir](https://elixir-lang.org/install.html) (1.15+) and OTP/erlang (25+)
2021-11-13 07:42:51 +00:00
- Recent versions of [Rust](https://www.rust-lang.org/tools/install) and Cargo
- [yarn](https://yarnpkg.com)
2021-11-13 07:42:51 +00:00
- Postgres 12+ (or rather [Postgis](https://postgis.net/install/) if using the bonfire_geolocate extension)
- [Meili Search](https://docs.meilisearch.com/learn/getting_started/installation.html) (optional)
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
- You will need to set the relevant environment variables in the .env file (see above) to match your local install of Postgres.
2021-05-22 14:36:40 +00:00
- If you want search capabilities, you'll also need to setup a Meili server and set the relevant env variables as well.
2023-03-23 23:49:51 +00:00
- Make sure you've set the environment variable to indicate your choice: `export WITH_DOCKER=no` and proceed to Hello world!
2021-05-22 14:36:40 +00:00
2022-08-01 04:20:00 +00:00
### Option E - the nix one (dev environment with Nix)
2021-05-22 16:01:58 +00:00
Dependencies:
2022-08-01 02:49:01 +00:00
- Run a recent version of Nix or NixOS: https://nixos.org/download.html
2022-08-01 02:49:01 +00:00
- Enable Flakes: https://nixos.wiki/wiki/Flakes#Installing_flakes
2022-08-01 02:49:01 +00:00
- Install [direnv](https://direnv.net/) through nix if you don't have the tool already: `nix profile install nixpkgs#direnv` and add it to your shell: https://direnv.net/docs/hook.html
- Clone the bonfire-app repo if you haven't already and allow direnv to use environment variables:
```
git clone https://github.com/bonfire-networks/bonfire-app
cd `bonfire-app`
direnv allow
```
The tool direnv is necessary for the nix setup as the nix shell environment will use variables defined on `.envrc` to set itself up.
Note: when you run `direnv allow` on the bonfire-app directory for the first time, nix will automatically fetch the dependencies for bonfire. The process will take a while as it's downloading everything needed to use the development environment. Afterwards you will be able to use just fine. Proceeding times you enter the directory, the shell with automatically set up for your use without downloading the packages again.
You will need to update the db directory which is automatically created by nix the first time you initialized the shell with `direnv allow`. You can do so with the following steps:
- Update `props.nix` to the settings you want.
- Run `just nix-db-init` to create the database and user for postgres defined on `props.nix`.
2022-08-01 04:20:00 +00:00
- Modify the `.env` file to comment out all `POSTGRES_*` variables. These are populated automatically by nix. So if the variables are set here, you may get issues with overriding your settings in `props.nix` when using bonfire.
- You can now proceed to Hello World!
2021-05-22 16:01:58 +00:00
2022-08-01 04:20:00 +00:00
Note: if you ever want to shut off the postgres server in nix, simply run the nix-db targets in just:
```
# stop postgres server running locally
just nix-db stop
# start postgres server running locally
just nix-db start
```
2021-05-22 14:36:40 +00:00
## Hello world!
- From a fresh checkout of this repository, this command will fetch the app's dependencies and setup the database (the same commands apply for all three options above):
```
2023-08-26 20:59:18 +00:00
just setup-dev
2021-05-22 14:36:40 +00:00
```
- You should then be able to run the app with:
```
2022-05-23 02:20:39 +00:00
just dev
2021-05-22 14:36:40 +00:00
```
2022-05-23 02:20:39 +00:00
- See the `just` commands below for more things you may want to do.
2021-05-22 14:36:40 +00:00
## Onboarding
By default, the back-end listens on port 4000 (TCP), so you can access it on http://localhost:4000/
2022-02-28 11:14:01 +00:00
Your first step will be to create an account to log in with. The
easiest way to do this is with our mix task:
```
2022-05-23 02:20:39 +00:00
$ just mix bonfire.account.new
2022-02-28 11:14:01 +00:00
Enter an email address: root@localhost
Enter a password:
```
Your password must be at least 10 characters long and the output could be more helpful if you don't do that. This task seems to work most reliably if you open a second terminal window with the devserver running. We're not sure why.
You should then be able to log in and create a user through the web interface.
2022-05-23 02:20:39 +00:00
If you would like to become an administrator, there is a mix task for that too:
2022-02-28 11:14:01 +00:00
```shell
2022-05-23 02:20:39 +00:00
just mix bonfire.user.admin.promote your_username
2022-02-28 11:14:01 +00:00
```
2022-03-24 16:10:48 +00:00
## The Bonfire Environment
2022-02-28 11:14:01 +00:00
2022-03-24 16:10:48 +00:00
We like to think of bonfire as a comfortable way of developing software - there are a lot of
2022-04-30 09:27:07 +00:00
conveniences built in once you know how they all work. The gotcha is that while you don't know them, it can be a bit overwhelming. Don't worry, we've got your back.
2022-02-28 11:14:01 +00:00
2022-04-30 09:27:07 +00:00
* [Architecture](./ARCHITECTURE.md) - an overview of the stack and code structure.
- [Bonfire-flavoured Elixir](./BONFIRE-FLAVOURED-ELIXIR.md) - an introduction to the way we write Elixir.
- [Bonfire's Database: an Introduction](./DATABASE.md) - an overview of how our database is designed.
- [Boundaries](./BOUNDARIES.md) - an introduction to our access control system.
2022-04-30 09:27:07 +00:00
2022-03-24 16:10:48 +00:00
Note: these are still at the early draft stage, we expect to gradually improve documentation over time.
2021-05-22 14:36:40 +00:00
## Documentation
2022-05-23 02:20:39 +00:00
The code is somewhat documented inline. You can generate HTML docs (using `Exdoc`) by running `just docs`.
2021-05-22 14:36:40 +00:00
2021-05-22 16:01:58 +00:00
## Additional information
2021-07-03 08:04:49 +00:00
- messctl is a little utility for programmatically updating the .deps files from which the final elixir dependencies list is compiled by the mess script. The only use of it is in the dep-\* tasks of the Makefile. It is used by some of the project developers and the build does not rely on it.
2021-05-22 16:01:58 +00:00
- `./extensions/` is used to hack on local copies of Bonfire extensions. You can clone an extension from its git repo and use the local version during development, eg: `just dep-clone-local bonfire_me https://github.com/bonfire-networks/bonfire_me`
- `./forks/` is used to hack on local copies of any other dependencies.
2021-05-22 16:01:58 +00:00
2023-07-31 17:56:36 +00:00
- You can migrate the DB when the app is running (also runs automatically on startup): `Bonfire.Common.Repo.migrate`
2021-05-22 16:01:58 +00:00
- You can generate a dependency graph using `just xref-graph` which will generate a DOT file at `docs/` (if Graphviz is installed it will also generate an SVG visualisation using `dot`).
2022-03-24 16:10:48 +00:00
### Usage under Windows (WSL, MSYS or CYGWIN)
2021-05-22 16:01:58 +00:00
2022-05-23 02:20:39 +00:00
By default, the `justfile` requires symlinks, which can be enabled with the help of [this link](https://stackoverflow.com/a/59761201).
2022-03-24 16:10:48 +00:00
See the [pull request adding WSL support](https://github.com/bonfire-networks/bonfire-app/pull/111) for details about usage without symlinks.
2021-05-22 16:01:58 +00:00
2022-05-23 02:20:39 +00:00
## `just` commands
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
Run `just` followed by any of these commands when appropriate rather than directly using the equivalent commands like `mix`, `docker`, `docker-compose`, etc. For example, `just setup` will get you started, and `just dev` will run the app.
2021-05-22 14:36:40 +00:00
2021-05-22 14:42:44 +00:00
You can first set an env variable to control which mode these commands will assume you're using. Here are your options:
2021-07-03 08:04:49 +00:00
- `WITH_DOCKER=total` : use docker for everything (default)
- `WITH_DOCKER=partial` : use docker for services like the DB
- `WITH_DOCKER=easy` : use docker for services like the DB & compiled utilities like messctl
- `WITH_DOCKER=no` : please no
2021-05-22 14:36:40 +00:00
2022-05-23 02:20:39 +00:00
Run `just help` to see the list of possible commands and what they do.
2021-05-22 14:36:40 +00:00
## Troubleshooting
### EACCES Permissions Error
If you get a permissions error when following any of the steps, run the following command and it should be fixed:
```shell
cd bonfire && sudo chown -R yourusername:yourusername .
```
Note that the command should be modified so your shell is pointing to wherever you have bonfire installed. If you are already in the bonfire directory then you only need to worry about running the `chown` portion of the command.
### Unable to access Postgres database
If you are getting any `:nxdomain` errors, check if you have any firewalls that may be blocking the port on your system.
For example, if you are running UFW (a lot of Linux distros do), run the following command to allow access to port 4000:
```shell
sudo ufw allow 4000
```
2021-05-22 14:36:40 +00:00
### (Mix) Package fetch failed
Example:
```
** (Mix) Package fetch failed and no cached copy available (https://repo.hex.pm/tarballs/distillery-2.0.12.tar)
```
In this case, distillery (as an example of a dependency) made a new release and retired the old release from hex. The new version (`2.0.14`) is quite close to the version we were depending on (`2.0.12`), so we chose to upgrade:
```shell
mix deps.update distillery
```
This respects the version bounds in `mix.exs` (`~> 2.0`), so increment that if required.
### `(DBConnection.ConnectionError) tcp recv: closed`
Example:
```
** (DBConnection.ConnectionError) tcp recv: closed (the connection was closed by the pool, possibly due to a timeout or because the pool has been terminated)
```
In this case, the seeds were unable to complete because a query took too long to execute on your machine. You can configure the timeout to be larger in the `dev` environment:
1. Open `config/dev.exs` in your editor.
2. Find the database configuration (search for `Bonfire.Common.Repo`).
2021-05-22 14:36:40 +00:00
3. Add `timeout: 60_000` to the list of options:
```
config :bonfire, Bonfire.Common.Repo,
2021-05-22 14:36:40 +00:00
timeout: 60_000,
[...]
```