oliverdavies.uk/source/_daily_emails/2024-08-20.md
2024-09-02 23:15:55 +01:00

33 lines
1.7 KiB
Markdown

---
title: Writing bash scripts with Nix
date: 2024-08-20
permalink: daily/2024/08/20/writing-bash-scripts-with-nix
tags:
- software-development
- linux
- nix
cta: ~
snippet: |
Dependency injection in bash scripts? (Nix mentioned)
---
Yesterday, I wrote about [a bash script I've written][0] to export a list of videos on my external hard drive.
If you [looked at the script on GitHub][1] 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][2] - 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.
[0]: {{site.url}}/daily/2024/08/19/bash-scripting-for-fun-and-profit
[1]: https://github.com/opdavies/dotfiles.nix/blob/a1ef2d1402c9c607e7a3e4427ce125d0cabeddcd/lib/shared/scripts/export-video-list.nix#L12-L31
[2]: {{site.url}}/archive/2022/09/26/experimenting-with-the-nix-package-manager