Skip to content

Instantly share code, notes, and snippets.

@bbarker
Last active June 6, 2020 12:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbarker/7f6b831518e53dacb669febe223d0eae to your computer and use it in GitHub Desktop.
Save bbarker/7f6b831518e53dacb669febe223d0eae to your computer and use it in GitHub Desktop.
Building or Installing PureScript on the Raspberry Pi 4 with Raspbian and the Nix package manager

Prerequisites

  • A Raspberry Pi 4 with Raspbian 10
  • At least a 32GB SD card and probably at least 10GB of free space available.

Overview

Increase swap space, switch to a 64-bit kernel to use Nix aarch64 packages, and use Nix packages where possible. At the time of this writing it was only necessary to build PureScript (purs) itself; you can check Hydra to see if this is still an issue, replacing "20.03" in the URL below with the release of nixpkgs you are interested in:

https://hydra.nixos.org/job/nixos/release-20.03-aarch64/nixpkgs.haskellPackages.purescript.aarch64-linux

Configuring Raspbian

Switch Kernel

We'll need a 64 bit kernel for Nix. The kernel is probalby not as well vetted and stable as the standard 32bit kernel, but nothing terrible has happened for me so far. You can always switch between the two fairly easily if needed.

As a bonus, Nix will allow you to use many other (64 bit) packages on Raspbian, which currently only has a 32 bit userland. I used instructions from here, but the relevant steps should be:

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ ls /boot/kernel*.img # confirm you see "kernel8.img" listed
$ echo "arm_64bit=1" | sudo tee -a /boot/config.txt
$ sudo systemctl reboot

After you reboot, you can confirm you see "aarch64" in the output of uname -a:

$ uname -a
Linux raspberrypi 4.19.114-v8+ #1303 SMP PREEMPT Tue Apr 7 15:55:30 BST 2020 aarch64 GNU/Linux

Increasing Swap

For this section and for building PureScript, I relied heavily on another guide.

Building Haskell (and perhaps, PureScript) programs can be memory intensive. Our first mitigating step is to enable and/or increase swap.

Stop swapping:

sudo /etc/init.d/dphys-swapfile stop

Edit the file /etc/dphys-swapfile to change the size of the swap area by updating the line with CONF_SWAPSIZE to:

CONF_SWAPSIZE=4096

(As noted by the author:)

This should give you 4GB of swap space, but it appears you get a maximum of 2GB. That’s sufficient, so let’s go with it.

Restart swapping:

sudo /etc/init.d/dphys-swapfile start

Installing Nix

The standard install method should work, just go to https://nixos.org/download.html and follow the instructions there and in the terminal.

Once installed, we need to configure nix-channels to use a stable version of nixpkgs rather than unstable, primarily so that we can rely on the nix build cache and not have to build the universe on the Raspberry Pi 4 (trust me you don't want to).

Check your current channel:

$ nix-channel --list

If listed as unstable, change it to the latest release, e.g.:

$ nix-channel --add https://nixos.org/channels/nixos-20.03 nixpkgs
$ nix-channel --list

You should see just the above chanenl now if you took care to use the same alias as existed for the unstable branch (nixpkgs in this case).

Now update the channel:

$ nix-channel --update

Building PureScript

Clone the PureScript repository and check out the desired release (e.g. git checkout v0.13.8).

From the repository root:

$ nix-shell -p stack
$ stack --nix setup
$ stack --nix install --flag purescript:RELEASE --jobs 1

This will take a while - I ran it overnight.

If all went well, you will want to add the installation location to your PATH. I also find it useful to initiate the nix environment by default. Taken together, you can add these two blocks to your $HOME/.profile (if not already added):

if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
export PATH

if [ -e $HOME/.nix-profile/etc/profile.d/nix.sh ]; then . $HOME/.nix-profile/etc/profile.d/nix.sh; fi # added by Nix installer

At this point, you may wish to reboot to make sure all shells have the updated environment.

Building a project

To build a project, load any build tools in a new nix shell

$ nix-shell -p spago yarn

You may need to edit the package.json to not install purescript, spago, etc, as we need to use the nix-installed aarch64 binaries.

Other References and Resources

  1. purescript-emo8 - An unique functional 2D game engine that can create games with only emoji. (Can be built and played on the RPi4).
  2. Haskell on Raspberry PI 4
@bbarker
Copy link
Author

bbarker commented May 26, 2020

As a side note, I found the firefox available in nixpkgs helpful, as there no longer seems to be a firefox package in Raspbian 10, and the Raspbian-included Chromium had some issues rendering the build products of purescript-emo8.

@opyapeus
Copy link

opyapeus commented May 27, 2020

I guess the rendering issues might have occurred because of some lacking of emoji fonts which is owned by the OS or browser.
Emo8 engine itself has no emoji font. It uses just Unicode text. So the apperances depends on the OS or browser.

Is it possible to solve this problem by installing some emoji fonts on the OS, referring to the link below?
You may have already tried it. I’m sorry I did not try it.

https://www.raspberrypi.org/forums/viewtopic.php?t=253484

@bbarker
Copy link
Author

bbarker commented May 28, 2020

@opyapeus Thanks, I think that took care of most (possibly all) font issues for Chromium!

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