adrianhesketh.com

Running DynamoDB Local with nix

I’ve been using Nix as a package manager for a while. Rather than installing packages globally like brew, the idea with Nix is to have a minimal set of base packages, then to start shell sessions containing just the tools you need. This reduces the amount of cruft just sitting around on your machine.

I wanted to run a local DynamoDB for testing, but you need to have Java installed, and, of course, I don’t have it installed by default.

The alternatives the documentation lists (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html) are to use Maven (a Java package manager) or Docker but let’s face it, if I don’t like having Java installed, I’m not going to like running Docker.

I don’t like it because on the Mac, it’s a waste of CPU/RAM, because it runs a Virtual Machine and a daemon. You’re probably wondering why I use a Mac at all, and it’s something I wonder myself until I remember when I had a Linux laptop.

Fortunately, I can start a shell with Nix that just contains my base packages, plus Java to run DynamoDB instead.

nix-shell --packages jdk11

So that’s fine, but I also need the DynamoDBLocal.jar file, and I want to make sure that I keep the data in my local table between runs.

Well, that’s easy enough to pull together into a single script:

#!/bin/sh
if ! [ -f ./DynamoDBLocal.jar ]; then
  curl https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/dynamodb_local_latest.zip -o d.zip
  unzip d.zip
  rm d.zip
fi
mkdir -p data
nix-shell --packages jdk11 --run "java -jar DynamoDBLocal.jar -sharedDb -dbPath ./data"

Now, I can run zsh ./dynamodb.sh and I’ve got it running, but when I exit, I’ve not got Java installed.