feat(nix): add forAllUsers' and docstrings to lib
Adds forAllUsers' function and docstrings to nix lib.
This commit is contained in:
parent
46cf93f69b
commit
07413c4ac0
2 changed files with 88 additions and 3 deletions
|
@ -5,7 +5,92 @@ let
|
|||
in
|
||||
{
|
||||
flake.lib = {
|
||||
/**
|
||||
Remove the top level attributes from an attribute set and return the merged attributes.
|
||||
|
||||
# Inputs
|
||||
|
||||
`attrset`
|
||||
|
||||
: An attribute set to flatten.
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
flattenAttrs :: AttrSet -> AttrSet
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `flattenAttrs` usage example
|
||||
|
||||
```nix
|
||||
flattenAttrs { x = { a = 1; b = 2; }; y = { c = 3; d = 4; }; }
|
||||
=> { a = 1; b = 2; c = 3; d = 4; }
|
||||
```
|
||||
*/
|
||||
flattenAttrs = attrset: concatMapAttrs (_: v: v) attrset;
|
||||
forAllUsers = f: mapAttrs f cfg.manifest.users;
|
||||
|
||||
/**
|
||||
Return an attribute set for use with a option that needs to be used for all users.
|
||||
|
||||
# Inputs
|
||||
|
||||
`attrset`
|
||||
|
||||
: An attribute set to apply to all the users.
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
forAllUsers :: AttrSet -> AttrSet
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `forAllUsers` usage example
|
||||
|
||||
```nix
|
||||
flake.manifest.users.rafiq = { ... };
|
||||
flake.modules.nixos.default.users = forAllUsers {
|
||||
isNormalUser = true;
|
||||
};
|
||||
=> flake.modules.nixos.default.users.rafiq.isNormalUser = true;
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
forAllUsers = attrset: mapAttrs (_: _: attrset) cfg.manifest.users;
|
||||
|
||||
/**
|
||||
Like forAllUsers, but passes in the name and value from the manifest.
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
||||
: A function that takes an attribute name and its value, and returns the new value for the attribute.
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
forAllUsers' :: (String -> Any -> Any) -> AttrSet
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `forAllUsers'` usage example
|
||||
|
||||
```nix
|
||||
flake.manifest.users.rafiq = { ... };
|
||||
flake.modules.homeManager.users = forAllUsers' (name: value: {
|
||||
home.username = name;
|
||||
});
|
||||
=> flake.modules.homeManager.default.users.rafiq.home.username = "rafiq";
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
forAllUsers' = f: mapAttrs f cfg.manifest.users;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.flake;
|
||||
inherit (config.flake.lib) forAllUsers flattenAttrs;
|
||||
inherit (config.flake.lib) forAllUsers' flattenAttrs;
|
||||
inherit (lib.attrsets) filterAttrs;
|
||||
owner = flattenAttrs (filterAttrs (_: v: (v.primary or false)) cfg.manifest.users);
|
||||
in
|
||||
|
@ -27,7 +27,7 @@ in
|
|||
openssh.authorizedKeys.keys = [ owner.pubkey ];
|
||||
};
|
||||
};
|
||||
home-manager.users = forAllUsers (
|
||||
home-manager.users = forAllUsers' (
|
||||
name: _: {
|
||||
#TODO: move into nixos/darwin config - should not apply to homeConfigurations
|
||||
home.username = name;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue