`specialArgs` considered harmful
This article discusses the issue of using specialArgs in NixOS/nix-darwin/home-manager, which can lead to unshareable modules and make it hard to copy-paste configurations. The author explains that specialArgs are often used in inputs to pass around styling dependencies across various components in NixOS. However, as configurations grow larger, specialArgs can become unshareable and lead to hard-to-debug errors.
The author suggests that the issue stems from how modules are evaluated with lib.evalModules and how specialArgs are injected into the arguments passed to the modules. They explain that a module can either have 0 or 1 argument, and that when a module asks for inputs in its arguments, it will only work if the user puts the inputs in the specialArgs or config._module.args.
To address this issue, the author proposes moving curried modules to a separate file and calling import ./version.nix { inherit inputs; }. This approach uses importApplyWithArgs to scan every file under a module directory, applying user-passed arguments and wrapping the result to ensure error messages point to the correct location. By using this method, external consumers of the modules are not forced to provide arguments they don't define, and the structural scoping provided by the module system is maintained.
The author also mentions that the module system keeps the first module with a given key and never reads the rest, which can lead to silent drops of modules when applied with different arguments. They recommend using _module.args instead of specialArgs, but note that this may cause issues in certain cases, such as infinite recursion when pulling a module out of inputs with it.
Rewritten by urgent.news; may contain errors — check the original.
Source: ysun.co