2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Some functions for manipulating meta attributes, as well as the
|
|
|
|
name attribute.
|
|
|
|
*/
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2017-07-28 20:05:35 -04:00
|
|
|
{ lib }:
|
2013-12-26 04:22:29 +02:00
|
|
|
|
lib.meta: Avoid attrset allocation in platformMatch
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
}
}
```
2023-11-25 01:58:52 +13:00
|
|
|
let
|
2025-04-01 20:10:43 +02:00
|
|
|
inherit (lib)
|
|
|
|
matchAttrs
|
|
|
|
any
|
|
|
|
all
|
|
|
|
isDerivation
|
|
|
|
getBin
|
|
|
|
assertMsg
|
|
|
|
;
|
2024-08-25 00:57:44 +02:00
|
|
|
inherit (lib.attrsets) mapAttrs' filterAttrs;
|
2023-12-12 16:42:45 +13:00
|
|
|
inherit (builtins) isString match typeOf;
|
lib.meta: Avoid attrset allocation in platformMatch
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
}
}
```
2023-11-25 01:58:52 +13:00
|
|
|
|
|
|
|
in
|
2009-03-30 13:22:19 +00:00
|
|
|
rec {
|
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Add to or override the meta attributes of the given
|
|
|
|
derivation.
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`newAttrs`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.addMetaAttrs` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
addMetaAttrs {description = "Bla blah";} somePkg
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2009-03-30 13:22:19 +00:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
addMetaAttrs = newAttrs: drv: drv // { meta = (drv.meta or { }) // newAttrs; };
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Disable Hydra builds of given derivation.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2017-05-27 20:44:00 +03:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
dontDistribute = drv: addMetaAttrs { hydraPlatforms = [ ]; } drv;
|
2017-05-27 20:44:00 +03:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Change the [symbolic name of a derivation](https://nixos.org/manual/nix/stable/language/derivations.html#attr-name).
|
|
|
|
|
|
|
|
:::{.warning}
|
|
|
|
Dependent derivations will be rebuilt when the symbolic name is changed.
|
|
|
|
:::
|
2024-05-15 05:26:35 -04:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`name`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
2009-03-30 13:22:19 +00:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
setName = name: drv: drv // { inherit name; };
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Like `setName`, but takes the previous name as an argument.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`updater`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.updateName` usage example
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
```nix
|
|
|
|
updateName (oldName: oldName + "-experimental") somePkg
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2009-03-30 13:22:19 +00:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
updateName = updater: drv: drv // { name = updater (drv.name); };
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Append a suffix to the name of a package (before the version
|
|
|
|
part).
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`suffix`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
appendToName =
|
|
|
|
suffix:
|
|
|
|
updateName (
|
|
|
|
name:
|
|
|
|
let
|
|
|
|
x = builtins.parseDrvName name;
|
|
|
|
in
|
|
|
|
"${x.name}-${suffix}-${x.version}"
|
|
|
|
);
|
2009-03-30 13:22:19 +00:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Apply a function to each derivation and only to derivations in an attrset.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`f`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`set`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
2013-12-26 04:22:29 +02:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
mapDerivationAttrset =
|
|
|
|
f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;
|
2013-12-26 04:22:29 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
2024-08-29 09:04:57 -07:00
|
|
|
The default priority of packages in Nix. See `defaultPriority` in [`src/nix/profile.cc`](https://github.com/NixOS/nix/blob/master/src/nix/profile.cc#L47).
|
2025-04-01 20:10:43 +02:00
|
|
|
*/
|
2024-08-29 09:04:57 -07:00
|
|
|
defaultPriority = 5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Set the nix-env priority of the package. Note that higher values are lower priority, and vice versa.
|
2024-06-26 22:12:18 +02:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`priority`
|
2024-08-29 09:04:57 -07:00
|
|
|
: 1\. The priority to set.
|
2024-06-26 22:12:18 +02:00
|
|
|
|
|
|
|
`drv`
|
|
|
|
: 2\. Function argument
|
2018-11-18 08:26:13 +00:00
|
|
|
*/
|
|
|
|
setPrio = priority: addMetaAttrs { inherit priority; };
|
2013-12-26 04:22:29 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Decrease the nix-env priority of the package, i.e., other
|
|
|
|
versions/variants of the package will be preferred.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2009-03-30 13:22:19 +00:00
|
|
|
*/
|
2018-11-18 08:26:13 +00:00
|
|
|
lowPrio = setPrio 10;
|
2013-12-26 04:22:29 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
2024-08-29 09:04:57 -07:00
|
|
|
Apply lowPrio to an attrset with derivations.
|
2024-06-26 22:12:18 +02:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`set`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2013-12-26 04:22:29 +02:00
|
|
|
*/
|
2014-01-20 14:54:50 +01:00
|
|
|
lowPrioSet = set: mapDerivationAttrset lowPrio set;
|
2013-12-26 04:22:29 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Increase the nix-env priority of the package, i.e., this
|
|
|
|
version/variant of the package will be preferred.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2010-07-22 08:59:46 +00:00
|
|
|
*/
|
2018-11-18 08:26:13 +00:00
|
|
|
hiPrio = setPrio (-10);
|
2013-12-26 04:22:29 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
2024-08-29 09:04:57 -07:00
|
|
|
Apply hiPrio to an attrset with derivations.
|
2024-06-26 22:12:18 +02:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`set`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2013-12-26 04:22:29 +02:00
|
|
|
*/
|
|
|
|
hiPrioSet = set: mapDerivationAttrset hiPrio set;
|
2014-01-20 14:54:50 +01:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
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`).
|
2018-03-19 18:03:46 -04:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
We can inject these into a pattern for the whole of a structured platform,
|
|
|
|
and then match that.
|
2018-03-19 18:03:46 -04:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
# Inputs
|
2023-01-27 02:19:30 -08:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
`platform`
|
2018-03-19 18:03:46 -04:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
: 1\. Function argument
|
2024-02-27 14:48:53 -05:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
`elem`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.platformMatch` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
lib.meta.platformMatch { system = "aarch64-darwin"; } "aarch64-darwin"
|
|
|
|
=> true
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2018-03-19 18:03:46 -04:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
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
|
|
|
|
);
|
2021-02-26 00:21:13 +08:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
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.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`platform`
|
2021-02-26 00:21:13 +08:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
: 1\. Function argument
|
2021-02-26 00:21:13 +08:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
`pkg`
|
2021-02-26 00:21:13 +08:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
: 2\. Function argument
|
2024-02-27 12:31:58 -05:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.availableOn` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
lib.meta.availableOn { system = "aarch64-darwin"; } pkg.zsh
|
|
|
|
=> true
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2021-02-26 00:21:13 +08:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
availableOn =
|
|
|
|
platform: pkg:
|
Revert "lib.meta.availableOn: Return false if pkg parameter is null"
I believe this change is wrong both theoretically and practically.
Theoretically, `null` is available on every platform, because
`buildInputs = [ null ];` always succeeds and never throws a platform
availability error. `null` should be handled consistently with packages
that have no explicit list of supported platforms, as it of course
has no such list itself.
Practically, we use `null` to represent libraries that are always
present on a platform and do not require a library (for instance,
because they are part of `libc` or the macOS SDK). This has been
used for a long time by `libintl` (on all non‐glibc platforms),
and is also now used by `libGL` and friends on Darwin. This change
broke the check SDL3 does for OpenGL availability on Darwin, causing
<https://github.com/NixOS/nixpkgs/issues/407056>, which had to be
worked around by <https://github.com/NixOS/nixpkgs/pull/409525>.
Both `libintl` and `libGL` should count as available on platforms
where their functionality is part of the standard build environment,
and a package that is completely unavailable and whose functionality
cannot be expected should not use `null`, as it should result in
errors if used in a dependency list on an unsupported platform.
I accept that overriding with `null` is often a useful way to disable
dependencies that don’t have explicit feature flags, but I do not
think that making it work better with feature flags conditioned on
availability is worth the inconsistency and problems caused by this
change. Packages can instead expose the relevant feature flags as
arguments that default to the `lib.meta.availableOn` check or, if they
want to keep an “override the dependency to `null`” interface,
insert an explicit `pkg != null && …` check.
Additionally, the pull request was merged over a week after all
breaking changes were restricted for the 25.05 release. I believe that
the potential problems of dealing with the effects of this change for
an entire release cycle – the first release cycle where `libGL` is
`null` on Darwin, a change I made before the deadline and before this
change to `lib.meta.availableOn` – offset the risks of backporting
this revert at such a late stage.
It will cause overrides to backwards‐incompatibly revert to the
behaviour they had before the change, but since such overrides were
not possible until a few weeks ago, I hope that is an acceptable risk
compared to the potential issues leaving this in the release can
cause, given that it was merged after the deadline and has already
broken an existing construction in Nixpkgs.
This reverts commit 9338d924dbe0c6b93daec3bf435322812fd176fe.
2025-05-23 14:58:13 +01:00
|
|
|
((!pkg ? meta.platforms) || any (platformMatch platform) pkg.meta.platforms)
|
2025-04-01 20:10:43 +02:00
|
|
|
&& all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or [ ]);
|
2021-10-11 07:08:40 +08:00
|
|
|
|
2024-08-11 19:06:54 +02:00
|
|
|
/**
|
|
|
|
Mapping of SPDX ID to the attributes in lib.licenses.
|
|
|
|
|
2024-08-25 00:57:44 +02:00
|
|
|
For SPDX IDs, see https://spdx.org/licenses.
|
|
|
|
Note that some SPDX licenses might be missing.
|
2024-08-11 19:06:54 +02:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.licensesSpdx` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
lib.licensesSpdx.MIT == lib.licenses.mit
|
|
|
|
=> true
|
|
|
|
lib.licensesSpdx."MY LICENSE"
|
|
|
|
=> error: attribute 'MY LICENSE' missing
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
licensesSpdx = mapAttrs' (_key: license: {
|
|
|
|
name = license.spdxId;
|
|
|
|
value = license;
|
|
|
|
}) (filterAttrs (_key: license: license ? spdxId) lib.licenses);
|
2024-08-11 19:06:54 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
2024-06-30 02:27:03 +08:00
|
|
|
Get the corresponding attribute in lib.licenses from the SPDX ID
|
|
|
|
or warn and fallback to `{ shortName = <license string>; }`.
|
|
|
|
|
2024-08-25 00:57:44 +02:00
|
|
|
For SPDX IDs, see https://spdx.org/licenses.
|
|
|
|
Note that some SPDX licenses might be missing.
|
2024-06-26 22:12:18 +02:00
|
|
|
|
|
|
|
# Type
|
|
|
|
|
|
|
|
```
|
|
|
|
getLicenseFromSpdxId :: str -> AttrSet
|
|
|
|
```
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.getLicenseFromSpdxId` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
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"; }
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2021-10-11 07:08:40 +08:00
|
|
|
*/
|
|
|
|
getLicenseFromSpdxId =
|
2024-06-26 08:42:44 +08:00
|
|
|
licstr:
|
2025-04-01 20:10:43 +02:00
|
|
|
getLicenseFromSpdxIdOr licstr (
|
|
|
|
lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}" {
|
|
|
|
shortName = licstr;
|
|
|
|
}
|
|
|
|
);
|
2022-04-05 01:10:44 +03:00
|
|
|
|
2024-06-26 08:42:44 +08:00
|
|
|
/**
|
|
|
|
Get the corresponding attribute in lib.licenses from the SPDX ID
|
|
|
|
or fallback to the given default value.
|
|
|
|
|
2024-08-25 00:57:44 +02:00
|
|
|
For SPDX IDs, see https://spdx.org/licenses.
|
|
|
|
Note that some SPDX licenses might be missing.
|
2024-06-26 08:42:44 +08:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`licstr`
|
|
|
|
: 1\. SPDX ID string to find a matching license
|
|
|
|
|
|
|
|
`default`
|
|
|
|
: 2\. Fallback value when a match is not found
|
|
|
|
|
|
|
|
# Type
|
|
|
|
|
|
|
|
```
|
|
|
|
getLicenseFromSpdxIdOr :: str -> Any -> Any
|
|
|
|
```
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.getLicenseFromSpdxIdOr` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
lib.getLicenseFromSpdxIdOr "MIT" null == lib.licenses.mit
|
|
|
|
=> true
|
|
|
|
lib.getLicenseFromSpdxId "mIt" null == lib.licenses.mit
|
|
|
|
=> true
|
|
|
|
lib.getLicenseFromSpdxIdOr "MY LICENSE" lib.licenses.free == lib.licenses.free
|
|
|
|
=> true
|
|
|
|
lib.getLicenseFromSpdxIdOr "MY LICENSE" null
|
|
|
|
=> null
|
|
|
|
lib.getLicenseFromSpdxIdOr "MY LICENSE" (builtins.throw "No SPDX ID matches MY LICENSE")
|
|
|
|
=> error: No SPDX ID matches MY LICENSE
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
*/
|
|
|
|
getLicenseFromSpdxIdOr =
|
|
|
|
let
|
2024-08-11 19:06:54 +02:00
|
|
|
lowercaseLicenses = lib.mapAttrs' (name: value: {
|
|
|
|
name = lib.toLower name;
|
|
|
|
inherit value;
|
|
|
|
}) licensesSpdx;
|
2025-04-01 20:10:43 +02:00
|
|
|
in
|
|
|
|
licstr: default: lowercaseLicenses.${lib.toLower licstr} or default;
|
2024-06-26 08:42:44 +08:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Get the path to the main program of a package based on meta.mainProgram
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`x`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
# Type
|
|
|
|
|
|
|
|
```
|
|
|
|
getExe :: package -> string
|
|
|
|
```
|
2022-04-05 01:10:44 +03:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.getExe` usage example
|
2022-04-05 01:10:44 +03:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
```nix
|
|
|
|
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"
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2022-04-05 01:10:44 +03:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
getExe =
|
|
|
|
x:
|
|
|
|
getExe' x (
|
|
|
|
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
|
|
|
|
)
|
|
|
|
);
|
2023-08-13 12:27:41 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
/**
|
|
|
|
Get the path of a program of a derivation.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`x`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`y`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
|
|
|
|
|
|
|
# Type
|
|
|
|
|
|
|
|
```
|
|
|
|
getExe' :: derivation -> string -> string
|
|
|
|
```
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.getExe'` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
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"
|
|
|
|
```
|
2023-08-13 12:27:41 +02:00
|
|
|
|
2024-06-26 22:12:18 +02:00
|
|
|
:::
|
2023-08-13 12:27:41 +02:00
|
|
|
*/
|
2025-04-01 20:10:43 +02:00
|
|
|
getExe' =
|
|
|
|
x: y:
|
2023-12-12 16:42:45 +13:00
|
|
|
assert assertMsg (isDerivation x)
|
|
|
|
"lib.meta.getExe': The first argument is of type ${typeOf x}, but it should be a derivation instead.";
|
|
|
|
assert assertMsg (isString y)
|
|
|
|
"lib.meta.getExe': The second argument is of type ${typeOf y}, but it should be a string instead.";
|
2024-12-23 21:58:07 +01:00
|
|
|
assert assertMsg (match ".*/.*" y == null)
|
2023-12-12 16:42:45 +13:00
|
|
|
"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.";
|
|
|
|
"${getBin x}/bin/${y}";
|
2009-03-30 13:22:19 +00:00
|
|
|
}
|