lib: standardize arguments (again)

We'll just copy what we did with Bahaghari project.
This commit is contained in:
Gabriel Arazas 2024-03-03 16:39:32 +08:00
parent 344a1667a4
commit ba6b30ca95
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
9 changed files with 63 additions and 53 deletions

View File

@ -2,41 +2,12 @@
{ pkgs }:
pkgs.lib.makeExtensible
(self:
rec {
/* Count the attributes with the given predicate.
(self:
let
inherit (pkgs) lib;
callLib = file: import file { inherit pkgs lib self; };
in {
trivial = callLib ./trivial.nix;
Examples:
countAttrs (name: value: value) { d = true; f = true; a = false; }
=> 2
countAttrs (name: value: value.enable) { d = { enable = true; }; f = { enable = false; package = [ ]; }; }
=> 1
*/
countAttrs = pred: attrs:
pkgs.lib.count (attr: pred attr.name attr.value)
(pkgs.lib.mapAttrsToList pkgs.lib.nameValuePair attrs);
/* Returns the file path of the given config of the given environment.
Type: getConfig :: String -> String -> Path
Example:
getConfig "home-manager" "foo-dogsquared"
=> ../configs/home-manager/foo-dogsquared
*/
getConfig = type: config: ../configs/${type}/${config};
/* Returns the file path of the given user subpart of the given
environment. Only certain environments such as NixOS have this type of
setup.
Type: getConfig :: String -> String -> Path
Example:
getUser "nixos" "foo-dogsquared"
=> ../configs/nixos/_users/foo-dogsquared
*/
getUser = type: user: ../configs/${type}/_users/${user};
})
inherit (self.trivial) countAttrs getConfig getUser;
})

View File

@ -1,5 +1,5 @@
# Custom libraries for home-manager library.
{ pkgs, lib }:
{ pkgs, lib, self }:
rec {
/*
@ -15,7 +15,7 @@ rec {
# The default value when `attrPath` is missing.
default:
attrs ? nixosConfig && pkgs.lib.attrByPath attrPath default attrs;
attrs ? nixosConfig && lib.attrByPath attrPath default attrs;
hasDarwinConfigAttr =
# The configuration attribute set of the home-manager configuration.

View File

@ -1,14 +1,14 @@
# All of the functions suitable only for NixOS.
{ pkgs, lib }:
{ pkgs, lib, self }:
rec {
{
# Checks if the NixOS configuration is part of the nixos-generator build.
# Typically, we just check if there's a certain attribute that is imported
# from it.
hasNixosFormat = config:
pkgs.lib.hasAttrByPath [ "formatAttr" ] config;
lib.hasAttrByPath [ "formatAttr" ] config;
# Checks if the NixOS config is being built for a particular format.
isFormat = config: format:
hasNixosFormat config && config.formatAttr == format;
(config.formatAttr or "") == format;
}

View File

@ -1,4 +1,4 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
{
isStandalone = config:

View File

@ -1,5 +1,5 @@
# A library specifically for environments with sops-nix.
{ pkgs, lib }:
{ pkgs, lib, self }:
{
/* Get the secrets from a given sops file. This will set the individual
@ -21,7 +21,7 @@
let
getKey = key: { inherit key sopsFile; };
in
pkgs.lib.mapAttrs
lib.mapAttrs
(path: attrs:
(getKey path) // attrs)
secrets;
@ -44,9 +44,9 @@
}))
*/
attachSopsPathPrefix = prefix: secrets:
pkgs.lib.mapAttrs'
lib.mapAttrs'
(key: settings:
pkgs.lib.nameValuePair
lib.nameValuePair
"${prefix}/${key}"
({ inherit key; } // settings))
secrets;

39
lib/trivial.nix Normal file
View File

@ -0,0 +1,39 @@
{ pkgs, lib, self }:
{
/* Count the attributes with the given predicate.
Examples:
countAttrs (name: value: value) { d = true; f = true; a = false; }
=> 2
countAttrs (name: value: value.enable) { d = { enable = true; }; f = { enable = false; package = [ ]; }; }
=> 1
*/
countAttrs = pred: attrs:
lib.count (attr: pred attr.name attr.value)
(lib.mapAttrsToList lib.nameValuePair attrs);
/* Returns the file path of the given config of the given environment.
Type: getConfig :: String -> String -> Path
Example:
getConfig "home-manager" "foo-dogsquared"
=> ../configs/home-manager/foo-dogsquared
*/
getConfig = type: config: ../configs/${type}/${config};
/* Returns the file path of the given user subpart of the given
environment. Only certain environments such as NixOS have this type of
setup.
Type: getConfig :: String -> String -> Path
Example:
getUser "nixos" "foo-dogsquared"
=> ../configs/nixos/_users/foo-dogsquared
*/
getUser = type: user: ../configs/${type}/_users/${user};
}

View File

@ -8,8 +8,8 @@ in
{
_module.args.foodogsquaredLib =
foodogsquaredLib.extend (final: prev: {
home-manager = import ../../../lib/home-manager.nix { inherit pkgs; lib = prev; };
home-manager = import ../../../lib/home-manager.nix { inherit pkgs lib; self = final; };
} // lib.optionalAttrs (options?sops) {
sops-nix = import ../../../lib/sops.nix { inherit pkgs; lib = prev; };
sops-nix = import ../../../lib/sops.nix { inherit pkgs lib; self = final; };
});
}

View File

@ -8,8 +8,8 @@ in
{
_module.args.foodogsquaredLib =
foodogsquaredLib.extend (final: prev: {
nixos = import ../../../lib/nixos.nix { inherit pkgs; lib = final; };
nixos = import ../../../lib/nixos.nix { inherit pkgs lib; self = final; };
} // lib.optionalAttrs (options?sops) {
sops-nix = import ../../../lib/sops.nix { inherit pkgs; lib = final; };
sops-nix = import ../../../lib/sops.nix { inherit pkgs lib; self = final; };
});
}

View File

@ -7,6 +7,6 @@ in
{
_module.args.foodogsquaredLib =
foodogsquaredLib.extend (final: prev: {
nixvim = import ../../../lib/nixvim.nix { inherit pkgs; lib = prev; };
nixvim = import ../../../lib/nixvim.nix { inherit pkgs lib; self = final; };
});
}