← All posts

The gateway drug to Nix

by Tomasz Kowal

nix

The case of PlantUML

Have you ever tried to install something and failed multiple times?

I needed to generate some diagrams using plantuml. When installing such things, I always start with apt:

sudo apt-get install plantuml

And it looked like everything was fine, but I got errors when generating the diagrams. Some unrecognised syntax. I was positive that my colleagues used the same source to create other diagrams I’d already seen, so I checked the version.

plantuml -version
PlantUML version 1.2018.13 (Mon Nov 26 18:11:51 CET 2018)

Oh! That is it. It is three years old! Let’s grab one from the official website. There are eleven different versions to download! After some digging, I went with the MIT version.

java -jar plantuml.jar sequenceDiagram.txt

And it failed because my java version is too new.

And then I realised! I have this new magical piece of software called Nix on my PC!

Nix might feel complicated because it has many facades: NixOS, Nix package manager and the website even mentions cloud and continues integration. The Learn section on Nix website starts immediately with setting up a *.nix file. There is, however, one even simpler thing you can try.

nix-shell -p plantuml

This command starts a new shell with plantuml ready to use. The first time it runs, it will fetch all necessary dependencies and store them. The program is prepared to use when Nix finishes. This time plantuml is much more recent.

plantuml -version                                                                                                                                                    
PlantUML version 1.2021.16 (Wed Dec 08 18:25:22 CET 2021)

No knowledge of how Nix works is required. It just works! I’ve escaped the dependency hell.

It is awesome!

The case of Emacs

I wanted to use Emacs 27.2 because it has some nice optimisations making some packages work faster. However, apt-get had only version 27.1.

I’ve found an article that recommends custom PPA or flatpak to install 27.2. I tried PPA first, and it worked great until the first system update. Some dependencies in the PPA conflicted with standard ones and prevented automatic updates. I don’t even remember the details. I figured I’d use the old version instead of fighting with dependencies on every update.

There was also a second solution using flatpak in the article. The installation was painless, but the editor was unusable. Flatpak runs its applications in contained environments. It is fantastic from a security perspective, but it can only read and write to specific places in the file system by default. And I expect my editor to open every file in the system. Maybe it would be possible to configure, but I wanted something working now.

And then I tried Nix:

nix-shell -p emacs

And it just worked! Emacs 27.2 GUI was running. I wanted to close the shell after launching emacs, so I made a three-line script:

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p emacs
emacs &
disown
exit

Notice two lines of shebang. Usually, it tells the script to use bash as an interpreter, but I want this script to run in nix-shell called with -p emacs. I start Emacs GUI in the background and call disown so closing the shell does not kill the Emacs GUI and exit the nix-shell. It is my current way of running GUI tools with nix.

Summary

Next time, when you’d like to try out some new tool without installing it globally - try nix. You won’t regret it!


Did you like it? Follow me on Mastodon or Twitter (for as long as it lasts)