nix-darwin flake中homeDirectory未定义错误排查求助
排查Mac M1上nix-darwin+home-manager配置错误:homeDirectory为null的问题
在Mac M1上配置nix-darwin与home-manager时,使用以下最小flake配置:
{ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nix-darwin.url = "github:nix-darwin/nix-darwin/master"; nix-darwin.inputs.nixpkgs.follows = "nixpkgs"; home-manager.url = "github:nix-community/home-manager"; # Optional: align inputs to avoid mismatched versions home-manager.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.neovim pkgs.git pkgs.fzf pkgs.tig pkgs.zsh pkgs.zsh-autosuggestions pkgs.zsh-syntax-highlighting pkgs.oh-my-zsh ]; # Necessary for using flakes on this system. nix.settings.experimental-features = "nix-command flakes"; # Enable alternative shell support in nix-darwin. # programs.fish.enable = true; # Set Git commit hash for darwin-version. system.configurationRevision = self.rev or self.dirtyRev or null; # Used for backwards compatibility, please read the changelog before changing. # $ darwin-rebuild changelog system.stateVersion = 6; # The platform the configuration will be used on. nixpkgs.hostPlatform = "aarch64-darwin"; }; in { darwinConfigurations."fes300s-MacBook-Pro" = nix-darwin.lib.darwinSystem { modules = [ configuration home-manager.darwinModules.home-manager { home-manager = { useGlobalPkgs = true; useUserPackages = true; users.fes300 = { home.username = "fes300"; home.homeDirectory = "/Users/fes300"; home.packages = with nixpkgs.legacyPackages.aarch64-darwin; [ htop ]; home.stateVersion = "23.11"; }; }; } ]; }; };
执行sudo darwin-rebuild switch时出现错误:
error: … while evaluating the attribute 'optionalValue.value' at /nix/store/xrr233zsz756b4srmma8z6y0ff606qd4-source/lib/modules.nix:1148:5: 1147| 1148| optionalValue = if isDefined then { value = mergedValue; } else { }; | ^ 1149| }; … while evaluating a branch condition at /nix/store/xrr233zsz756b4srmma8z6y0ff606qd4-source/lib/modules.nix:1148:21: 1147| 1148| optionalValue = if isDefined then { value = mergedValue; } else { }; | ^ 1149| }; … while evaluating definitions from `/nix/store/y4570srvp978bdplsk93xsva5x19cf8r-source/modules/system': … while evaluating the option `home-manager.users.fes300.home.homeDirectory': (stack trace truncated; use '--show-trace' to show the full, detailed trace) error: A definition for option `home-manager.users.fes300.home.homeDirectory' is not of type `absolute path'. Definition values: - In `/nix/store/6lk47wqmxqg5fs5prvi2i1pazwlkgncj-source/nixos/common.nix': null
问题原因
错误根源在于home-manager用户配置的层级结构错误:你把username、homeDirectory等属性嵌套在了home子节点下,但这些属性应该直接定义在用户节点的顶层。这种错误结构导致home-manager无法正确读取homeDirectory的值,最终解析为null,触发类型校验错误。
解决方法
修正flake配置中home-manager用户部分的结构,将home.username、home.homeDirectory移到用户节点顶层,同时简化home.packages的写法(因为启用了useGlobalPkgs,可以直接使用全局的pkgs)。
修正后的完整配置:
{ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nix-darwin.url = "github:nix-darwin/nix-darwin/master"; nix-darwin.inputs.nixpkgs.follows = "nixpkgs"; home-manager.url = "github:nix-community/home-manager"; # Optional: align inputs to avoid mismatched versions home-manager.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = inputs@{ self, nix-darwin, nixpkgs, home-manager }: let configuration = { pkgs, ... }: { environment.systemPackages = [ pkgs.neovim pkgs.git pkgs.fzf pkgs.tig pkgs.zsh pkgs.zsh-autosuggestions pkgs.zsh-syntax-highlighting pkgs.oh-my-zsh ]; nix.settings.experimental-features = "nix-command flakes"; system.configurationRevision = self.rev or self.dirtyRev or null; system.stateVersion = 6; nixpkgs.hostPlatform = "aarch64-darwin"; }; in { darwinConfigurations."fes300s-MacBook-Pro" = nix-darwin.lib.darwinSystem { modules = [ configuration home-manager.darwinModules.home-manager { home-manager = { useGlobalPkgs = true; useUserPackages = true; users.fes300 = { # 将属性移到用户节点顶层 username = "fes300"; homeDirectory = "/Users/fes300"; # 直接使用全局pkgs,无需指定legacyPackages packages = with pkgs; [ htop ]; stateVersion = "23.11"; }; }; } ]; }; };
重新执行sudo darwin-rebuild switch即可完成配置。
内容的提问来源于stack exchange,提问作者federico sordillo
相关产品推荐
相关产品推荐

