Mac + Nix + home_manager Love Story

I had been eyeing Nix for a while, intrigued by the possibility to deterministically define dependencies to projects et cetera. Then, at the end of 2021 I found out about home-manager, which is a way to manage your standard development environment setup with Nix – It's like any dotfiles repo you've ever seen, but better.

Installation

First, we install Nix to accommodate for home-manager (I recommend everyone to read all the output and not just blindly answer* yes *to every choice):

sh <(curl -L https://nixos.org/nix/install)

Then, after that’s done with, we can install home-manager. Restart your shell and follow the standalone installation steps at https://nix-community.github.io/home-manager/index.html#sec-install-standalone.

Configuration

If you're not familiar with Nix, making your custom home-manager configuration will be quite the hurdle. I recommend looking at existing configuration files and poking around them to make them your own, or at least that's what I did. As the creator of home-manager himself says, the error messages given out by home-manager & Nix when the configuration is not working properly are going to be hard to decipher, and you will not get far by looking at them, let alone googling the outputs, so be warned.

Problems with OS updates

In my experience, Nix and thus home-manager breaks every time my Mac has a larger update. This probably has something to do with permissions and/or the nix-store partition which gets made on a Mac when you install Nix. Fortunately, fixing this minor annoyance is usually pretty straight forward: just reinstall Nix and follow the installation instructions. Like magic, home-manager is fixed too and your development environment is as it should.

Lately, this method has not been working right away, but have required some repeated wiggling from me to get it right; I'm sharing a small script that should remove your worries (at least if you're on an (Apple Silicon) Mac, that is.):

# Script ensuring nix installation
if nix --version ; then
    echo "Nix installation found"
else
		# Nix installation backs shell configs to these files,
		# remove existing backups before proceeding
    sudo rm /etc/bashrc.backup-before-nix
    sudo rm /etc/bash.bashrc.backup-before-nix
    sudo rm /etc/zshrc.backup-before-nix

		# Run Nix installation answering y to all questions.
		# Might require additional permissions from the OS
    yes | sh <(curl -L https://nixos.org/nix/install)
fi

If you add that to your home-manager’s shell config, nix will automatically reinstall whenever the installation breaks. (After asking the sudo password, that is) It’s a good idea to run home-manager’s switch command after this to get the latest packages.

The configuration files this blog post is based on reside here:

Subscribe to jantto
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.