Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Last active December 3, 2022 21:15
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save akiatoji/3a8c04ccba04a15551fe to your computer and use it in GitHub Desktop.
Save akiatoji/3a8c04ccba04a15551fe to your computer and use it in GitHub Desktop.
Running Clojure on Raspberry Pi with OS X

Clojure on Raspberry Pi with OS X

"Clojure running on Raspberry Pi" sounded so cool that I just had to give it a try.

Install JDK

  • Download ARM JDK from Oracle and instlal on Raspberry Pi
  • Change visudo to contain the following
Defaults        env_keep +="JAVA_TOOL_OPTIONS LEIN_ROOT"
Defaults        mail_badpass
Defaults        secure_path="/usr/lib/jvm/jdk-7-oracle-armhf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Install Leiningen

Create ~/bin if not there, log off, and log back in.

Create clojure app.

Let's see, what would be a one of a kind app that noone would think of.

Oh, I know I know, an adserver written in Clojure running on $35 Raspberry Pi.

pi@raspberrypi ~/ $  lein new app piadserver

Development Support

You want to run the code on Raspberry Pi, but you don't want to have to do the development itself on Pi. It'd be ideal if you can just edit the files remotely, and run repl remotely.

We can do all that.

Install avahi and netatalk

Avahi is open source replacement of Bonjour, and netatalk is AFP (AppleTalk over network). By installing these two packages, your raspberry pi will magically appear in your Finder sidebar, and you can connect (be sure to use valid user/password on RaspberryPi) browse, and open files on your Pi.

sudo apt-get install avahi-daemon
sudo apt-get install netatalk

Now you can edit your app in your favorite IDE. I use IntelliJ IDEA with La Clojure, although this setup is known to hang at startup, so I also frequently use Eclipse.

Run headless REPL

You can login via ssh and run REPL, but for some reason Pi's network latency is awful. I get ping times in 100's of mS range even on directly connected ethernet, and trying to work via remote login is reminescent of 2400bps serial terminal.

Fortunately, you can run a headless REPL on Pi and connect to it from REPL shell running locally on your Mac

On Pi

Start REPL in headless mode. Note that :host argument means which network interfaces to listen to, so you can enter your Pi's IP address here.

I use Wifi dongle on the Pi so it has two network interfaces, and I'm too lazy to look up Pi's IP address everytime. So I use 0.0.0.0 which means listen on all interfaces (IP addresses) on the Pi.

If you want to just keep the headless REPL running, you can run it in tmux session (install tmux package first, of course). I recently found out that you can make screen do the same.

lein repl :headless :host 0.0.0.0 :port 9000

Also, starting REPL can take a couple of minutes on Pi. You may need to add the following to your project.clj

:repl-options {:timeout 300000}

On Mac

From the mounted project directory, run the following

lein repl :connect xx.xx.x.xx:9000/repl

Now you are connected to remote REPL running on Pi.

From IntelliJ IDEA

Although slightly old, there is a great writeup at IntelliJ IDEA ate my nREPL for maven based setup.

You don't really have to go through all that gyration if you use Cursive plugin on IntelliJ IDEA 13.1. Remote REPL 'just works'.

See Cursive site for remote REPL settings

@JonathanGTH
Copy link

Very nice

@dkmn
Copy link

dkmn commented Mar 29, 2020

Indeed. This is great and very concise! Thank you.

Tip for those new to netatalk:
You'll need to edit the netatalk config file via
sudo nano /etc/netatalk/afp.conf
or similar to actually expose your files to AFP browsing or viewing.

E.g.
[Homes] basedir regex = /home
to let all users log in and see their home directories, or another AFP volume via that parameter:
[My AFP Share Name] path = /Volumes/UsefulDrive

I found this helpful:
https://pimylifeup.com/raspberry-pi-afp/

Using avahi just allows use of Bonjour to not need manual use of IP addresses of DHCP mapping, etc.
HTH,
DK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment