I couldn’t find a forum category for questions about solutions without problems, so I’ll ask here…
Has anyone written and/or used a function which “decorates” NixOS modules? I’m not talking about nixfmt
here. More along the lines of function composition.
A NixOS module is a function like: (please correct if I’m wrong)
args@{ lib, config, name, ... } -> result@{ imports, options, config }
There could exist decorator functions which:
- Modify
args
before evaluating the module. - Modify
result
after evaluating the module.
For example, suppose you had a flake exporting a NixOS module. You wish to give that module access to the flake self
variable as an extra argument.
{
outputs = { self, nixpkgs }: {
nixosModules.default = nixpkgs.lib.hypothetical.setModuleArgs
(args: args // { inherit self; })
./module.nix;
};
}
Or maybe you want to translate all option descriptions into Klingon.
{
outputs = { self, nixpkgs, klingon }: let inherit (nixpkgs) lib; in {
nixosModules.default = lib.hypothetical.mapModule (result:
result // {
options = lib.mapAttrsRecursive
(path: value:
if (lib.last path == "description" && lib.isString value)
then klingon.lib.fromEnglish value
else value)
(result.options or {});
}) ./module.nix;
};
}
Usefulness aside, are there reasons why such decorator functions would be impossible or infeasible to implement?