···248248### `lib.sourceTypes.binaryBytecode` {#lib.sourceTypes.binaryBytecode}
249249250250Code to run on a VM interpreter or JIT compiled into bytecode by a third party. This includes packages which download Java `.jar` files from another source.
251251+252252+## Software identifiers {#sec-meta-identifiers}
253253+254254+Package's `meta.identifiers` attribute specifies information about software identifiers associated with this package. Software identifiers are used, for example:
255255+* to generate Software Bill of Materials (SBOM) that lists all components used to build the software, which can later be used to perform vulnerability or license analysis of the resulting software;
256256+* to lookup software in different vulnerability databases or report new vulnerabilities to them.
257257+258258+Overriding the default `meta.identifiers` attribute is optional, but it is recommended to fill in pieces to help tools mentioned above get precise data.
259259+For example, we could get automatic notifications about potential vulnerabilities for users in the future.
260260+All identifiers specified in `meta.identifiers` are expected to be unambiguous and valid.
261261+262262+`meta.identifiers` contains `v1` attribute which is an attribute set that guarantees backward compatibility of its constituents. Right now it contains copies of all other attributes in `meta.identifiers`.
263263+264264+### CPE {#sec-meta-identifiers-cpe}
265265+266266+Common Platform Enumeration (CPE) is a specification maintained by NIST as part of the Security Content Automation Protocol (SCAP). It is used to identify software in National Vulnerabilities Database (NVD, https://nvd.nist.gov) and other vulnerability databases.
267267+268268+Current version of CPE 2.3 consists of 13 parts:
269269+270270+```
271271+cpe:2.3:a:<vendor>:<product>:<version>:<update>:<edition>:<language>:<sw_edition>:<target_sw>:<target_hw>:<other>
272272+```
273273+274274+Some of them are as follows:
275275+276276+* *CPE version* - current version of CPE is `2.3`
277277+* *part* - usually in Nixpkgs `a` for "application", can also be `o` for "operating system" or `h` for "hardware"
278278+* *vendor* - can point to the source of the package, or to Nixpkgs itself
279279+* *product* - name of the package
280280+* *version* - version of the package
281281+* *update* - name of the latest update, can be a patch version for semantically versioned packages
282282+* *edition* - any additional specification about the version
283283+284284+You can find information about all of these attributes in the [official specification](https://csrc.nist.gov/projects/security-content-automation-protocol/specifications/cpe/naming) (heading 5.3.3, pages 11-13).
285285+286286+Any fields that don't have a value are set to either `-` if the value is not available or `*` when the field can match any value.
287287+288288+For example, for glibc 2.40.1 CPE would be `cpe:2.3:a:gnu:glibc:2.40:1:*:*:*:*:*:*`.
289289+290290+#### `meta.identifiers.cpeParts` {#var-meta-identifiers-cpeParts}
291291+292292+This attribute contains an attribute set of all parts of the CPE for this package. Most of the parts default to `*` (match any value), with some exceptions:
293293+294294+* `part` defaults to `a` (application), can also be set to `o` for operating systems, for example, Linux kernel, or to `h` for hardware
295295+* `vendor` cannot be deduced from other sources, so it must be specified by the package author
296296+* `product` defaults to provided derivation's `pname` attribute and must be provided explicitly if `pname` is missing
297297+* `version` and `update` have no defaults and should be specified explicitly or using helper functions, when missing, `cpe` attribute will be empty, and all possible guesses using helper functions will be in `possibleCPEs` attribute.
298298+299299+It is up to the package author to make sure all parts are correct and match expected values in [NVD dictionary](https://nvd.nist.gov/products/cpe). Unknown values can be skipped, which would leave them with the default value of `*`.
300300+301301+Following functions help with filling out `version` and `update` fields:
302302+303303+* [`lib.meta.cpeFullVersionWithVendor`](#function-library-lib.meta.cpeFullVersionWithVendor)
304304+* [`lib.meta.cpePatchVersionInUpdateWithVendor`](#function-library-lib.meta.cpePatchVersionInUpdateWithVendor)
305305+306306+For many packages to make CPE available it should be enough to specify only:
307307+308308+```nix
309309+{
310310+ # ...
311311+ meta.identifiers.cpeParts = lib.meta.cpePatchVersionInUpdateWithVendor vendor version;
312312+}
313313+```
314314+315315+#### `meta.identifiers.cpe` {#var-meta-identifiers-cpe}
316316+317317+A readonly attribute that concatenates all CPE parts in one string.
318318+319319+#### `meta.identifiers.possibleCPEs` {#var-meta-identifiers-possibleCPEs}
320320+321321+A readonly attribute containing the list of guesses for what CPE for this package can look like. It includes all variants of version handling mentioned above. Each item is an attrset with attributes `cpeParts` and `cpe` for each guess.
+188-1
lib/meta.nix
···1515 assertMsg
1616 ;
1717 inherit (lib.attrsets) mapAttrs' filterAttrs;
1818- inherit (builtins) isString match typeOf;
1818+ inherit (builtins)
1919+ isString
2020+ match
2121+ typeOf
2222+ elemAt
2323+ ;
19242025in
2126rec {
···491496 assert assertMsg (match ".*/.*" y == null)
492497 "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.";
493498 "${getBin x}/bin/${y}";
499499+500500+ /**
501501+ Generate [CPE parts](#var-meta-identifiers-cpeParts) from inputs. Copies `vendor` and `version` to the output, and sets `update` to `*`.
502502+503503+ # Inputs
504504+505505+ `vendor`
506506+507507+ : package's vendor
508508+509509+ `version`
510510+511511+ : package's version
512512+513513+ # Type
514514+515515+ ```
516516+ cpeFullVersionWithVendor :: string -> string -> AttrSet
517517+ ```
518518+519519+ # Examples
520520+ :::{.example}
521521+ ## `lib.meta.cpeFullVersionWithVendor` usage example
522522+523523+ ```nix
524524+ lib.meta.cpeFullVersionWithVendor "gnu" "1.2.3"
525525+ => {
526526+ vendor = "gnu";
527527+ version = "1.2.3";
528528+ update = "*";
529529+ }
530530+ ```
531531+532532+ :::
533533+ :::{.example}
534534+ ## `lib.meta.cpeFullVersionWithVendor` usage in derivations
535535+536536+ ```nix
537537+ mkDerivation rec {
538538+ version = "1.2.3";
539539+ # ...
540540+ meta = {
541541+ # ...
542542+ identifiers.cpeParts = lib.meta.cpeFullVersionWithVendor "gnu" version;
543543+ };
544544+ }
545545+ ```
546546+ :::
547547+ */
548548+ cpeFullVersionWithVendor = vendor: version: {
549549+ inherit vendor version;
550550+ update = "*";
551551+ };
552552+553553+ /**
554554+ Alternate version of [`lib.meta.cpePatchVersionInUpdateWithVendor`](#function-library-lib.meta.cpePatchVersionInUpdateWithVendor).
555555+ If `cpePatchVersionInUpdateWithVendor` succeeds, returns an attribute set with `success` set to `true` and `value` set to the result.
556556+ Otherwise, `success` is set to `false` and `error` is set to the string representation of the error.
557557+558558+ # Inputs
559559+560560+ `vendor`
561561+562562+ : package's vendor
563563+564564+ `version`
565565+566566+ : package's version
567567+568568+ # Type
569569+570570+ ```
571571+ tryCPEPatchVersionInUpdateWithVendor :: string -> string -> AttrSet
572572+ ```
573573+574574+ # Examples
575575+ :::{.example}
576576+ ## `lib.meta.tryCPEPatchVersionInUpdateWithVendor` usage example
577577+578578+ ```nix
579579+ lib.meta.tryCPEPatchVersionInUpdateWithVendor "gnu" "1.2.3"
580580+ => {
581581+ success = true;
582582+ value = {
583583+ vendor = "gnu";
584584+ version = "1.2";
585585+ update = "3";
586586+ };
587587+ }
588588+ ```
589589+590590+ :::
591591+ :::{.example}
592592+ ## `lib.meta.cpePatchVersionInUpdateWithVendor` error example
593593+594594+ ```nix
595595+ lib.meta.tryCPEPatchVersionInUpdateWithVendor "gnu" "5.3p0"
596596+ => {
597597+ success = false;
598598+ error = "version 5.3p0 doesn't match regex `([0-9]+\\.[0-9]+)\\.([0-9]+)`";
599599+ }
600600+ ```
601601+602602+ :::
603603+ */
604604+ tryCPEPatchVersionInUpdateWithVendor =
605605+ vendor: version:
606606+ let
607607+ regex = "([0-9]+\\.[0-9]+)\\.([0-9]+)";
608608+ # we have to call toString here in case version is an attrset with __toString attribute
609609+ versionMatch = builtins.match regex (toString version);
610610+ in
611611+ if versionMatch == null then
612612+ {
613613+ success = false;
614614+ error = "version ${version} doesn't match regex `${regex}`";
615615+ }
616616+ else
617617+ {
618618+ success = true;
619619+ value = {
620620+ inherit vendor;
621621+ version = elemAt versionMatch 0;
622622+ update = elemAt versionMatch 1;
623623+ };
624624+ };
625625+626626+ /**
627627+ Generate [CPE parts](#var-meta-identifiers-cpeParts) from inputs. Copies `vendor` to the result. When `version` matches `X.Y.Z` where all parts are numerical, sets `version` and `update` fields to `X.Y` and `Z`. Throws an error if the version doesn't match the expected template.
628628+629629+ # Inputs
630630+631631+ `vendor`
632632+633633+ : package's vendor
634634+635635+ `version`
636636+637637+ : package's version
638638+639639+ # Type
640640+641641+ ```
642642+ cpePatchVersionInUpdateWithVendor :: string -> string -> AttrSet
643643+ ```
644644+645645+ # Examples
646646+ :::{.example}
647647+ ## `lib.meta.cpePatchVersionInUpdateWithVendor` usage example
648648+649649+ ```nix
650650+ lib.meta.cpePatchVersionInUpdateWithVendor "gnu" "1.2.3"
651651+ => {
652652+ vendor = "gnu";
653653+ version = "1.2";
654654+ update = "3";
655655+ }
656656+ ```
657657+658658+ :::
659659+ :::{.example}
660660+ ## `lib.meta.cpePatchVersionInUpdateWithVendor` usage in derivations
661661+662662+ ```nix
663663+ mkDerivation rec {
664664+ version = "1.2.3";
665665+ # ...
666666+ meta = {
667667+ # ...
668668+ identifiers.cpeParts = lib.meta.cpePatchVersionInUpdateWithVendor "gnu" version;
669669+ };
670670+ }
671671+ ```
672672+673673+ :::
674674+ */
675675+ cpePatchVersionInUpdateWithVendor =
676676+ vendor: version:
677677+ let
678678+ result = tryCPEPatchVersionInUpdateWithVendor vendor version;
679679+ in
680680+ if result.success then result.value else throw result.error;
494681}
···1111 inherit (lib)
1212 all
1313 attrNames
1414+ attrValues
1415 concatMapStrings
1516 concatMapStringsSep
1617 concatStrings
···37383839 inherit (lib.meta)
3940 availableOn
4141+ cpeFullVersionWithVendor
4242+ tryCPEPatchVersionInUpdateWithVendor
4043 ;
41444245 inherit (lib.generators)
···438441 # Used for the original location of the maintainer and team attributes to assist with pings.
439442 maintainersPosition = any;
440443 teamsPosition = any;
444444+445445+ identifiers = attrs;
441446 };
442447443448 checkMetaAttr =
···571576 else
572577 validYes;
573578579579+ # Helper functions and declarations to handle identifiers, extracted to reduce allocations
580580+ hasAllCPEParts =
581581+ cpeParts:
582582+ let
583583+ values = attrValues cpeParts;
584584+ in
585585+ (length values == 11) && !any isNull values;
586586+ makeCPE =
587587+ {
588588+ part,
589589+ vendor,
590590+ product,
591591+ version,
592592+ update,
593593+ edition,
594594+ sw_edition,
595595+ target_sw,
596596+ target_hw,
597597+ language,
598598+ other,
599599+ }:
600600+ "cpe:2.3:${part}:${vendor}:${product}:${version}:${update}:${edition}:${sw_edition}:${target_sw}:${target_hw}:${language}:${other}";
601601+ possibleCPEPartsFuns = [
602602+ (vendor: version: {
603603+ success = true;
604604+ value = cpeFullVersionWithVendor vendor version;
605605+ })
606606+ tryCPEPatchVersionInUpdateWithVendor
607607+ ];
608608+574609 # The meta attribute is passed in the resulting attribute set,
575610 # but it's not part of the actual derivation, i.e., it's not
576611 # passed to the builder and is not a dependency. But since we
···634669 # if you add a new maintainer or team attribute please ensure that this expectation is still met.
635670 maintainers =
636671 attrs.meta.maintainers or [ ] ++ concatMap (team: team.members or [ ]) attrs.meta.teams or [ ];
672672+673673+ identifiers =
674674+ let
675675+ # nix-env writes a warning for each derivation that has null in its meta values, so
676676+ # fields without known values are removed from the result
677677+ defaultCPEParts = {
678678+ part = "a";
679679+ #vendor = null;
680680+ ${if attrs ? pname then "product" else null} = attrs.pname;
681681+ #version = null;
682682+ #update = null;
683683+ edition = "*";
684684+ sw_edition = "*";
685685+ target_sw = "*";
686686+ target_hw = "*";
687687+ language = "*";
688688+ other = "*";
689689+ };
690690+691691+ cpeParts = defaultCPEParts // attrs.meta.identifiers.cpeParts or { };
692692+ cpe = if hasAllCPEParts cpeParts then makeCPE cpeParts else null;
693693+694694+ possibleCPEs =
695695+ if cpe != null then
696696+ [ { inherit cpeParts cpe; } ]
697697+ else if attrs.meta.identifiers.cpeParts.vendor or null == null || attrs.version or null == null then
698698+ [ ]
699699+ else
700700+ concatMap (
701701+ f:
702702+ let
703703+ result = f attrs.meta.identifiers.cpeParts.vendor attrs.version;
704704+ # Note that attrs.meta.identifiers.cpeParts at this point can include defaults with user overrides.
705705+ # Since we can't split them apart, user overrides don't apply to possibleCPEs.
706706+ guessedParts = cpeParts // result.value;
707707+ in
708708+ optional (result.success && hasAllCPEParts guessedParts) {
709709+ cpeParts = guessedParts;
710710+ cpe = makeCPE guessedParts;
711711+ }
712712+ ) possibleCPEPartsFuns;
713713+ v1 = {
714714+ inherit cpeParts possibleCPEs;
715715+ ${if cpe != null then "cpe" else null} = cpe;
716716+ };
717717+ in
718718+ v1
719719+ // {
720720+ inherit v1;
721721+ };
637722638723 # Expose the result of the checks for everyone to see.
639724 unfree = hasUnfreeLicense attrs;