Not Nixing Nix

Lossy Wet Memory: A Crux
Unlike an elephant, among other things, people can be forgetful. Further, unlike an elephant, people made the mistake that is computers. Computers can be used to remember things for people but perhaps that's only because we've yet to find a good elephant-memory interface.
That's only sort of a "tongue-in-cheek" joke given that there are companies like FinalSpark progressing towards wetware computing using "organoids" derived from human stem cells. Give this paper a read if you want to be intrigued and disturbed.
If you've done any development or work with computers that requires creating and maintaining an "environment" very quickly you might forget how the state of your current environment has come to be. If not congratulations on being a literate elephant. Otherwise, unless you are regularly rebuilding your environment, determining exactly how it came to be is tedious.
To fix the problem let's consider abstracting a layer and create a reproducible environment with Nix. Thus begins a foray with Nix.
Factors for Limitation
The machine we're working on is a Debian flavor distro on WSL. If you are following along on WSL be sure to enable systemd in your /etc/wsl.conf
[boot]
systemd=true
Installing Nix, specifically the package manager, and reading the docs are left as an exercise to the reader.
Further for the sake of this posts' brevity the "environment" for this exercise will simply be tmux.
Declarative Shell Configuration
Assuming you've got your Nix installation and daemon working the initial inclination to create our "environment" might be a declarative shell environment via a shell.nix
file containing...
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = {}; overlays = []; };
in
pkgs.mkShellNoCC {
packages = with pkgs; [
tmux
];
shellHook = ''tmux'';
}
In the same directory run nix-shell
and if all goes well it will spawn you into tmux session. The keen observer will notice that the "shell" presented in the tmux session is not a "nix-shell". This is good or bad depending on what the desired "environment" is.
Using shell.nix
in this way is beneficial if you would like a different environment for different directories or projects. You can modify the shell.nix
file to suit the project needs in many ways. If you proceed with declarative shells you might want to also setup automatic environment activation.
There are some flaws however with this method of reproducible environments. Namely what if two projects have essentially the same dependencies? It would be inefficient to instantiate a new shell for each. Also if we will be using tmux for everything we could consider that a user configuration and not necessarily a project level configuration. Specifying user configurations in each of the projects does not seem "appropriate." These considerations naturally lead to Nix Home Manager.
Management
"The future of all humanity comes down to one word... Management." - Bud Askins
Once you've installed home manager we'll be working with the config file in ~/.config/home-manager/home.nix
Add the pkgs.tmux
to the home.packages and add
programs.tmux = {
enable=true;
};
Run home-manager build
and if successful you will get no errors. Run home-manager switch
to activate your user environment.
If we wanted to install the tmux plugins, before Nix, we could download install and then update our tmux config files to source the config. However Nix community has packaged these plugins and can be installed by updating the home.nix file.
programs.tmux = {
enable=true;
plugins = with pkgs;
[
tmuxPlugins.better-mouse-mode
tmuxPlugins.dracula
];
};
In Closing
We explore a minimal tangent of the capabilities Nix provides by looking at 2 ways of configuring an "environment." The Nix ecosystem is fast evolving and the "home-manager" is not considered a stable feature. If you are considering migrating all your dotfiles to the purview of Nix it will take some time and effort and to an extent you will be sacrificing some stability unless you explicitly specify the versions of packages and programs you will use with Nix. After all elephants are migratory.