Merge pull request #274022 from hercules-ci/doc-attrsets-operators

lib/attrsets: Document and link Nix language operators

authored by Silvan Mosberger and committed by GitHub 5027eafc a931a220

+24
+24
lib/attrsets.nix
··· 14 15 /* Return an attribute from nested attribute sets. 16 17 Example: 18 x = { a = { b = 3; }; } 19 # ["a" "b"] is equivalent to x.a.b ··· 50 attrByPath' 0 set; 51 52 /* Return if an attribute from nested attribute set exists. 53 54 **Laws**: 55 1. ```nix ··· 176 177 /* Like `attrByPath`, but without a default value. If it doesn't find the 178 path it will throw an error. 179 180 Example: 181 x = { a = { b = 3; }; }
··· 14 15 /* Return an attribute from nested attribute sets. 16 17 + Nix has an [attribute selection operator `. or`](https://nixos.org/manual/nix/stable/language/operators#attribute-selection) which is sufficient for such queries, as long as the number of attributes is static. For example: 18 + 19 + ```nix 20 + (x.a.b or 6) == attrByPath ["a" "b"] 6 x 21 + # and 22 + (x.${f p}."example.com" or 6) == attrByPath [ (f p) "example.com" ] 6 x 23 + ``` 24 + 25 Example: 26 x = { a = { b = 3; }; } 27 # ["a" "b"] is equivalent to x.a.b ··· 58 attrByPath' 0 set; 59 60 /* Return if an attribute from nested attribute set exists. 61 + 62 + Nix has a [has attribute operator `?`](https://nixos.org/manual/nix/stable/language/operators#has-attribute), which is sufficient for such queries, as long as the number of attributes is static. For example: 63 + 64 + ```nix 65 + (x?a.b) == hasAttryByPath ["a" "b"] x 66 + # and 67 + (x?${f p}."example.com") == hasAttryByPath [ (f p) "example.com" ] x 68 + ``` 69 70 **Laws**: 71 1. ```nix ··· 192 193 /* Like `attrByPath`, but without a default value. If it doesn't find the 194 path it will throw an error. 195 + 196 + Nix has an [attribute selection operator](https://nixos.org/manual/nix/stable/language/operators#attribute-selection) which is sufficient for such queries, as long as the number of attributes is static. For example: 197 + 198 + ```nix 199 + x.a.b == getAttrByPath ["a" "b"] x 200 + # and 201 + x.${f p}."example.com" == getAttrByPath [ (f p) "example.com" ] x 202 + ``` 203 204 Example: 205 x = { a = { b = 3; }; }