2009-03-30 13:22:19 +00:00
|
|
|
/* Some functions for manipulating meta attributes, as well as the
|
|
|
|
name attribute. */
|
|
|
|
|
2017-07-29 00:05:35 +00:00
|
|
|
{ lib }:
|
2013-12-26 02:22:29 +00:00
|
|
|
|
2009-03-30 13:22:19 +00:00
|
|
|
rec {
|
|
|
|
|
|
|
|
|
|
|
|
/* Add to or override the meta attributes of the given
|
|
|
|
derivation.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
addMetaAttrs {description = "Bla blah";} somePkg
|
|
|
|
*/
|
|
|
|
addMetaAttrs = newAttrs: drv:
|
2014-01-20 13:54:50 +00:00
|
|
|
drv // { meta = (drv.meta or {}) // newAttrs; };
|
2009-03-30 13:22:19 +00:00
|
|
|
|
|
|
|
|
2017-05-27 17:44:00 +00:00
|
|
|
/* Disable Hydra builds of given derivation.
|
|
|
|
*/
|
|
|
|
dontDistribute = drv: addMetaAttrs { hydraPlatforms = []; } drv;
|
|
|
|
|
|
|
|
|
2009-03-30 13:22:19 +00:00
|
|
|
/* 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;};
|
|
|
|
|
|
|
|
|
2023-01-01 21:51:54 +00:00
|
|
|
/* Like `setName`, but takes the previous name as an argument.
|
2009-03-30 13:22:19 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
updateName (oldName: oldName + "-experimental") somePkg
|
|
|
|
*/
|
|
|
|
updateName = updater: drv: drv // {name = updater (drv.name);};
|
|
|
|
|
|
|
|
|
2014-03-10 14:01:56 +00:00
|
|
|
/* 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}");
|
2009-03-30 13:22:19 +00:00
|
|
|
|
|
|
|
|
2018-11-18 08:26:13 +00:00
|
|
|
/* Apply a function to each derivation and only to derivations in an attrset.
|
2013-12-26 02:22:29 +00:00
|
|
|
*/
|
|
|
|
mapDerivationAttrset = f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;
|
|
|
|
|
2018-11-18 08:26:13 +00:00
|
|
|
/* Set the nix-env priority of the package.
|
|
|
|
*/
|
|
|
|
setPrio = priority: addMetaAttrs { inherit priority; };
|
2013-12-26 02:22:29 +00:00
|
|
|
|
2009-03-30 13:22:19 +00:00
|
|
|
/* Decrease the nix-env priority of the package, i.e., other
|
|
|
|
versions/variants of the package will be preferred.
|
|
|
|
*/
|
2018-11-18 08:26:13 +00:00
|
|
|
lowPrio = setPrio 10;
|
2013-12-26 02:22:29 +00:00
|
|
|
|
|
|
|
/* Apply lowPrio to an attrset with derivations
|
|
|
|
*/
|
2014-01-20 13:54:50 +00:00
|
|
|
lowPrioSet = set: mapDerivationAttrset lowPrio set;
|
2013-12-26 02:22:29 +00:00
|
|
|
|
|
|
|
|
2010-07-22 08:59:46 +00:00
|
|
|
/* Increase the nix-env priority of the package, i.e., this
|
|
|
|
version/variant of the package will be preferred.
|
|
|
|
*/
|
2018-11-18 08:26:13 +00:00
|
|
|
hiPrio = setPrio (-10);
|
2013-12-26 02:22:29 +00:00
|
|
|
|
|
|
|
/* Apply hiPrio to an attrset with derivations
|
|
|
|
*/
|
|
|
|
hiPrioSet = set: mapDerivationAttrset hiPrio set;
|
2014-01-20 13:54:50 +00:00
|
|
|
|
2018-03-19 22:03:46 +00: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.
|
|
|
|
|
2023-01-27 11:16:19 +00:00
|
|
|
2. (modern) a pattern for the entire platform structure (see `lib.systems.inspect.platformPatterns`).
|
2023-01-27 10:19:30 +00:00
|
|
|
|
2023-01-27 11:16:29 +00:00
|
|
|
3. (modern) a pattern for the platform `parsed` field (see `lib.systems.inspect.patterns`).
|
2018-03-19 22:03:46 +00:00
|
|
|
|
2022-01-24 15:45:43 +00:00
|
|
|
We can inject these into a pattern for the whole of a structured platform,
|
2018-03-19 22:03:46 +00:00
|
|
|
and then match that.
|
|
|
|
*/
|
2023-01-27 09:49:39 +00:00
|
|
|
platformMatch = platform: elem: let
|
2018-03-19 22:03:46 +00:00
|
|
|
pattern =
|
|
|
|
if builtins.isString elem
|
|
|
|
then { system = elem; }
|
2023-01-27 10:19:30 +00:00
|
|
|
else if elem?parsed
|
|
|
|
then elem
|
2018-03-19 22:03:46 +00:00
|
|
|
else { parsed = elem; };
|
|
|
|
in lib.matchAttrs pattern platform;
|
2021-02-25 16:21:13 +00:00
|
|
|
|
|
|
|
/* Check if a package is available on a given platform.
|
|
|
|
|
|
|
|
A package is available on a platform if both
|
|
|
|
|
2022-10-09 22:25:46 +00:00
|
|
|
1. One of `meta.platforms` pattern matches the given
|
|
|
|
platform, or `meta.platforms` is not present.
|
2021-02-25 16:21:13 +00:00
|
|
|
|
|
|
|
2. None of `meta.badPlatforms` pattern matches the given platform.
|
|
|
|
*/
|
|
|
|
availableOn = platform: pkg:
|
2022-10-09 22:26:14 +00:00
|
|
|
((!pkg?meta.platforms) || lib.any (platformMatch platform) pkg.meta.platforms) &&
|
2021-02-25 16:21:13 +00:00
|
|
|
lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
|
2021-10-10 23:08:40 +00:00
|
|
|
|
|
|
|
/* 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; }
|
|
|
|
);
|
2022-04-04 22:10:44 +00:00
|
|
|
|
|
|
|
/* Get the path to the main program of a derivation with either
|
|
|
|
meta.mainProgram or pname or name
|
|
|
|
|
|
|
|
Type: getExe :: derivation -> 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:
|
lib.getExe: Do not make assumptions about the main program
Before this commit, getExe assumes that if `meta.mainProgram` is unset,
it has a main program that's named after the package name.
While this is probable, it leads to a bad error when the assumption does
not hold. If the user called `getExe` themselves, they might narrow down
the location of the assumption quite easily, but if that's not the case,
they'll have to go down the rabbit hole to figure out what went wrong.
For example, a NixOS module may use `lib.getExe` on a package-typed option
which is then used in the system configuration. This then typically leads
to a failure *after* deployment, which is bad, and it's quite likely that
the user will debug the package output contents before digging through the
NixOS module, which is bad.
Furthermore the `getExe` call is rather inconspicuous as it does not
contain something like "/bin/foo", which is bad.
Also modules can be hard to read for a newbie, which is bad.
All of this can be avoided by requiring `meta.mainProgram`.
Many packages already have the attribute, and I would expect 80% of
`getExe` usages to be covered by 20% of packages, because plenty of
packages aren't used with `getExe` anyway.
Finally we could make an effort to set `mainProgram` semi-automatically
using `nix-index`.
2023-07-31 16:56:19 +00:00
|
|
|
"${lib.getBin x}/bin/${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, specify the full path to the program, such as \"\${lib.getBin foo}/bin/bar\"."
|
|
|
|
lib.getName x
|
|
|
|
)}";
|
2009-03-30 13:22:19 +00:00
|
|
|
}
|