# Developing Akri on macOS with Raspberry Pi and Mutagen


[Akri](https://www.cncf.io/projects/akri/), a CNCF sandbox project, makes it easier to expose edge devices to Kubernetes clusters.
However, if you're developing Akri on macOS, you’ll quickly hit a roadblock:
**Akri does not build on macOS** (to the date of writing this blog) due to its Linux-only dependencies.

<!--more-->

To solve this, we’ll set up a **remote development workflow** using [Mutagen](https://mutagen.io),
a fast and reliable tool for file synchronization. You'll write code on your Mac using Neovim
(or any editor of your choice), and the code will automatically sync to a Raspberry Pi,
where it can be built and tested natively.

<br>

## What You’ll Achieve?

- Write code on macOS.
- Have it automatically synced to a Raspberry Pi.
- Build Akri using `cargo build` **on the Pi**, with updated dependencies and `Cargo.lock` synced in real time.

<br>

## Prerequisites

- A Raspberry Pi (running a Debian-based Linux distro).
- Rust installed on the Pi (check [Rust docs](https://www.rust-lang.org/tools/install))
- SSH access to your Pi.
- [`mutagen`](https://mutagen.io/documentation/introduction/installation) installed on your Mac.
- [Akri repo](https://github.com/project-akri/akri) cloned on macOS.

<br>

## Step 1: Set Up SSH Access (Passwordless)

We'll set up SSH key-based login so that `mutagen` can sync files without being prompted for a password.

### Generate an SSH Key (if you don't have one)

```sh
ssh-keygen -t ed25519
```

This command generates a new SSH key pair. You can press Enter to accept the default file location and leave the passphrase empty for passwordless access.

<br>

### Copy the SSH public key to your Pi

The command below copies your public key to the Raspberry Pi, allowing passwordless SSH access.

```sh
ssh-copy-id -i ~/.ssh/id_ed25519.pub pi@<RASPBERRY_PI_IP>
```

Replace <RASPBERRY_PI_IP> with your Pi’s IP address.

<br>

### Add a host entry

To simplify SSH access, add an entry to your `~/.ssh/config` file:

```sh
Host akri-pi
    HostName <RASPBERRY_PI_IP>
    User gaurav
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
```

This allows you to SSH into your Pi using the alias `akri-pi`:

```sh
ssh akri-pi
```

<br>

## Step 2: Clone the Akri Repository

If you haven't already, clone the Akri repository to your macOS machine:

```sh
git clone https://github.com/project-akri/akri.git
cd akri
```

<br>

## Step 3: Configure Mutagen

Create a Mutagen configuration file to sync your Akri code to the Raspberry Pi. Create a file named `mutagen.yml` in the root of your Akri repository with the following content:

```yaml
sync:
  defaults:
    flushOnCreate: true
    ignore:
      vcs: true
  code:
    alpha: "."
    beta: "akri-pi:/home/gaurav/workspace/akri"
    ignore:
      paths:
        - "target"
        - ".github"
```

This configuration tells Mutagen to sync the current directory (your Akri code) to the specified path on your Raspberry Pi. It ignores the `target` directory and `.github` folder, which are not needed for development.

Also, make sure that the directory desired directroy, like `/home/gaurav/workspace/akri`,
exists on your Raspberry Pi. You can create it using:

```sh
ssh akri-pi "mkdir -p /home/gaurav/workspace/akri"
```

<br>

## Step 4: Start Mutagen Sync

Initialize the project with Mutagen:

```sh
mutagen project start
```

This command will start the synchronization process. Mutagen will analyze the directories and begin syncing files from your macOS machine to the Raspberry Pi.

You can check the status of the sync with:

```sh
mutagen sync list
```

If everything is set up correctly, you should see your Akri code being synced to the Raspberry Pi. Here’s an example output:

```sh
Name: code
Identifier: sync_bNo8KQzBAmTwoBGrVZbyaaquMzNcGX8Mdgmq7NlOnEp
Alpha:
        URL: /Users/gaurav.gahlot/workspace/gauravgahlot/akri
        Connected: Yes
        Synchronizable contents:
                73 directories
                239 files (1.6 MB)
                0 symbolic links
Beta:
        URL: akri-pi:/home/gaurav/workspace/akri
        Connected: Yes
        Synchronizable contents:
                73 directories
                239 files (1.6 MB)
                0 symbolic links
Status: Watching for changes
```

<br>

## Step 5: Build Akri on the Raspberry Pi (from macOS)

Now that your code is synced, you can build Akri directly on the Raspberry Pi without needing to SSH into it every time:

```sh
ssh akri-pi 'bash -l -c "cd /home/gaurav/workspace/akri && cargo build"'
```

If everything is set up correctly, you should see the build process start, and it will use the latest code synced from your macOS machine.

<br>

## Troubleshooting

### `cargo` Not Found When Using SSH?

If you face an error like:

```sh
bash: line 1: cargo: command not found
```

It means that the `cargo` command is not in the default PATH for non-interactive shells. To fix this, ensure that the Rust environment is set up correctly in your `.bashrc` or `.bash_profile` on the Raspberry Pi. You can add the following line to your `.bashrc`:

```sh
. "$HOME/.cargo/env"
```

<br>

### target/ is still syncing?

If you notice that the `target/` directory is still being synced, it might be because Mutagen has already started syncing it before you added it to the ignore rules.

Mutagen remembers previously synced files, so changing ignore rules won’t apply retroactively. To fully reset:

```sh
mutagen sync terminate akri-sync
mutagen project terminate akri-dev
mutagen sync flush
```

Then delete the residual directory on the Pi:

```sh
ssh akri-pi "rm -rf /home/gaurav/workspace/akri/target"
```

Now, restart the sync:

```sh
mutagen project start
```

<br>

## Conclusion

Akri is a CNCF sandbox project aimed at enabling edge devices in Kubernetes. While it’s targeted at Linux-based environments, the developer ecosystem often includes macOS machines.

This setup allows you to develop Akri on macOS while leveraging the Raspberry Pi for building and testing. By using Mutagen, you can ensure that your code is always in sync with the Pi, enabling a smooth development workflow without needing to switch environments or worry about cross-compilation issues.

Feel free to reach out if you have any questions or run into issues while setting up your development environment. Happy hacking!

