mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-24 16:03:23 +00:00
4b4d413817
Benchmarks (`nix-instantiate ./. -A python3`)
- Before
``` json
{
"cpuTime": 0.30625399947166443,
"envs": {
"bytes": 4484216,
"elements": 221443,
"number": 169542
},
"gc": {
"heapSize": 402915328,
"totalBytes": 53091024
},
"list": {
"bytes": 749424,
"concats": 4242,
"elements": 93678
},
"nrAvoided": 253991,
"nrFunctionCalls": 149848,
"nrLookups": 49614,
"nrOpUpdateValuesCopied": 1588326,
"nrOpUpdates": 10106,
"nrPrimOpCalls": 130356,
"nrThunks": 359013,
"sets": {
"bytes": 30432320,
"elements": 1860540,
"number": 41480
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236218,
"number": 24459
},
"values": {
"bytes": 10504632,
"number": 437693
}
}
```
- After
```
{
"cpuTime": 0.29695799946784973,
"envs": {
"bytes": 3296712,
"elements": 169055,
"number": 121517
},
"gc": {
"heapSize": 402915328,
"totalBytes": 49044992
},
"list": {
"bytes": 504928,
"concats": 4242,
"elements": 63116
},
"nrAvoided": 175403,
"nrFunctionCalls": 110554,
"nrLookups": 44907,
"nrOpUpdateValuesCopied": 1588326,
"nrOpUpdates": 10106,
"nrPrimOpCalls": 82330,
"nrThunks": 306625,
"sets": {
"bytes": 29943328,
"elements": 1843076,
"number": 28382
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236218,
"number": 24459
},
"values": {
"bytes": 9037752
,
"number": 376573
}
}
```
186 lines
6.5 KiB
Nix
186 lines
6.5 KiB
Nix
/* Some functions for manipulating meta attributes, as well as the
|
|
name attribute. */
|
|
|
|
{ lib }:
|
|
|
|
let
|
|
inherit (lib) matchAttrs any all;
|
|
inherit (builtins) isString;
|
|
|
|
in
|
|
rec {
|
|
|
|
|
|
/* Add to or override the meta attributes of the given
|
|
derivation.
|
|
|
|
Example:
|
|
addMetaAttrs {description = "Bla blah";} somePkg
|
|
*/
|
|
addMetaAttrs = newAttrs: drv:
|
|
drv // { meta = (drv.meta or {}) // newAttrs; };
|
|
|
|
|
|
/* Disable Hydra builds of given derivation.
|
|
*/
|
|
dontDistribute = drv: addMetaAttrs { hydraPlatforms = []; } drv;
|
|
|
|
|
|
/* Change the symbolic name of a package for presentation purposes
|
|
(i.e., so that nix-env users can tell them apart).
|
|
*/
|
|
setName = name: drv: drv // {inherit name;};
|
|
|
|
|
|
/* Like `setName`, but takes the previous name as an argument.
|
|
|
|
Example:
|
|
updateName (oldName: oldName + "-experimental") somePkg
|
|
*/
|
|
updateName = updater: drv: drv // {name = updater (drv.name);};
|
|
|
|
|
|
/* Append a suffix to the name of a package (before the version
|
|
part). */
|
|
appendToName = suffix: updateName (name:
|
|
let x = builtins.parseDrvName name; in "${x.name}-${suffix}-${x.version}");
|
|
|
|
|
|
/* Apply a function to each derivation and only to derivations in an attrset.
|
|
*/
|
|
mapDerivationAttrset = f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;
|
|
|
|
/* Set the nix-env priority of the package.
|
|
*/
|
|
setPrio = priority: addMetaAttrs { inherit priority; };
|
|
|
|
/* Decrease the nix-env priority of the package, i.e., other
|
|
versions/variants of the package will be preferred.
|
|
*/
|
|
lowPrio = setPrio 10;
|
|
|
|
/* Apply lowPrio to an attrset with derivations
|
|
*/
|
|
lowPrioSet = set: mapDerivationAttrset lowPrio set;
|
|
|
|
|
|
/* Increase the nix-env priority of the package, i.e., this
|
|
version/variant of the package will be preferred.
|
|
*/
|
|
hiPrio = setPrio (-10);
|
|
|
|
/* Apply hiPrio to an attrset with derivations
|
|
*/
|
|
hiPrioSet = set: mapDerivationAttrset hiPrio set;
|
|
|
|
|
|
/* Check to see if a platform is matched by the given `meta.platforms`
|
|
element.
|
|
|
|
A `meta.platform` pattern is either
|
|
|
|
1. (legacy) a system string.
|
|
|
|
2. (modern) a pattern for the entire platform structure (see `lib.systems.inspect.platformPatterns`).
|
|
|
|
3. (modern) a pattern for the platform `parsed` field (see `lib.systems.inspect.patterns`).
|
|
|
|
We can inject these into a pattern for the whole of a structured platform,
|
|
and then match that.
|
|
*/
|
|
platformMatch = platform: elem: (
|
|
# Check with simple string comparison if elem was a string.
|
|
#
|
|
# The majority of comparisons done with this function will be against meta.platforms
|
|
# which contains a simple platform string.
|
|
#
|
|
# Avoiding an attrset allocation results in significant performance gains (~2-30) across the board in OfBorg
|
|
# because this is a hot path for nixpkgs.
|
|
if isString elem then platform ? system && elem == platform.system
|
|
else matchAttrs (
|
|
# Normalize platform attrset.
|
|
if elem ? parsed then elem
|
|
else { parsed = elem; }
|
|
) platform
|
|
);
|
|
|
|
/* Check if a package is available on a given platform.
|
|
|
|
A package is available on a platform if both
|
|
|
|
1. One of `meta.platforms` pattern matches the given
|
|
platform, or `meta.platforms` is not present.
|
|
|
|
2. None of `meta.badPlatforms` pattern matches the given platform.
|
|
*/
|
|
availableOn = platform: pkg:
|
|
((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) &&
|
|
all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
|
|
|
|
/* Get the corresponding attribute in lib.licenses
|
|
from the SPDX ID.
|
|
For SPDX IDs, see
|
|
https://spdx.org/licenses
|
|
|
|
Type:
|
|
getLicenseFromSpdxId :: str -> AttrSet
|
|
|
|
Example:
|
|
lib.getLicenseFromSpdxId "MIT" == lib.licenses.mit
|
|
=> true
|
|
lib.getLicenseFromSpdxId "mIt" == lib.licenses.mit
|
|
=> true
|
|
lib.getLicenseFromSpdxId "MY LICENSE"
|
|
=> trace: warning: getLicenseFromSpdxId: No license matches the given SPDX ID: MY LICENSE
|
|
=> { shortName = "MY LICENSE"; }
|
|
*/
|
|
getLicenseFromSpdxId =
|
|
let
|
|
spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls)
|
|
(lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses)));
|
|
in licstr:
|
|
spdxLicenses.${ lib.toLower licstr } or (
|
|
lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
|
|
{ shortName = licstr; }
|
|
);
|
|
|
|
/* Get the path to the main program of a package based on meta.mainProgram
|
|
|
|
Type: getExe :: package -> string
|
|
|
|
Example:
|
|
getExe pkgs.hello
|
|
=> "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello"
|
|
getExe pkgs.mustache-go
|
|
=> "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache"
|
|
*/
|
|
getExe = x:
|
|
let
|
|
y = x.meta.mainProgram or (
|
|
# This could be turned into an error when 23.05 is at end of life
|
|
lib.warn "getExe: Package ${lib.strings.escapeNixIdentifier x.meta.name or x.pname or x.name} does not have the meta.mainProgram attribute. We'll assume that the main program has the same name for now, but this behavior is deprecated, because it leads to surprising errors when the assumption does not hold. If the package has a main program, please set `meta.mainProgram` in its definition to make this warning go away. Otherwise, if the package does not have a main program, or if you don't control its definition, use getExe' to specify the name to the program, such as lib.getExe' foo \"bar\"."
|
|
lib.getName
|
|
x
|
|
);
|
|
in
|
|
getExe' x y;
|
|
|
|
/* Get the path of a program of a derivation.
|
|
|
|
Type: getExe' :: derivation -> string -> string
|
|
Example:
|
|
getExe' pkgs.hello "hello"
|
|
=> "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello"
|
|
getExe' pkgs.imagemagick "convert"
|
|
=> "/nix/store/5rs48jamq7k6sal98ymj9l4k2bnwq515-imagemagick-7.1.1-15/bin/convert"
|
|
*/
|
|
getExe' = x: y:
|
|
assert lib.assertMsg (lib.isDerivation x)
|
|
"lib.meta.getExe': The first argument is of type ${builtins.typeOf x}, but it should be a derivation instead.";
|
|
assert lib.assertMsg (lib.isString y)
|
|
"lib.meta.getExe': The second argument is of type ${builtins.typeOf y}, but it should be a string instead.";
|
|
assert lib.assertMsg (builtins.length (lib.splitString "/" y) == 1)
|
|
"lib.meta.getExe': The second argument \"${y}\" is a nested path with a \"/\" character, but it should just be the name of the executable instead.";
|
|
"${lib.getBin x}/bin/${y}";
|
|
}
|