lib: privatize parts of library

Some of the functions cater to this specific setup so no need to export
them.
This commit is contained in:
Gabriel Arazas 2022-07-09 13:46:06 +08:00
parent d091d5c29d
commit 40492d55b7
3 changed files with 26 additions and 29 deletions

View File

@ -87,8 +87,9 @@
# We're considering this as the variant since we'll export the custom
# library as `lib` in the output attribute.
lib' =
nixpkgs.lib.extend (final: prev: import ./lib { lib = nixpkgs.lib; });
lib' = nixpkgs.lib.extend (final: prev:
import ./lib { lib = prev; }
// import ./lib/private.nix { lib = final; });
mkHost = { system ? defaultSystem, extraModules ? [ ] }:
(lib'.makeOverridable inputs.nixpkgs.lib.nixosSystem) {

View File

@ -60,33 +60,6 @@ rec {
let paths = lib.collect builtins.isPath attrs;
in builtins.map (path: import path) paths;
/* Return an attribute set of valid users from a given list of users.
This is a convenience function for getting users from the `./users` directory.
Signature:
list -> attrset
Where:
- `list` is a list of usernames as strings
- `attrset` is a set of valid users with the name as the key and the path as the value.
Example:
# Assuming only 'foo-dogsquared' is the existing user for 'home-manager'.
getUsers "home-manager" [ "foo-dogsquared" "archie" "brad" ]
=> { foo-dogsquared = /home/foo-dogsquared/projects/nixos-config/users/foo-dogsquared; }
*/
getUsers = type: users:
let
userModules = filesToAttr ../users/${type};
invalidUsernames = [ "config" "modules" ];
in lib.filterAttrs (n: _: !lib.elem n invalidUsernames && lib.elem n users) userModules;
# Return the path of `user` from `type`.
getUser = type: user:
lib.getAttr user (getUsers type [ user ]);
# Return the path of `secrets` from `../secrets`.
getSecret = path: ../secrets/${path};
/* Count the attributes with the given predicate.
Examples:

23
lib/private.nix Normal file
View File

@ -0,0 +1,23 @@
# This is just a library intended solely for this flake.
# It is expected to use the nixpkgs library with `lib/default.nix`.
{ lib }:
rec {
getSecret = path: ../secrets/${path};
getUsers = type: users:
let
userModules = lib.filesToAttr ../users/${type};
invalidUsernames = [ "config" "modules" ];
users' = lib.filterAttrs (n: _: !lib.elem n invalidUsernames && lib.elem n users) userModules;
userList = lib.attrNames users';
nonExistentUsers = lib.filter (name: !lib.elem name userList) users;
in lib.trivial.throwIfNot ((lib.length nonExistentUsers) == 0)
"there are no users ${lib.concatMapStringsSep ", " (u: "'${u}'") nonExistentUsers} from ${type}"
(r: r) users';
getUser = type: user:
lib.getAttr user (getUsers type [ user ]);
}