Old version Version 4 of 6

Finder on macOS is slowing down your Nix builds

ed05c18a 4 min read

You are reading an old version of this post. Read the latest version →

View

TLDR: Skip to the fix.

I’m one of those people who main Nix on a MacBook Pro. I will be the first to advocate for NixOS, but for a travel machine, I just want something reliable, that pairs with my AirPods, and can survive away from an outlet for a few hours.

I’ll admit that Apple’s ecosystem isn’t particularly welcoming to a tool like Nix, but support has improved drastically in recent years (thank you, dear contributors!). We’re at a point now where I can recommend both Nix and nix-darwin on macOS with a clear conscience.

Yet there’s one problem that has continued to plague me:

Finder likes to burn through an entire CPU core whenever Nix builds anything, performs garbage collection, or otherwise modifies the /nix/store.

I figured this was just the reality of Nix on macOS, or some issue with APFS, where macOS didn’t give us a way to exclude this volume entirely from indexing.

The problem

A Finder window shows one result symlink. Activity Monitor shows Finder at 100% CPU.
Finder at 100% CPU during a Nix build, with one result symlink on screen. This is running in a VM. I was sound asleep at 5 AM.

What I recently realized is that the issue isn’t random or always present. You can actually trigger it yourself by opening a directory in Finder that has a symlink into the Nix store. This is quite common in Nix workflows. Just think of all the result links left behind by nix build, or symlinked configuration files in your home directory.

Once Finder has seen that symlink, anything that changes the Nix store sends Finder into a loop, and closing the window does absolutely nothing.

nix buildor a garbage collection/nix/store134k entriesfseventsdwatches the Nix volumeFinderlists the whole store againresultsymlink into the storewriteschangeFSEventresolvespoints torepeats on every change nix buildor a garbage collection/nix/store134k entriesfseventsdwatches the Nix volumeFinderlists the whole store againresultsymlink into the storewriteschangeFSEventresolvespoints torepeats on every change

The only workaround that I knew of was to force relaunch Finder and avoid opening any windows until Nix was done doing its thing.

Digging into Finder

The first thing I did was sample Finder to see what it was actually doing. I also noticed that Finder was holding /nix/store open as a directory and had grown from 51 MB to 648 MB of memory. Every run showed the same hot path:

FSEvents thread
3228 TSystemNotificationTask::HandleFSEvent  (in DesktopServicesPriv)
  3228 TNode::HandleChange  (in DesktopServicesPriv)
    3228 TNode::FindChildButDontSynchronize  (in DesktopServicesPriv)
      3228 __psynch_mutexwait  (in libsystem_kernel.dylib)

Sync thread
3228 TNode::HandleNodeRequest  (in DesktopServicesPriv)
  3228 TNode::HandleSync  (in DesktopServicesPriv)
    3205 TNode::SynchronizeChildren  (in DesktopServicesPriv)
      3015 TChildrenList::FindRenamedChild  (in DesktopServicesPriv)

The thread that receives the file system event waits on a lock while a second thread syncs the store. Most of the samples land in FindRenamedChild, which looks for renamed entries.

Activity Monitor lists Finder first, with 648.3 MB of memory.
Finder at 648 MB after one build in a test VM.

I tasked Claude with writing a couple of scripts to reproduce this behaviour systematically. It sampled Finder, added paths to the store, and counted the samples under TNode::HandleSync in the call tree. It then created some symlinks in a Finder window to trigger the behaviour and collected more samples.

What did not work

Issues with Finder and the Nix store are well known on macOS, and the various Nix installers already apply mitigations to stop the file system services from watching the store.

  • The Nix store lives on its own volume
  • The volume is mounted with nobrowse in /etc/fstab
  • The /nix mount is flagged hidden

These flags stop Finder and Spotlight from walking into /nix, but not from following a symlink.

I had also previously tried adding the documented .fseventsd/no_log marker file to the volume. This stops the on-disk event history, but doesn’t actually stop the live events from arriving, as others have found.

Disassembly

At this point, I needed to dig deeper into what Finder was actually doing and what the functions in the stack traces were. I opened DesktopServicesPriv in Hopper and started digging through the functions.

When Finder shows a symlink, TNode::FollowAlias gets a node for the link target. This creates a node for each directory on the path, including /nix/store. When an event arrives, TNode::HandleChange looks up the event path in the node tree. If it finds a node, it synchronizes it: TNode::HandleSync calls TNode::SynchronizeChildren, which lists the full directory and builds a node for every entry. Nothing checks whether a window still shows the directory.

Hopper shows the disassembly of TNode::FollowAlias with two highlighted blocks: a call to TNode::GetNodeFromFSInfo that gets the node for the link target, and a call to TNode::SetAliasTarget that keeps the target node on the link node.
TNode::FollowAlias gets a node for the link target.
Hopper shows the disassembly of TNode::HandleChange with one highlighted block: when a node exists for the event path, it calls TNode::HandleListeners, which leads to the sync.
Once /nix/store has a node, each event on it starts a sync.

The undocumented marker

Without any obvious knobs to turn in Finder, I decided to dig into fseventsd, which actually generates the events. Loading the binary into Hopper, I found some curious strings right next to each other:

%s/.fseventsd/no_log
%s/.fseventsd/ignore
%s: mount_point(%s) dev(%d) will be completely ignored

There’s the documented no_log marker, which we had already figured out does not help us. But there’s also another one called ignore. What’s interesting is that no_log shadows ignore. A volume with both markers behaves as if it only had no_log.

Hopper shows the disassembly of fseventsd with two highlighted blocks: the no_log check first, then the ignore check.
fseventsd checks for no_log first, and for ignore only when no_log is absent.
mountno_logmarker exists?ignoremarker exists?Normalall eventsHistory log offlive events still sentVolume ignoredno events at allnonoyesyesStops here. ignore is never checked.What the Nix store needs. mountno_logmarker exists?ignoremarker exists?Normalall eventsHistory log offlive events still sentVolume ignoredno events at allyesnoStops here. ignore is never checked.yesnoWhat the Nix store needs.

Let’s go try out ignore!

The fix

sudo mkdir -p /nix/.fseventsd
sudo touch /nix/.fseventsd/ignore

Make sure you don’t have a /nix/.fseventsd/no_log file in there, because it takes precedence over ignore.

Reboot the machine.

You can also remount the volume and relaunch Finder, but you might also have to relaunch various Nix services too.

Validation

I tested each marker in a clean macOS VM with a result symlink open in Finder while adding paths to the store.

Marker on /nixEvents for /nix/storeFinder CPU
none5 of 597%
no_log5 of 596%
ignore0 of 5idle
0200400600 MBBefore the build, 51 MBNo marker648 MBno_log670 MBignore51 MBno change 0200400600 MBBefore the build, 51 MBNo marker648 MBno_log670 MBignore51 MBno change
Finder memory after one build in the test VM, measured with footprint.

What’s next

I did a cursory search for this flag and haven’t see anyone talking about it yet. We should absolutely get this into every Nix installer out there ASAP. I’m already getting more battery life, faster builds, and my laptop is no longer as much of a space heater.

The trade-off here is that anything that relies on FSEvents won’t see any changes on the Nix volume. I think that’s generally a pro rather than a con, just because FSEvents is extremely slow, so I don’t think you really want it enabled on the store anyway. kqueue-based watchers should still work.

I suppose the other downside is that the marker is undocumented, so it’s kind of private API. But that’s par for the course on macOS.

fix(writing): inline figures with a phone layout

+3 −3 lines · +516 −489 characters · since version 3

The problem

   Once Finder has seen that symlink, anything that changes the Nix store sends Finder into a loop, and closing the window does absolutely nothing.  −![A result symlink in a Finder window points into /nix/store. Each store change sends an FSEvent, and Finder lists the whole store again.](./figures/trigger.svg)+![A result symlink in a Finder window points into /nix/store. Each store change sends an FSEvent, and Finder lists the whole store again.](./figures/trigger.svg){.inline}   The only workaround that I knew of was to force relaunch Finder and avoid opening any windows until Nix was done doing its thing.  

The undocumented marker

   ![Hopper shows the disassembly of fseventsd with two highlighted blocks: the no_log check first, then the ignore check.](./screenshots/hopper-fseventsd-markers.jpg "fseventsd checks for no_log first, and for ignore only when no_log is absent.")  −![fseventsd checks no_log first and returns. It checks ignore only when no_log is absent.](./figures/marker-logic.svg)+![fseventsd checks no_log first and returns. It checks ignore only when no_log is absent.](./figures/marker-logic.svg){.inline}   Let's go try out `ignore`!  

Validation

 | `no_log`         | 5 of 5                  | 96%        | | `ignore`         | 0 of 5                  | idle       |  −![Finder memory after one build: 648 MB with no marker, 670 MB with no_log, 51 MB with ignore. It starts at 51 MB.](./figures/memory.svg "Finder memory after one build in the test VM, measured with footprint.")+![Finder memory after one build: 648 MB with no marker, 670 MB with no_log, 51 MB with ignore. It starts at 51 MB.](./figures/memory.svg "Finder memory after one build in the test VM, measured with footprint."){.inline}   ## What's next  

Other files

  • default.nix changed
  • figures/chart.py changed
  • figures/marker_logic.py changed
  • figures/svg.py changed
  • figures/trigger.py changed