mirror of
https://github.com/ryan4yin/nix-config.git
synced 2026-03-26 19:31:31 +01:00
feat: my custom libAttrs
This commit is contained in:
46
lib/attrs.nix
Normal file
46
lib/attrs.nix
Normal file
@@ -0,0 +1,46 @@
|
||||
{ lib, ... }:
|
||||
|
||||
rec {
|
||||
# Generate an attribute set from a list.
|
||||
#
|
||||
# lib.genAttrs [ "foo" "bar" ] (name: "x_" + name)
|
||||
# => { foo = "x_foo"; bar = "x_bar"; }
|
||||
listToAttrs = lib.genAttrs;
|
||||
|
||||
# Update only the values of the given attribute set.
|
||||
#
|
||||
# mapAttrs
|
||||
# (name: value: ("bar-" + value))
|
||||
# { x = "a"; y = "b"; }
|
||||
# => { foo = "bar-a"; foo = "bar-b"; }
|
||||
mapAttrs = lib.attrsets.mapAttrs;
|
||||
|
||||
# Update both the names and values of the given attribute set.
|
||||
#
|
||||
# mapAttrs'
|
||||
# (name: value: nameValuePair ("foo_" + name) ("bar-" + value))
|
||||
# { x = "a"; y = "b"; }
|
||||
# => { foo_x = "bar-a"; foo_y = "bar-b"; }
|
||||
mapAttrs' = lib.attrsets.mapAttrs';
|
||||
|
||||
# Merge a list of attribute sets into one. smilar to the operator `a // b`, but for a list of attribute sets.
|
||||
#
|
||||
# mergeAttrsList
|
||||
# [ { x = "a"; y = "b"; } { x = "c"; z = "d"; } { g = "e"; } ]
|
||||
# => { x = "c"; y = "b"; z = "d"; g = "e"; }
|
||||
mergeAttrsList = lib.attrsets.mergeAttrsList;
|
||||
|
||||
# Generate a string from an attribute set.
|
||||
#
|
||||
# attrsets.foldlAttrs
|
||||
# (acc: name: value: acc + "\nexport ${name}=${value}")
|
||||
# "# A shell script"
|
||||
# { x = "a"; y = "b"; }
|
||||
# =>
|
||||
# ```
|
||||
# # A shell script
|
||||
# export x=a
|
||||
# export y=b
|
||||
# ````
|
||||
foldlAttrs = lib.attrsets.foldlAttrs;
|
||||
}
|
||||
Reference in New Issue
Block a user