feat(lib): add userListToAttrs function

This commit is contained in:
Mohammad Rafiq 2025-07-07 17:23:36 +08:00
parent d33aabf9d8
commit f53a6c6c99
No known key found for this signature in database

View file

@ -1,10 +1,41 @@
{ lib, config, ... }:
let
cfg = config.flake;
inherit (builtins) foldl' attrNames;
inherit (lib.attrsets) mapAttrs;
in
{
flake.lib.modules = {
/**
Fold over the users list and create an attribute set.
# Inputs
`f`
: A function that takes the name of a user and returns an attribute set.
# Type
```
userListToAttrs :: (String -> AttrSet) -> AttrSet
```
# Examples
:::{.example}
## `userListToAttrs` usage example
```nix
flake.manifest.users.rafiq = { ... };
flake.modules.homeManager.users = userListToAttrs (name: {
${name}.home.username = name;
});
=> flake.modules.homeManager.default.users.rafiq.home.username = "rafiq";
```
:::
*/
userListToAttrs = f: foldl' (acc: elem: acc // (f elem)) { } (attrNames cfg.manifest.users);
/**
Return an attribute set for use with a option that needs to be used for all users.