nix-darwin

Using nix-darwin on MacOS

Why Nix?

A full rundown on NixOS can be found at how nix works, but the key feature that sparked my interest were the following:

Declarative System Configuration Model

In NixOS, the entire operating system — the kernel, applications, system packages, configuration files, and so on — is built by the Nix package manager from a description in a purely functional build language. The fact that it’s purely functional essentially means that building a new configuration cannot overwrite previous configurations. Most of the other features follow from this.

The upshot of this is that it should provide a repeatable and consistent environment. As I learned more and realized that this include configuration and services, the more interested I became. Unlike Puppet which is a bolt on system that tries to do this, NixOS has it baked into the OS and package manager. The thought of a system that had these qualities was too much to ignore.

But why Nix on your mac?

A few reasons lead me to try Nix on my Mac:

  • I like the idea of nix-shell and being able to try a new piece of software without changing my underlying system.
  • I like the way that Nix handles installing software and managing the system, much better than something like brew.
  • I wanted to learn Nix, and I wasn’t going to dual boot my MacBook Air. This let’s me start to learn Nix without having to commit the whole system.

Enter nix-darwin

nix-darwin is an effort to bring the nix declarative system configuration model to the mac. Not only declaring packages, but also system settings and services can be defined. My hope is that this lets me have a way to backup my installed packages and daemons, and their configurations, and not just my user files or having to hope that Time Machine gets the right things.

Installing Nix

I won’t run through the entire nix install process here, instead please read the install documentation provided by the project. I followed the directions there and they worked fine.

💡Even if you don’t want to install nix-darwin, you may want to continue to the section further down for “Adding a channel”. There’s more information about how to get updated package versions and the mistakes I made.

Installing nix-darwin

I followed the documentation in the project repository, and have pulled out the key pieces here.

Initial install

➜  ~ nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer
➜  ~ ./result/bin/darwin-installer

Both commands need to be run - the first command brings in the package for nix-darwin, and the second does the actual install.

Updating nix-darwin

➜  ~ nix-channel --update darwin
➜  ~ darwin-rebuild changelog

The above commands will update specifically nix-darwin.

Adding a channel

When I first installed Nix on my Mac, it was not obvious to me that I had to add a channel to track. I could already install packages, after all. On linux distributions I’d used in the past, if I didn’t have a repository available I’d have nothing. @jakehamilton@hachyderm.io provided a hint:

you want to update your nixpkgs channel, not the Darwin channel.

Aha! If I need to update something other than the Darwin channel, and I don’t already have it, I must need to add it.

💡What channel to use?

The NixOS Wiki has more information on channels. In short, these are the major differences:

Stable channels (nixos-23.11) provide conservative updates for fixing bugs and security vulnerabilities, but do not receive major updates after initial release. New stable channels are released every six months.

Unstable channels (nixos-unstable, nixpkgs-unstable) correspond to the main development branch (unstable) of Nixpkgs, delivering the latest tested updates on a rolling basis.

Adding a channel was simple:

➜  ~ nix-channel --add https://nixos.org/channels/channel-name nixos
➜  ~ nix-channel --update
➜  ~ nix-channel --list 

On a fresh install of nix-darwin, I see the following output after adding nixos-unstable:

➜  ~ nix-channel --add https://nixos.org/channels/nixpkgs-unstable
➜  ~ nix-channel --update
unpacking channels...
➜  ~ nix-channel --list
darwin https://github.com/LnL7/nix-darwin/archive/master.tar.gz
nixpkgs https://nixos.org/channels/nixpkgs-unstable

For me, unstable is fine. I should be able to change the channel I am on and rebuild the system.

Modifying the system

Now that nix-darwin is installed, how do we change the system?

Configuration is stored in ~/.nixpkgs/darwin-configuration.nix There are examples in the nix-darwin repository. Once the configuration file has been modified, run darwin-rebuild switch to re-evaluate the configuration and bring the system into that state.

What’s next?

The next things I want to work with are flakes and managing configuration with Nix.