Skip to content

Instantly share code, notes, and snippets.

@mbrasch
Last active October 26, 2023 14:03
Show Gist options
  • Save mbrasch/b335115356858e746307d35483c7f68b to your computer and use it in GitHub Desktop.
Save mbrasch/b335115356858e746307d35483c7f68b to your computer and use it in GitHub Desktop.
Broken Nix after macOS update

Unfortunately, macOS always overwrites the /etc/zshrc file during updates. This is a problem insofar as the hook for Nix is here (and has to be here). To circumvent this problem, you can use a launchd job to check on every system start whether this hook still exists and rewrite it if necessary:

  • create the file /Library/LaunchDaemons/org.nixos.darwin.check-zshrc-nix-hook.plist with the content mentioned below
  • set the correct user and access rights:
sudo chown root:wheel /Library/LaunchDaemons/org.nixos.darwin.check-zshrc-nix-hook.plist
sudo chmod u=rw,go=r /Library/LaunchDaemons/org.nixos.darwin.check-zshrc-nix-hook.plist
  • load the launchd job:
sudo launchctl load /Library/LaunchDaemons/org.nixos.darwin.check-zshrc-nix-hook.plist

To remove this launchd job simply do:

sudo launchctl bootout system/org.nixos.darwin.check-zshrc-nix-hook
sudo rm /Library/LaunchDaemons/org.nixos.darwin.check-zshrc-nix-hook.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>GroupName</key>
<string>wheel</string>
<key>InitGroups</key>
<true/>
<key>Label</key>
<string>org.nixos.darwin.check-zshrc-nix-hook</string>
<key>LaunchOnlyOnce</key>
<false/>
<key>LimitLoadToSessionType</key>
<string>System</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>-c</string>
<string>
if grep -q '# Nix' /etc/zshrc ; then
echo
else
echo '
# Nix
if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
. '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
fi
# End Nix
' | sudo tee -a /etc/zshrc
fi
</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>root</string>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment