Writing bash scripts with Nix

Yesterday, I wrote about a bash script I've written to export a list of videos on my external hard drive.

If you looked at the script on GitHub and you're familiar with bash scripting, you may have thought it looked a bit odd or missing some things.

That's because I wrote it with Nix - the tool I've been using for almost two years to manage my dotfiles and operating system configuration.

Using Nix for bash scripts has advantages, like automatically adding the shebang line, setting sensible defaults for error handling, and automatically running shellcheck when switching.

I think the main advantage, though, is it can inject any dependency packages into the script.

This script needs tree and jq, so they are dependencies of the script and the script can't run if they aren't installed.

But, I don't need to assume they are available. Nix can do it automatically and make any packages added as build inputs available.

Now, if anyone else wanted to use this script, or I wanted to share another script with colleagues or a client, they can use it without needing to install any dependencies separately and the script will continue to work in the future using its explicitly-added dependencies.

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.