The purpose of this repository is to document my exploration of the way to set up Nix for my MacOS system on both my MacBook (personal) and my Mac Mini (family shared computer).
I first learnt about Nix from watching tech videos on YouTube and that led me to learning about Nix, NixOS, nix-darwin, and the EXPERIMENTAL Flakes. The following are some of the important I had read before I decided on the current way of setting up Nix on my MacOS systems:
- Official Nix site
- How to learn Nix
- Determinate Systems
- Zero to Nix
- Flakes - NixOS Wiki
- Flakes aren't real and cannot hurt you: a guide to using Nix flakes the non-flake way
- Summary of Nix Flakes vs original Nix
- Nix Flakes is an experiment that did too much at once…
- nix-darwin
- Declarative macOS Configuration Using nix-darwin And home-manager
My goals of my Nix journey:
- Study how easy it will be to reproduce a macOS-based setup on another brand new machine - especially in terms of setting up non-Microsoft tools.
- Document the learning points during my journey.
- Add relevant tools or script that I believe would help create reproducible macOS setup
Before I started messing with my actual MacOS setup, I first setup MacOS VMs to experiment. MacOS VMs can be setup via UTM - Securely run operating systems on your Mac.
After quite After quite a few tests, I finally decided to start setting up Nix on my MacBook with the "standard" way of setting up Nix on MacOS. This page documents what actually occurs on your MacOS system during the setup process.
Getting the version of Nix
installed
nix --version
Getting the list of subscribed channels:
sudo nix-channel --list
Channels are something like Linux Debian apt sources - they point to where packages can be downloaded and installed.
The default subscribed channel is nixpkgs
which by default points to
https://nixos.org/channels/nixpkgs-unstable
When you install Nix
via the "official"
nixos install method,
it uses the latest version you can find in the nix releases
folder here.
But if you subsequently execute the command nix upgrade-nix
, you may likely
downgrade your Nix
version. You can find out the version of the Nix
you are
"upgrading" to before running it by executing either of the following commands:
nix-env -qaA nixpkgs.nix
or
sudo nix upgrade-nix --dry-run
Follow these instructions to install a specific version of nix.
To find the actual latest version of Nix
(outside of nixpkgs
):
sudo nix-env --query nix 2>/dev/null
If you had accidentally ran the command:
sudo nix upgrade-nix
that would result in an older version of Nix
being installed, you can force
Nix
back to the latest version by running the following command:
sudo nix upgrade-nix --nix-store-paths-url https://releases.nixos.org/nix/$(nix-env --query nix)/fallback-paths.nix
This was discussed in the Nix installer bug
report - see
grahamc's comment on 26 Nov 2023 for context. It seems that the nixpkgs
maintenance team is "responsible" for deciding on the "safe" version of Nix
to
use when an upgrade is executed.
I am still not yet decided which is "safe" to use - but since the purpose of
Nix
is to be a package manager, then it may be best to start the experiment
with using the nixpkgs
team's recommendation of the safe version to use.
There is a series of complex steps for uninstalling Nix from
MacOS.
Because these steps require reboots and messing around with MacOS's daemons, I
created an uninstall.sh script to automate this process so that it is easier to
uninstall Nix
correctly.
I first experimented with flakes because it was a very popular recommendation amongst many Nix enthusiasts. And it was also recommended by Determinate Systems, a company cofounded by Eelco Dolstra - the inventor of Nix.
I decided against using flakes for starting my Nix journey for the following reasons:
- I felt that I could transition to flakes easily later
- After installing the Determinate Systems installer, I tried to uninstall. But I found that there are some leftover folders and services that were not removed after uninstall.
- The articles arguing against flakes
- Parts of the Nix development community seem to be against it because of the lack of proper process in the manner it was adopted
I have not delved too much into nix-darwin except to confirm that it does not require flakes. So, it is likely I would proceed with playing around with this project to see how it helps in reproducing a macOS setup with a "declarative system approach to macOS".
I created auto-install scripts to install Nix
and nix-darwin
with minimal
user intervention. These tools can now be installed by executing this command
in the Terminal window:
sh <(curl -L https://raw.githubusercontent.com/khsoh/mymacnix/main/nix-autoinstall) [--install=nixonly] [--branch=<git-feature-branch>]
The --branch
option can be use to test feature branches. The --install
option can be used to stop to auto-install process at a Nix-only install
An important update on the learning journey. I discovered this important site nix.dev by the Nix documentation team to guide newbies in their Nix journey. It would have been good if I had started on my journey with this resource. But I believe my future learning would be best spent on this site.
As of today, I have managed to get installation to the point of setting up the most important tools and configurations for them:
- home-manager
- neovim
- tmux
- git
- 1Password (CLI)
The only non-automated step I think I need to do now is to install 1Password GUI
on the MAC because nix-darwin
installs this in the /Applications/Nix Apps/
folder. But 1Password does not execute because it was not installed inside of
/Applications/
folder (reference: 1Password doesn't work on Darwin/macOS
#254944
Next steps are:
- Further investigation of 1Password
- Investigate how to use agenix package for secrets management
- Studying nix.dev to understand Nix better