Merge pull request #262301 from ShamrockLee/doc-lib-customisation

Generate and refine documentation for `lib.customisation`

authored by

Silvan Mosberger and committed by
GitHub
200aa036 32e8db24

+70 -19
+1
doc/default.nix
··· 23 23 { name = "sources"; description = "source filtering functions"; } 24 24 { name = "cli"; description = "command-line serialization functions"; } 25 25 { name = "gvariant"; description = "GVariant formatted string serialization functions"; } 26 + { name = "customisation"; description = "Functions to customise (derivation-related) functions, derivatons, or attribute sets"; } 26 27 ]; 27 28 }; 28 29
+69 -19
lib/customisation.nix
··· 13 13 scenarios (e.g. in ~/.config/nixpkgs/config.nix). For instance, 14 14 if you want to "patch" the derivation returned by a package 15 15 function in Nixpkgs to build another version than what the 16 - function itself provides, you can do something like this: 17 - 18 - mySed = overrideDerivation pkgs.gnused (oldAttrs: { 19 - name = "sed-4.2.2-pre"; 20 - src = fetchurl { 21 - url = ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2; 22 - hash = "sha256-MxBJRcM2rYzQYwJ5XKxhXTQByvSg5jZc5cSHEZoB2IY="; 23 - }; 24 - patches = []; 25 - }); 16 + function itself provides. 26 17 27 18 For another application, see build-support/vm, where this 28 19 function is used to build arbitrary derivations inside a QEMU ··· 35 26 36 27 You should in general prefer `drv.overrideAttrs` over this function; 37 28 see the nixpkgs manual for more information on overriding. 29 + 30 + Example: 31 + mySed = overrideDerivation pkgs.gnused (oldAttrs: { 32 + name = "sed-4.2.2-pre"; 33 + src = fetchurl { 34 + url = ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2; 35 + hash = "sha256-MxBJRcM2rYzQYwJ5XKxhXTQByvSg5jZc5cSHEZoB2IY="; 36 + }; 37 + patches = []; 38 + }); 39 + 40 + Type: 41 + overrideDerivation :: Derivation -> ( Derivation -> AttrSet ) -> Derivation 38 42 */ 39 43 overrideDerivation = drv: f: 40 44 let ··· 55 59 injects `override` attribute which can be used to override arguments of 56 60 the function. 57 61 62 + Please refer to documentation on [`<pkg>.overrideDerivation`](#sec-pkg-overrideDerivation) to learn about `overrideDerivation` and caveats 63 + related to its use. 64 + 65 + Example: 58 66 nix-repl> x = {a, b}: { result = a + b; } 59 67 60 68 nix-repl> y = lib.makeOverridable x { a = 1; b = 2; } ··· 65 73 nix-repl> y.override { a = 10; } 66 74 { override = «lambda»; overrideDerivation = «lambda»; result = 12; } 67 75 68 - Please refer to "Nixpkgs Contributors Guide" section 69 - "<pkg>.overrideDerivation" to learn about `overrideDerivation` and caveats 70 - related to its use. 76 + Type: 77 + makeOverridable :: (AttrSet -> a) -> AttrSet -> a 71 78 */ 72 79 makeOverridable = f: lib.setFunctionArgs 73 80 (origArgs: let ··· 105 112 `autoArgs`. This function is intended to be partially 106 113 parameterised, e.g., 107 114 115 + ```nix 108 116 callPackage = callPackageWith pkgs; 109 117 pkgs = { 110 118 libfoo = callPackage ./foo.nix { }; 111 119 libbar = callPackage ./bar.nix { }; 112 120 }; 121 + ``` 113 122 114 123 If the `libbar` function expects an argument named `libfoo`, it is 115 124 automatically passed as an argument. Overrides or missing 116 125 arguments can be supplied in `args`, e.g. 117 126 127 + ```nix 118 128 libbar = callPackage ./bar.nix { 119 129 libfoo = null; 120 130 enableX11 = true; 121 131 }; 132 + ``` 133 + 134 + <!-- TODO: Apply "Example:" tag to the examples above --> 135 + 136 + Type: 137 + callPackageWith :: AttrSet -> ((AttrSet -> a) | Path) -> AttrSet -> a 122 138 */ 123 139 callPackageWith = autoArgs: fn: args: 124 140 let ··· 129 145 # This includes automatic ones and ones passed explicitly 130 146 allArgs = builtins.intersectAttrs fargs autoArgs // args; 131 147 132 - # A list of argument names that the function requires, but 148 + # a list of argument names that the function requires, but 133 149 # wouldn't be passed to it 134 150 missingArgs = lib.attrNames 135 151 # Filter out arguments that have a default value ··· 176 192 177 193 /* Like callPackage, but for a function that returns an attribute 178 194 set of derivations. The override function is added to the 179 - individual attributes. */ 195 + individual attributes. 196 + 197 + Type: 198 + callPackagesWith :: AttrSet -> ((AttrSet -> AttrSet) | Path) -> AttrSet -> AttrSet 199 + */ 180 200 callPackagesWith = autoArgs: fn: args: 181 201 let 182 202 f = if lib.isFunction fn then fn else import fn; ··· 193 213 194 214 195 215 /* Add attributes to each output of a derivation without changing 196 - the derivation itself and check a given condition when evaluating. */ 216 + the derivation itself and check a given condition when evaluating. 217 + 218 + Type: 219 + extendDerivation :: Bool -> Any -> Derivation -> Derivation 220 + */ 197 221 extendDerivation = condition: passthru: drv: 198 222 let 199 223 outputs = drv.outputs or [ "out" ]; ··· 227 251 /* Strip a derivation of all non-essential attributes, returning 228 252 only those needed by hydra-eval-jobs. Also strictly evaluate the 229 253 result to ensure that there are no thunks kept alive to prevent 230 - garbage collection. */ 254 + garbage collection. 255 + 256 + Type: 257 + hydraJob :: (Derivation | Null) -> (Derivation | Null) 258 + */ 231 259 hydraJob = drv: 232 260 let 233 261 outputs = drv.outputs or ["out"]; ··· 265 293 called with the overridden packages. The package sets may be 266 294 hierarchical: the packages in the set are called with the scope 267 295 provided by `newScope` and the set provides a `newScope` attribute 268 - which can form the parent scope for later package sets. */ 296 + which can form the parent scope for later package sets. 297 + 298 + Type: 299 + makeScope :: (AttrSet -> ((AttrSet -> a) | Path) -> AttrSet -> a) -> (AttrSet -> AttrSet) -> AttrSet 300 + */ 269 301 makeScope = newScope: f: 270 302 let self = f self // { 271 303 newScope = scope: newScope (self // scope); ··· 287 319 { inherit otherSplices keep extra f; }; 288 320 289 321 /* Like makeScope, but aims to support cross compilation. It's still ugly, but 290 - hopefully it helps a little bit. */ 322 + hopefully it helps a little bit. 323 + 324 + Type: 325 + makeScopeWithSplicing' :: 326 + { splicePackages :: Splice -> AttrSet 327 + , newScope :: AttrSet -> ((AttrSet -> a) | Path) -> AttrSet -> a 328 + } 329 + -> { otherSplices :: Splice, keep :: AttrSet -> AttrSet, extra :: AttrSet -> AttrSet } 330 + -> AttrSet 331 + 332 + Splice :: 333 + { pkgsBuildBuild :: AttrSet 334 + , pkgsBuildHost :: AttrSet 335 + , pkgsBuildTarget :: AttrSet 336 + , pkgsHostHost :: AttrSet 337 + , pkgsHostTarget :: AttrSet 338 + , pkgsTargetTarget :: AttrSet 339 + } 340 + */ 291 341 makeScopeWithSplicing' = 292 342 { splicePackages 293 343 , newScope