2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Some functions for manipulating meta attributes, as well as the
|
|
|
|
name attribute.
|
|
|
|
*/
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
{ lib }:
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
let
|
|
|
|
inherit (lib) matchAttrs any all isDerivation getBin assertMsg;
|
|
|
|
inherit (builtins) isString match typeOf;
|
|
|
|
|
|
|
|
in
|
2020-04-24 23:36:52 +00:00
|
|
|
rec {
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Add to or override the meta attributes of the given
|
|
|
|
derivation.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`newAttrs`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
|
|
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.addMetaAttrs` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
addMetaAttrs {description = "Bla blah";} somePkg
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
addMetaAttrs = newAttrs: drv:
|
|
|
|
drv // { meta = (drv.meta or {}) // newAttrs; };
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Disable Hydra builds of given derivation.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
dontDistribute = drv: addMetaAttrs { hydraPlatforms = []; } drv;
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00: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-06-05 15:53:02 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`name`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
setName = name: drv: drv // {inherit name;};
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00: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
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
```nix
|
|
|
|
updateName (oldName: oldName + "-experimental") somePkg
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
updateName = updater: drv: drv // {name = updater (drv.name);};
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Append a suffix to the name of a package (before the version
|
|
|
|
part).
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`suffix`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
*/
|
2020-04-24 23:36:52 +00:00
|
|
|
appendToName = suffix: updateName (name:
|
|
|
|
let x = builtins.parseDrvName name; in "${x.name}-${suffix}-${x.version}");
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Apply a function to each derivation and only to derivations in an attrset.
|
|
|
|
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`f`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`set`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
mapDerivationAttrset = f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Set the nix-env priority of the package.
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`priority`
|
|
|
|
: 1\. Function argument
|
|
|
|
|
|
|
|
`drv`
|
|
|
|
: 2\. Function argument
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
setPrio = priority: addMetaAttrs { inherit priority; };
|
|
|
|
|
2024-07-01 15:47:52 +00: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
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
lowPrio = setPrio 10;
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Apply lowPrio to an attrset with derivations
|
|
|
|
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`set`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
lowPrioSet = set: mapDerivationAttrset lowPrio set;
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00: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
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
hiPrio = setPrio (-10);
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
|
|
|
Apply hiPrio to an attrset with derivations
|
|
|
|
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
`set`
|
|
|
|
|
|
|
|
: 1\. Function argument
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
|
|
|
hiPrioSet = set: mapDerivationAttrset hiPrio set;
|
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +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.
|
|
|
|
|
|
|
|
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`).
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
We can inject these into a pattern for the whole of a structured platform,
|
|
|
|
and then match that.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
# Inputs
|
2023-02-02 18:25:31 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
`platform`
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
: 1\. Function argument
|
2024-02-29 20:09:43 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
`elem`
|
|
|
|
|
|
|
|
: 2\. Function argument
|
|
|
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.platformMatch` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
lib.meta.platformMatch { system = "aarch64-darwin"; } "aarch64-darwin"
|
|
|
|
=> true
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
2024-01-02 11:29:13 +00: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-05-20 23:08:51 +00:00
|
|
|
|
2024-07-01 15:47:52 +00: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-05-20 23:08:51 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
: 1\. Function argument
|
2021-05-20 23:08:51 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
`pkg`
|
2021-05-20 23:08:51 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
: 2\. Function argument
|
2024-02-29 20:09:43 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.availableOn` usage example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
lib.meta.availableOn { system = "aarch64-darwin"; } pkg.zsh
|
|
|
|
=> true
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2021-05-20 23:08:51 +00:00
|
|
|
*/
|
|
|
|
availableOn = platform: pkg:
|
2024-01-02 11:29:13 +00:00
|
|
|
((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) &&
|
|
|
|
all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
|
2021-12-19 01:06:50 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
/**
|
2024-07-27 06:49:29 +00:00
|
|
|
Get the corresponding attribute in lib.licenses from the SPDX ID
|
|
|
|
or warn and fallback to `{ shortName = <license string>; }`.
|
|
|
|
|
|
|
|
For SPDX IDs, see https://spdx.org/licenses
|
2024-07-01 15:47:52 +00: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-12-19 01:06:50 +00:00
|
|
|
*/
|
|
|
|
getLicenseFromSpdxId =
|
2024-07-27 06:49:29 +00:00
|
|
|
licstr:
|
|
|
|
getLicenseFromSpdxIdOr licstr (
|
2021-12-19 01:06:50 +00:00
|
|
|
lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
|
|
|
|
{ shortName = licstr; }
|
|
|
|
);
|
2022-04-27 09:35:20 +00:00
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
/**
|
|
|
|
Get the corresponding attribute in lib.licenses from the SPDX ID
|
|
|
|
or fallback to the given default value.
|
|
|
|
|
|
|
|
For SPDX IDs, see https://spdx.org/licenses
|
|
|
|
|
|
|
|
# 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
|
|
|
|
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: default:
|
|
|
|
spdxLicenses.${ lib.toLower licstr } or default;
|
|
|
|
|
2024-07-01 15:47:52 +00: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-27 09:35:20 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `lib.meta.getExe` usage example
|
2022-04-27 09:35:20 +00:00
|
|
|
|
2024-07-01 15:47:52 +00: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-27 09:35:20 +00:00
|
|
|
*/
|
2024-01-02 11:29:13 +00: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-22 20:05:09 +00:00
|
|
|
|
2024-07-01 15:47:52 +00: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-22 20:05:09 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
:::
|
2023-08-22 20:05:09 +00:00
|
|
|
*/
|
2023-11-16 04:20:00 +00:00
|
|
|
getExe' = x: y:
|
2024-01-02 11:29:13 +00: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.";
|
|
|
|
assert assertMsg (match ".*\/.*" y == null)
|
|
|
|
"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}";
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|