处于Git仓库内时nix-darwin rebuild执行失败的问题排查
问题:Git初始化后的nix-darwin Flake执行报错
正常情况(未加入Git)
在~/x目录下执行命令无报错:
$ darwin-rebuild switch --dry-run --flake x/ building the system configuration... Password: user defaults... setting up user launchd services... setting up /Applications/Nix Apps... setting up pam... applying patches... setting up /etc... system defaults... setting up launchd services... reloading nix-daemon... waiting for nix-daemon configuring networking... setting up /Library/Fonts/Nix Fonts... setting nvram variables...
异常情况(加入Git后)
复制目录并初始化Git提交后,执行命令出现错误:
$ cp -r x y $ cd y $ git init && git add . && git commit -am 'initial' $ darwin-rebuild switch --dry-run --flake y/ building the system configuration... these 9 derivations will be built: /nix/store/6q8gs4030fj613h1rc77qdd2vmsiydn7-options.json.drv /nix/store/6i4fwbq03hrbsvw9skl60zz8w82z9mzp-options.json.drv /nix/store/1213z7hj01zs50wsy3yjqsj6lcdvw4p7-darwin-manpages.drv /nix/store/xdby0n64ijkcmyl1wf9dby5fgvmin03a-darwin-manual-html.drv /nix/store/7m19k5gppfcbisdzk0rlyhqcq2kqsq88-darwin-help.drv /nix/store/b35vswyff9pfd2a57y85zc8r326fsdq9-system-path.drv /nix/store/ss6wmn08c1zp3snng97hs1qb7pl7x9my-system-applications.drv /nix/store/z5c666bd7d5lrhg879n46hyl7mvp7a0j-darwin-version.json.drv /nix/store/b1wjlf0rnzj84mv3w1yffqa1rph62jnc-darwin-system-24.05.20240621.201ed88+darwin4.29b3096.drv don't know how to build these paths: /nix/store/a2j86yi5rbprnq9wis88pq5n4qyfm0gq-darwin-system-24.05.20240621.201ed88+darwin4.29b3096 error: build of '/nix/store/a2j86yi5rbprnq9wis88pq5n4qyfm0gq-darwin-system-24.05.20240621.201ed88+darwin4.29b3096' failed
用到的flake.nix代码
description = "Example Darwin system flake"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-24.05-darwin"; nix-darwin = { url = "github:LnL7/nix-darwin"; inputs.nixpkgs.follows = "nixpkgs"; }; home-manager = { url = "github:nix-community/home-manager/release-24.05"; inputs.nixpkgs.follows = "nixpkgs"; }; }; outputs = inputs@{ self, nix-darwin, nixpkgs, home-manager }: let configuration = { pkgs, ... }: { # List packages installed in system profile. To search by name, run: # $ nix-env -qaP | grep wget environment.systemPackages = [ pkgs.vim ]; system.stateVersion = 4; # The platform the configuration will be used on. nixpkgs.hostPlatform = "x86_64-darwin"; }; in { # Build darwin flake using: # $ darwin-rebuild build --flake .#xx darwinConfigurations = { "xx" = nix-darwin.lib.darwinSystem { modules = [ configuration home-manager.darwinModules.home-manager { home-manager.useGlobalPkgs = true; home-manager.useUserPackages = true; } ]; }; }; # Expose the package set, including overlays, for convenience. darwinPackages = self.darwinConfigurations."xx".pkgs; };
问题本质
目录被Git初始化后,Nix Flake会自动识别为Git仓库,此时Flake的self引用会绑定到Git的版本状态(如commit哈希)。而你的flake.nix中darwinPackages直接通过self引用darwinConfigurations."xx".pkgs,触发了版本化Flake的构建逻辑——这种情况下Nix会尝试生成基于Git版本的派生,但依赖链出现不匹配,导致无法找到正确的构建路径。
简单来说:普通目录是"本地非版本化Flake",Git仓库是"版本化Flake",两者的self解析逻辑不同,你的输出定义直接通过self引用配置派生,引发了冲突。
解决方法
方法1:修改flake.nix,避免通过self引用
调整darwinPackages的定义,直接复用配置对象而非通过self引用:
outputs = inputs@{ self, nix-darwin, nixpkgs, home-manager }: let configuration = { pkgs, ... }: { # 原有配置内容不变 environment.systemPackages = [ pkgs.vim ]; system.stateVersion = 4; nixpkgs.hostPlatform = "x86_64-darwin"; }; # 提前定义配置对象 xxConfig = nix-darwin.lib.darwinSystem { modules = [ configuration home-manager.darwinModules.home-manager { home-manager.useGlobalPkgs = true; home-manager.useUserPackages = true; } ]; }; in { darwinConfigurations = { "xx" = xxConfig; }; # 直接使用提前定义的对象,绕过self引用 darwinPackages = xxConfig.pkgs; };
方法2:执行命令时明确指定配置名称
运行命令时加上配置标识,让Nix直接定位到你定义的配置,而非自动推断:
darwin-rebuild switch --dry-run --flake y/#xx
方法3:强制忽略Git版本化(不推荐)
使用--impure参数让Nix将目录视为普通非版本化Flake:
darwin-rebuild switch --dry-run --flake y/ --impure
该方法会绕过Flake的纯模式检查,仅适合临时测试,不推荐长期使用。
内容的提问来源于stack exchange,提问作者ubi
相关产品推荐
相关产品推荐

