MacOS & Nix-Darwin
嗯,用户让我帮忙总结一下这篇文章的内容,控制在100字以内,而且不需要用“文章内容总结”之类的开头。直接写描述就行。 首先,我需要通读整篇文章,理解作者的主要经历和观点。文章讲的是作者在尝试将NixOS应用到MacBook上的过程。他之前用过NixOS在Framework笔记本上,但现在更常用的是MacBook,因为更便携。 作者提到他在新工作中使用了另一台Apple Silicon MacBook,发现配置管理起来有点麻烦,特别是忘记之前的工作电脑上的设置。于是他想到用Nix来管理配置,这样可以更方便地维护两台MacBook。 然后他介绍了安装Nix-Darwin的过程,遇到了一些问题,比如配置文件的设置和命令行的问题。但最终通过查阅文档和参考其他人的配置(比如Dustin Lyon的仓库),成功地将Nix-Darwin安装并集成到现有的配置中。 总结一下,文章主要讲述了作者如何通过Nix-Darwin在MacBook上实现类似NixOS的体验,并解决配置管理的问题。过程中遇到了一些挑战,但最终成功了,并且还计划公开自己的配置仓库以回馈社区。 现在要把这些内容浓缩到100字以内。要抓住关键点:作者尝试在MacBook上使用Nix-Darwin的原因、遇到的问题、解决方法以及结果。 可能的结构是:作者为了方便管理和统一配置,在MacBook上安装了Nix-Darwin;过程中遇到配置问题,通过参考他人仓库解决了;成功后计划公开自己的配置。 </think> 作者尝试在 MacBook 上使用 Nix-Darwin 实现类似 NixOS 的体验,并解决多设备配置管理问题。通过参考他人仓库和调整现有配置,成功安装并集成 Nix-Darwin,避免了覆盖原有设置。 2026-2-19 03:5:30 Author: 0xda.de(查看原文) 阅读量:10 收藏

Note: Like my previous articles about Nix, this is not meant to be a guide. It’s just my narrative journey while I explore the thing.

It’s been quite some time since my first foray into NixOS on my framework laptop. While I still love my Framework laptop, I find that I don’t use it that often – in part because it doesn’t really integrate into my Apple ecosystem, but really because it turns out the 16" laptop was just not a very convenient choice. It barely fits in my backpack and it’s just generally too big to carry around. I’ll probably use it to replace my desktop whenever it dies, but I haven’t been using it very much as a daily driver laptop.

I have, however, been using my old first-gen M1 Macbook quite a bit. I also started a new job a few months ago where I got another Apple-silicon Macbook. It was around that time that I learned that I failed to save a lot of my configurations from my previous job’s Macbook, and so I was trying to remember what I liked. I’m not good at remembering things. So I found myself basically setting up two macbooks, and both of them have subtle differences. Not just because one is work and so has a bunch of work software on it, but because I’m human, forgetful, and error-prone.

I found myself longing for the declarative nature of my Framework laptop. I was also chatting with my friend, Adam, about his NixOS experiences and some of the cool work he was doing with generating diffs on pull requests and the potential for better CI/CD integration. A very rapid unplanned inspiration event led me from “I should update my servers to NixOS so I don’t have to worry about them so much anymore” to “Well I have to use my laptop to update my servers, so I guess I should put Nix on my Macbook first.”

Thankfully Nix-Darwin exists, which lets you turn your Macbook into a NixOS-like experience, albeit with a few caveats. I think the most important caveat for me, and something I am exceedingly happy about while writing this, is that it doesn’t blow away all your installed applications or configuration when you set up Nix-Darwin. I hadn’t even considered this a possibility until the moment the rebuild started working (more on this in a minute), but then I briefly panicked like “Oh no, I already have a user, I already have all these apps installed, I already have a bunch of configuration, what if my barebones config blows that all away and I brick my Macbook?”

In classic hacker fashion, of course the command was already running before those thoughts crossed my mind. It would have been too late. Thankfully Nix-Darwin seems to be more forgiving about being installed on existing functional systems and it didn’t modify any of my existing stuff. But let’s rewind a little bit to the struggles that led up to this panic moment.

Installing nix-darwin

The readme for nix-darwin has some guidance in it, but the guidance was kind of confusing for me. I already had a flake from my existing NixOS configurations, and I’d like to reuse that so that I can keep it all in one repo and reuse components where appropriate. One could certainly make the argument that the darwin configuration and the nixos configuration should be completely separate, and I probably would even agree with that idea – it certainly would have made my life easier tonight.

Before I could install nix-darwin, I had to install nix. According to the nix-darwin readme, they recommend installing Lix rather than Nix, because Lix ships with an uninstaller, whereas Nix does not. In the event that I don’t like declaratively managing my Macbook, I thought the uninstaller would be helpful.

Installing Lix was uneventful and it behaves as Nix behaves, or at least so far it does. Next it was time to get the flake configuration setup. I cloned my nixcfg repo that houses the rest of my configuration, and started trying to follow the nix-darwin guidance. Unfortunately the command it suggests running repeatedly did not work for me - sudo nix run nix-darwin/nix-darwin-25.05#darwin-rebuild -- switch. It would just complain warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring error: file 'darwin' was not found in the Nix search path (add it using $NIX_PATH or -I). Like, duh, I’m trying to install nix-darwin, of course it’s not in the path yet.

After a fair bit of searching, I figured out that the solution was obvious in hindsight – I needed to enable flakes, specify my flake, and make sure my darwin configuration was added to my repo. Same as I did for my NixOS machine.

sudo nix run nix-darwin/master#darwin-rebuild --extra-experimental-features "nix-command flakes" -- switch --flake ./flake.nix

This ended up working for me from inside my nixcfg repo. But I’m pretty sure it would also work correctly from anywhere if I replaced ./flake.nix with ~/nixcfg, the path to my nixcfg repo.

After this ran successfully, I now had the darwin- commands available that I expected, such as darwin-rebuild, darwin-help, darwin-options, darwin-version, and darwin-uninstaller.

Dustin Lyon’s Nix Config

What also ended up being a huge help is Dustin Lyon’s nixos-config repository. It’s well laid out, taught me several things about Nix, especially how to integrate nix-darwin into my existing config repo, and overall just gave a lot of inspiration. He even provides templates for new configurations, but since I’m integrating into my existing config, that won’t be particularly helpful for me. I’ll instead be manually integrating the things I like, and leaving out the things I don’t like.

This also serves as a catalyst for me to clean up my nix config and make the repo public – I have gotten a great deal of value from the repos that others have made public, I should contribute that back. I just need to look through the history and make sure there are no secrets that would be problematic if published. On the other hand, I don’t necessarily want to map out all my infrastructure in one repo, so I’m still weighing this. I have some specific ideas for NixOS server configurations that I might publish independently, though.


文章来源: https://0xda.de/blog/2026/02/macos-nix-darwin/
如有侵权请联系:admin#unsafe.sh