Using nix and nushell for reproducible scripts

So in ham radio, we have the concept of QSL cards. These are postcards that we physically mail to each other in order to confirm we've made contact. This practice has mostly moved to electronic means of doing the same thing. This can be done via a website like Logbook of the World or qrz.com.

There are also folks that send what are called eqsl cards via email. A while back I received one of these that was obviously generated from software. I didn't know how they did it, so I hacked together a Python script to combine a templated SVG file and a ham log to render one new SVG file per contact. I then used Inkscape's CLI to render those SVGs into PNGs so that I could email them out. It was a fun little hack.

Given that I'm on a Nushell kick, I wondered what it would take to do the same with a Nu script.

For the Python script, I used adif-multitool with the sh package to render the commonly used log format, adif, into JSON. That JSON data wad then used by chevon to render the SVG files.

I can easily call adif-multitool with Nu to convert an adif file into a Nu record. I did that in my post about using adifmt with Nu.

There was one challenging bit. There is no nu module for rending mustache templates. So I would have to find a CLI tool for this. I searched nixpkgs for a CLI to render mustache templates and sure enough there was one, mustache-go.

Cool. Easy enough. I can whip together a nu command that will pass a nu record into the mustache-go command and output the rendered template.

That's all well and good, but in order to use this script, I need to make sure that both adifmt and mustache-go are installed.

I could do that with a flake and a dev shell, but I remembered that I used nix once to provide a bash script with the script's dependencies. Could I do that with Nu as well?

Sure enough, I can:

#!/usr/bin/env nix-shell
#! nix-shell -i nu --pure
#! nix-shell -p mustache-go nushell adif-multitool
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/8c50a710ddca43d7a530fb805ad55bde8d0141c5.tar.gz

Adding that to the top of my script gives my script all the dependencies it needs, and they're pinned to the version I used when writing the script. I love how Nix is basically the universal package manager. No messing around with apt-get, virtualenv, npm, or whatever. (Granted, this script now needs Internet access if the packages are not installed to run...)

If you're curious what the resulting nu script looks like I uploaded it here: esql.nu. It was just more of a preoccupation with whether I could do it with nu than if I should.