Ever wanted a way to quickly & temporarily install an amusing command-line tool that you read about on the interwebz?

I certainly needed something like that since forever. Every now and then I gotta compress a JPEG using imagemagick, format a markdown file using prettier, or demonstrate to my colleagues that [,,,].length is a valid JavaScript expression, while not having Node.js installed on my machine.

Second best thing I had was Homebrew. I’d brew install the tool, take it for a spin, and hopefully remember to brew uninstall it afterwards - or else it’ll not only bloat my system, but also slow down brew upgrades even more for the forseable future until I factory-reset my machine next Spring cleaning.

Enter Nix, the most up-to-date package manager in existence 1.

Say we’d like to convert books.json to YAML:

[
  {
    "title": "On Palestine",
    "authors": ["Noam Chomsky", "Ilan Pappé", "Frank Barat"],
    "subjects": ["Zionism", "Diplomatic relations", "Arab-Israeli conflict"]
  }
]

Let’s spawn a temporary shell with yq available at our disposal using some Nix goodness:

➜  ~ nix-shell -p yq-go
this path will be fetched (3.12 MiB download, 10.63 MiB unpacked):
  /nix/store/ki8im82wf5xmha76nz4a1ir7qpblk3a5-yq-go-4.44.6
copying path '/nix/store/ki8im82wf5xmha76nz4a1ir7qpblk3a5-yq-go-4.44.6' from 'https://cache.nixos.org'...

[nix-shell:~]$ which yq
/nix/store/ki8im82wf5xmha76nz4a1ir7qpblk3a5-yq-go-4.44.6/bin/yq

[nix-shell:~]$ cat books.json | yq -p json > books.yaml

which produces books.yaml:

- title: On Palestine
  authors:
    - Noam Chomsky
    - Ilan Pappé
    - Frank Barat
  subjects:
    - Zionism
    - Diplomatic relations
    - Arab-Israeli conflict

Once we exit our ephemeral Nix shell by hitting Ctrl + D, yq ceases to exist on our system 2:

➜  ~ which yq
yq not found

Awesome, right? It doesn’t end here. We have the ability to conjure more than one tool together to do something even cooler:

➜  ~ nix-shell -p fortune cowsay
these 3 paths will be fetched (1.22 MiB download, 4.70 MiB unpacked):
  /nix/store/i7mkmqlh50av8i392m87qx2pqizsx91d-cowsay-3.8.4
  /nix/store/7is49l011gicz47qdq8zlg742bqcdn5a-fortune-mod-3.22.0
  /nix/store/8k3987cmp2bbdj16mw0fyplcsg7h3a46-recode-3.7.14
copying path '/nix/store/i7mkmqlh50av8i392m87qx2pqizsx91d-cowsay-3.8.4' from 'https://cache.nixos.org'...
copying path '/nix/store/8k3987cmp2bbdj16mw0fyplcsg7h3a46-recode-3.7.14' from 'https://cache.nixos.org'...
copying path '/nix/store/7is49l011gicz47qdq8zlg742bqcdn5a-fortune-mod-3.22.0' from 'https://cache.nixos.org'...

[nix-shell:~]$ fortune -s | cowsay
 _________________________________________
/ The great merit of society is to make   \
| one appreciate solitude.                |
|                                         |
| -- Charles Chincholles, "Reflections on |
\ the Art of Life"                        /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

You could also craft one-liners to seamlessly terminal-bomb your workmate whom you’re gonna preach Nix to later today:

➜  ~ nix-shell -p asciiquarium --command 'asciiquarium'

Enjoy Nix!



  1. https://repology.org/repositories/statistics/newest ↩︎

  2. It’s technically still kicking around in the Nix store (at /nix/store/ki8im82wf5xmha76nz4a1ir7qpblk3a5-yq-go-4.44.6/bin/yq), in case we need it again in the near future, but it will be garbage-collected later by Nix. ↩︎