2 minutes
nh - yet another nix helper
When using NixOS you’re going to inevitably come to a point where the built-in nix commands are a PITA to type out. You got to cd into your config directory if it’s not in /etc/nixos/
and run sudo nixos-rebuild switch
a bunch of times. Especially if you’re debugging something that is just incredibly annoying. If you’re anything like me you probably wrote some shell scripts somewhere that automate that process.
But what if I told you there was another way…
Meet nh, a incredibly simple command line tool to give you shorthand commands for lots of common nix tasks.
Want to rebuild your system? Run nh os switch
!
Want to garbage collect your generations and the nix store? Run nh clean all
.
It’s really that simple. Especially when you configure it properly.
To install you just the following to your config:
programs.nh.enable = true;
There are, of course, configuration options to tweak it to your liking. For example, my complete flake-based nixos config is at the path /home/nic/Projects/nixos-config
. I can provide nh with that path using the programs.nh.flake
attribute to let it know that, if I want to rebuild my system, it needs to look there for my config. There’s a plethora of commands and other config options that you can dig through by reading nix source code of the package or running nh -h
.
If you just want to copy paste something to get going, I present to you my config using my ‘sensible defaults’®️.
programs.nh = {
enable = true;
clean.enable = true;
clean.extraArgs = "--keep-since 4d --keep 3";
flake = "/home/nic/Projects/nixos-config";
};
nh
is a incredible little tool that saves me so much time when changing something about my system and I absolutely recommend it for everyone that rebuilds their system on a regular basis.
One small pitfall that I encountered is, that the flake.lock file generated by the ‘vanilla’ nixos-rebuild switch
command is owned by root (since you need to invoke it using sudo). nh
only prompts for root access when it needs it. Changing the lock file is normally not one of those things. So when you run it, chances are that it fails because it can’t write to the lock file. To fix that just chown that file to belong to your user and you’re good to go.
Thanks for reading ❄️