Chapter 4 - Helpful Utilities
Real quick we’re going to install and setup some handy utilities that can help with Nix/NixOS.
In this chapter we will…
- Install
nom
in our config - Install
just
and get a basicjustfile
setup - Learn and install
comma
Nix Output Monitor
As your VM builds you might notice not much output is given. That’s because Nix only shows the current line out stdout when building.
Nix output monitor (nom
) is a community project that renders a nice-looking tree of all packages being built and shows the stdout of the current build process(es) above it.
Adding nix-output-monitor to our NixOS config is as simple as adding it to environment.systemPackages
.
From there whenever we use nix build
we can replace the nix
with nom
and it will build with better output.
To install nom
on your host system we can make use of nix profiles. These are generally frowned upon when being used in NixOS system but for your host system it’s a nice feature.
Just
Just is a very simple command-line runner that we can use to make project-specific shortcuts. All commands are stored in a justfile
which will be read from the CWD when running just
.
Installing Just
Similar to nix-output-monitor
, add just
to your environment.systemPackages
.
And install it on your host distro with nix profiles.
Using Just
Here’s the justfile
I recommend for your config. Replace YOURNAME
with the name of your system config.
- flake.nix
- flake.lock
- justfile Notice no file extension
- …
After installing, try running just vm
. The VM should build and run with nice output!
Comma
Comma is one of the most useful tools I’ve ever used. Sadly it’s a bit more complicated to install on non-NixOS system so I won’t get into how to do it here (all the more motivation to finish this tutorial I suppose).
Comma is a way to run a command that you don’t have installed. Remember how we ran nix run nixpkgs#python3
in chapter 1? It never installed python3
, merely copied it to our nix store and executed the binary in that store path.
That may not make total sense yet, next chapter we’ll get into how the Nix store works. For now what you need to know is that comma goes a step further, you pass it a command and it will find the package(s) that command belongs to, copy the package to your store, and execute it on-the-spot.
We’ll get into how to use it after installation.
Adding Comma
Adding Comma is not as simple as just
or nom
.
- We’ll need a new
flake.nix
input fornix-index-database
. This should point the the urlgithub:nix-community/nix-index-database
, and thenixpkgs
input should follow ournixpkgs
(we went over adding inputs in the last chapter with HM). Also don’t forget to add it as an argument tooutputs
. - Now, we’re going to add an overlay to our nixpkgs instance from
nix-index-database
.
An overlay is another kind of flake output that edits nixpkgs in some way, this could be adding packages (what it’ll do in our case), or it could even edit existing packages.
To add an overlay, below theinherit system;
line when weimport
nixpkgs. We’ll add a new array calledoverlays
, then add the itemnix-index-database.overlays.nix-index
. This will add the packages we want into our instance ofnixpkgs
. - Now all we need to do is add the
comma-with-db
package toenvironment.systemPackages
.
Using Comma
To test comma, start up your VM and try to run cowsay
.
Wait… what?? We don’t have cowsay installed? That’s wild. But we need to cowsay!!
Comma to the rescue! To automatically grab cowsay
from nixpkgs
on command name alone and fix this crucial issue, simply prepend a comma (,
) to the start of the command.
A prompt should come up to select a package, I’d recommend neo-cowsay
as the normal one has to download all of Perl.
Houston, we have cow! With comma you have access to any command in nixpkgs with no need to lookup which package it’s in.
Try it out with some commands! I recommend cowsay
, sl
, toilet
, lolcat
, and of course, neofetch
.
The command’s package will be cached in your nix store until you garbage collect it. What’s a nix store? What’s garbage collection? I’m glad you asked! In the next chapter we’ll go into the how and why of Nix and NixOS. And why a tool like comma can so easily exist.