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