a flake module to ease creating and managing multiple hosts in your nix flake.

fix: correct importing module args for some nixpkgs modules

+40 -22
+40 -22
lib.nix
··· 101 101 else 102 102 throw "cannot find nix-darwin input"; 103 103 104 + # create the modulesPath based on the system, we need 105 + modulesPath = 106 + if class == "darwin" then "${darwinInput}/modules" else "${inputs.nixpkgs}/nixos/modules"; 107 + 108 + # we need to import the module list for our system 109 + # this is either the nixos modules list provided by nixpkgs 110 + # or the darwin modules list provided by nix darwin 111 + baseModules = import "${modulesPath}/module-list.nix"; 112 + 104 113 eval = evalModules { 105 114 # we use recursiveUpdate such that users can "override" the specialArgs 106 115 # 107 116 # This should only be used for special arguments that need to be evaluated 108 117 # when resolving module structure (like in imports). 109 118 specialArgs = recursiveUpdate { 110 - # create the modulesPath based on the system, we need 111 - modulesPath = 112 - if class == "darwin" then "${darwinInput}/modules" else "${inputs.nixpkgs}/nixos/modules"; 119 + inherit 120 + # these are normal args that people expect to be passed 121 + lib 122 + self # even though self is just the same as `inputs.self` 123 + inputs 124 + 125 + # these come from flake-parts 126 + self' 127 + inputs' 113 128 114 - # laying it out this way is completely arbitrary, however it looks nice i guess 115 - inherit lib; 116 - inherit self self'; 117 - inherit inputs inputs'; 129 + # we need to set this beacuse some modules require it sadly 130 + # you may also recall `modulesPath + /installer/scan/not-detected.nix` 131 + modulesPath 132 + ; 118 133 } specialArgs; 119 134 120 135 # A nominal type for modules. When set and non-null, this adds a check to ··· 122 137 class = classToND class; 123 138 124 139 modules = flatten [ 140 + # bring in all of our base modules 141 + baseModules 142 + 125 143 # import our host system paths 126 144 ( 127 145 if path != null then ··· 140 158 "${inputs.nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal-new-kernel.nix" 141 159 ]) 142 160 143 - # we need to import the module list for our system 144 - # this is either the nixos modules list provided by nixpkgs 145 - # or the darwin modules list provided by nix darwin 146 - (import ( 147 - if class == "darwin" then 148 - "${darwinInput}/modules/module-list.nix" 149 - else 150 - "${inputs.nixpkgs}/nixos/modules/module-list.nix" 151 - )) 152 - 153 161 (singleton { 154 - # TODO: learn what this means and why its needed to build the iso 155 - _module.args.modules = [ ]; 162 + # some modules to have these arguments, like documentation.nix 163 + # <https://github.com/NixOS/nixpkgs/blob/9692553cb583e8dca46b66ab76c0eb2ada1a4098/nixos/modules/misc/documentation.nix> 164 + _module.args = { 165 + inherit baseModules; 166 + 167 + # this should in the future be the modules that the user added without baseModules 168 + modules = [ ]; 169 + 170 + # TODO: remove in 25.05 171 + # https://github.com/NixOS/nixpkgs/blob/9692553cb583e8dca46b66ab76c0eb2ada1a4098/nixos/lib/eval-config.nix#L38 172 + extraModules = [ ]; 173 + }; 156 174 157 175 # we set the systems hostname based on the host value 158 176 # which should be a string that is the hostname of the system ··· 236 254 ) 237 255 ); 238 256 239 - onlyDirs = filterAttrs (_: type: type == "directory"); 240 - 241 257 normaliseHosts = 242 258 cfg: hosts: 243 259 if (cfg.onlySystem == null) then ··· 281 297 if (cfg.onlySystem != null) then 282 298 hostsDir 283 299 else 284 - mapAttrs (path: _: readDir "${cfg.path}/${path}") (onlyDirs hostsDir); 300 + mapAttrs (path: _: readDir "${cfg.path}/${path}") ( 301 + filterAttrs (_: type: type == "directory") hostsDir 302 + ); 285 303 in 286 304 normaliseHosts cfg hosts; 287 305 in