My starter macOS Nix Config
What I used on a new macOS machine to write and publish this post.
I understand what Nix is. But I barely know how to use it. Earlier this week, I shared that I was interested in trying it on my new macOS machine, after upgrading to the new M4 MacBook Pro.
Anyone on 𝕏 using Nix on macOS?
— Kristian Freeman (@kristianf_) November 4, 2024
I'm interested in switching to it from Homebrew (or at least, brew for GUI apps and nix for everything else) - any tips/best practices recommended!
Found this recent blog post which seems like a good start 👀https://t.co/Vxi7wm5fBy
It arrived earlier today, and I got started setting up Nix. I still don’t 100% feel comfortable in it, but I learned enough that I was able to get it up and running in about an hour. Here’s what I did to get it running.
Installation
This guide assumes a brand new macOS install. Skip steps as needed if you’re using an existing machine.
First, you need to install the built-in Command Line Tools:
This is macOS’ build-essential
-equivalent. A bunch of tools for compiling and building software. If you are a dev on an existing Mac setup, you already have this installed.
There’s a number of ways to install Nix. I chose to use Determinate System’s installer, which does a bunch of nifty stuff with separating the Nix config into a separate drive, and a bunch of sane defaults that I probably don’t even know I need:
Setup
With Nix installed, we can install nix-darwin, which has a bunch of Mac-specific defaults.
I used the Nixcademy blog post “Setting up Nix on macOS” here to get an initial file to work off ot. The important part is to run these commands:
Where you put that directory doesn’t seem particularly important. I stuck mine in ~/Developer/nix
.
You’ll get flake.nix
, which is your Nix config. You can read the Nixcademy blog post above to learn more about how to customize it.
With everything set up, you can run the main command to re-init your Nix config:
The fun stuff
Now, it’s time to configure. Here’s my full Nix config that got me from start to finish with this repo, the one for the website you’re reading. That means installing Neovim, Zellij, and Node.js, as well as Homebrew. My username on my machine is kristian
and the hostname for my machine is bilbo
, so change as needed:
There’s some really interesting and impressive stuff here:
system.defaults.dock
allows full Dock customization! You can programatically set which items are in your Dock, which is quite cool.- You can install Homebrew packages from within Nix. And with the
onActivation.cleanup = "uninstall"
config option, any packages you remove will get installed. It’s declarative Homebrew, which is super useful.
There’s sure to be a lot more I’ll do here. I’ve heard home-manager
is cool, and now that I’m home for the winter, I’m dual-working on my Mac Studio and this laptop, so syncing config between them is sure to be useful. Excited to dig into this more!