attrsets: fix and add some doc types

This commit is contained in:
hsjobeki 2022-12-24 14:08:11 +01:00
parent d9c3fbfcdc
commit 5ff21bfc73
2 changed files with 38 additions and 27 deletions

View file

@ -16,6 +16,8 @@ rec {
Example: Example:
x = { a = { b = 3; }; } x = { a = { b = 3; }; }
# ["a" "b"] is equivalent to x.a.b
# 6 is a default value to return if the path does not exist in attrset
attrByPath ["a" "b"] 6 x attrByPath ["a" "b"] 6 x
=> 3 => 3
attrByPath ["z" "z"] 6 x attrByPath ["z" "z"] 6 x
@ -23,6 +25,7 @@ rec {
Type: Type:
attrByPath :: [String] -> Any -> AttrSet -> Any attrByPath :: [String] -> Any -> AttrSet -> Any
*/ */
attrByPath = attrByPath =
# A list of strings representing the attribute path to return from `set` # A list of strings representing the attribute path to return from `set`
@ -96,7 +99,7 @@ rec {
=> error: cannot find attribute `z.z' => error: cannot find attribute `z.z'
Type: Type:
getAttrFromPath :: [String] -> AttrSet -> Value getAttrFromPath :: [String] -> AttrSet -> Any
*/ */
getAttrFromPath = getAttrFromPath =
# A list of strings representing the attribute path to get from `set` # A list of strings representing the attribute path to get from `set`
@ -109,10 +112,7 @@ rec {
/* Map each attribute in the given set and merge them into a new attribute set. /* Map each attribute in the given set and merge them into a new attribute set.
Type: Type:
concatMapAttrs :: concatMapAttrs :: (String -> a -> AttrSet) -> AttrSet -> AttrSet
(String -> a -> AttrSet)
-> AttrSet
-> AttrSet
Example: Example:
concatMapAttrs concatMapAttrs
@ -168,8 +168,7 @@ rec {
] { a.b.c = 0; } ] { a.b.c = 0; }
=> { a = { b = { d = 1; }; }; x = { y = "xy"; }; } => { a = { b = { d = 1; }; }; x = { y = "xy"; }; }
Type: Type: updateManyAttrsByPath :: [{ path :: [String], update :: (Any -> Any) }] -> AttrSet -> AttrSet
updateManyAttrsByPath :: [AttrSet] -> AttrSet -> AttrSet
*/ */
updateManyAttrsByPath = let updateManyAttrsByPath = let
# When recursing into attributes, instead of updating the `path` of each # When recursing into attributes, instead of updating the `path` of each
@ -252,6 +251,7 @@ rec {
Example: Example:
attrValues {c = 3; a = 1; b = 2;} attrValues {c = 3; a = 1; b = 2;}
=> [1 2 3] => [1 2 3]
Type: Type:
attrValues :: AttrSet -> [Any] attrValues :: AttrSet -> [Any]
*/ */
@ -341,6 +341,7 @@ rec {
Type: Type:
foldAttrs :: (Any -> Any -> Any) -> Any -> [AttrSets] -> Any foldAttrs :: (Any -> Any -> Any) -> Any -> [AttrSets] -> Any
*/ */
foldAttrs = foldAttrs =
# A function, given a value and a collector combines the two. # A function, given a value and a collector combines the two.
@ -394,7 +395,7 @@ rec {
{ a = 2; b = 20; } { a = 2; b = 20; }
] ]
Type: Type:
cartesianProductOfSets :: AttrSet -> [AttrSet] cartesianProductOfSets :: AttrSet -> [AttrSet]
*/ */
cartesianProductOfSets = cartesianProductOfSets =
# Attribute set with attributes that are lists of values # Attribute set with attributes that are lists of values
@ -413,7 +414,7 @@ rec {
=> { name = "some"; value = 6; } => { name = "some"; value = 6; }
Type: Type:
nameValuePair :: String -> Any -> AttrSet nameValuePair :: String -> Any -> { name :: String, value :: Any }
*/ */
nameValuePair = nameValuePair =
# Attribute name # Attribute name
@ -600,7 +601,7 @@ rec {
=> { } => { }
Type: Type:
optionalAttrs :: Bool -> AttrSet optionalAttrs :: Bool -> AttrSet -> AttrSet
*/ */
optionalAttrs = optionalAttrs =
# Condition under which the `as` attribute set is returned. # Condition under which the `as` attribute set is returned.
@ -646,7 +647,7 @@ rec {
=> { a = ["x" "y"]; b = ["z"] } => { a = ["x" "y"]; b = ["z"] }
Type: Type:
zipAttrsWith :: (String -> [ Any ] -> Any) -> [ AttrSet ] -> AttrSet zipAttrsWith :: (String -> [ Any ] -> Any) -> [ AttrSet ] -> AttrSet
*/ */
zipAttrsWith = zipAttrsWith =
builtins.zipAttrsWith or (f: sets: zipAttrsWithNames (concatMap attrNames sets) f sets); builtins.zipAttrsWith or (f: sets: zipAttrsWithNames (concatMap attrNames sets) f sets);
@ -737,7 +738,7 @@ rec {
} }
Type: Type:
recursiveUpdate :: AttrSet -> AttrSet -> AttrSet recursiveUpdate :: AttrSet -> AttrSet -> AttrSet
*/ */
recursiveUpdate = recursiveUpdate =
# Left attribute set of the merge. # Left attribute set of the merge.
@ -795,6 +796,7 @@ rec {
/* Turns a list of strings into a human-readable description of those /* Turns a list of strings into a human-readable description of those
strings represented as an attribute path. The result of this function is strings represented as an attribute path. The result of this function is
not intended to be machine-readable. not intended to be machine-readable.
Create a new attribute set with `value` set at the nested attribute location specified in `attrPath`.
Example: Example:
showAttrPath [ "foo" "10" "bar" ] showAttrPath [ "foo" "10" "bar" ]
@ -831,11 +833,11 @@ rec {
If the output does not exist, fallback to `.out` and then to the default. If the output does not exist, fallback to `.out` and then to the default.
Example: Example:
getOutput pkgs.openssl getBin pkgs.openssl
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r" => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r"
Type: Type:
getOutput :: Derivation -> String getBin :: Derivation -> String
*/ */
getBin = getOutput "bin"; getBin = getOutput "bin";
@ -844,11 +846,11 @@ rec {
If the output does not exist, fallback to `.out` and then to the default. If the output does not exist, fallback to `.out` and then to the default.
Example: Example:
getOutput pkgs.openssl getLib pkgs.openssl
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-lib" => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-lib"
Type: Type:
getOutput :: Derivation -> String getLib :: Derivation -> String
*/ */
getLib = getOutput "lib"; getLib = getOutput "lib";
@ -857,11 +859,11 @@ rec {
If the output does not exist, fallback to `.out` and then to the default. If the output does not exist, fallback to `.out` and then to the default.
Example: Example:
getOutput pkgs.openssl getDev pkgs.openssl
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev" => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev"
Type: Type:
getOutput :: Derivation -> String getDev :: Derivation -> String
*/ */
getDev = getOutput "dev"; getDev = getOutput "dev";
@ -870,15 +872,19 @@ rec {
If the output does not exist, fallback to `.out` and then to the default. If the output does not exist, fallback to `.out` and then to the default.
Example: Example:
getOutput pkgs.openssl getMan pkgs.openssl
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-man" => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-man"
Type: Type:
getOutput :: Derivation -> String getMan :: Derivation -> String
*/ */
getMan = getOutput "man"; getMan = getOutput "man";
/* Pick the outputs of packages to place in `buildInputs` */ /* Pick the outputs of packages to place in `buildInputs`
Type: chooseDevOutputs :: [Derivation] -> [String]
*/
chooseDevOutputs = chooseDevOutputs =
# List of packages to pick `dev` outputs from # List of packages to pick `dev` outputs from
drvs: drvs:
@ -900,6 +906,7 @@ rec {
Type: Type:
recurseIntoAttrs :: AttrSet -> AttrSet recurseIntoAttrs :: AttrSet -> AttrSet
*/ */
recurseIntoAttrs = recurseIntoAttrs =
# An attribute set to scan for derivations. # An attribute set to scan for derivations.
@ -909,7 +916,7 @@ rec {
/* Undo the effect of recurseIntoAttrs. /* Undo the effect of recurseIntoAttrs.
Type: Type:
recurseIntoAttrs :: AttrSet -> AttrSet dontRecurseIntoAttrs :: AttrSet -> AttrSet
*/ */
dontRecurseIntoAttrs = dontRecurseIntoAttrs =
# An attribute set to not scan for derivations. # An attribute set to not scan for derivations.
@ -919,7 +926,10 @@ rec {
/* `unionOfDisjoint x y` is equal to `x // y // z` where the /* `unionOfDisjoint x y` is equal to `x // y // z` where the
attrnames in `z` are the intersection of the attrnames in `x` and attrnames in `z` are the intersection of the attrnames in `x` and
`y`, and all values `assert` with an error message. This `y`, and all values `assert` with an error message. This
operator is commutative, unlike (//). */ operator is commutative, unlike (//).
Type: unionOfDisjoint :: AttrSet -> AttrSet -> AttrSet
*/
unionOfDisjoint = x: y: unionOfDisjoint = x: y:
let let
intersection = builtins.intersectAttrs x y; intersection = builtins.intersectAttrs x y;
@ -930,9 +940,10 @@ rec {
in in
(x // y) // mask; (x // y) // mask;
# deprecated # DEPRECATED
zipWithNames = zipAttrsWithNames; zipWithNames = zipAttrsWithNames;
# deprecated
# DEPRECATED
zip = builtins.trace zip = builtins.trace
"lib.zip is deprecated, use lib.zipAttrsWith instead" zipAttrsWith; "lib.zip is deprecated, use lib.zipAttrsWith instead" zipAttrsWith;
} }

View file

@ -104,8 +104,6 @@ rec {
/* Creates an Option attribute set for an option that specifies the /* Creates an Option attribute set for an option that specifies the
package a module should use for some purpose. package a module should use for some purpose.
Type: mkPackageOption :: pkgs -> string -> { default :: [string], example :: null | string | [string] } -> option
The package is specified as a list of strings representing its attribute path in nixpkgs. The package is specified as a list of strings representing its attribute path in nixpkgs.
Because of this, you need to pass nixpkgs itself as the first argument. Because of this, you need to pass nixpkgs itself as the first argument.
@ -116,6 +114,8 @@ rec {
You can omit the default path if the name of the option is also attribute path in nixpkgs. You can omit the default path if the name of the option is also attribute path in nixpkgs.
Type: mkPackageOption :: pkgs -> string -> { default :: [string], example :: null | string | [string] } -> option
Example: Example:
mkPackageOption pkgs "hello" { } mkPackageOption pkgs "hello" { }
=> { _type = "option"; default = «derivation /nix/store/3r2vg51hlxj3cx5vscp0vkv60bqxkaq0-hello-2.10.drv»; defaultText = { ... }; description = "The hello package to use."; type = { ... }; } => { _type = "option"; default = «derivation /nix/store/3r2vg51hlxj3cx5vscp0vkv60bqxkaq0-hello-2.10.drv»; defaultText = { ... }; description = "The hello package to use."; type = { ... }; }