Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@
"sec-language-cosmic": [
"index.html#sec-language-cosmic"
],
"sec-meta-identifiers": [
"index.html#sec-meta-identifiers"
],
"sec-meta-identifiers-cpe": [
"index.html#sec-meta-identifiers-cpe"
],
"sec-modify-via-packageOverrides": [
"index.html#sec-modify-via-packageOverrides"
],
Expand Down Expand Up @@ -622,6 +628,15 @@
"typst-package-scope-and-usage": [
"index.html#typst-package-scope-and-usage"
],
"var-meta-identifiers-cpe": [
"index.html#var-meta-identifiers-cpe"
],
"var-meta-identifiers-cpeParts": [
"index.html#var-meta-identifiers-cpeParts"
],
"var-meta-identifiers-possibleCPEs": [
"index.html#var-meta-identifiers-possibleCPEs"
],
"var-meta-teams": [
"index.html#var-meta-teams"
],
Expand Down
71 changes: 71 additions & 0 deletions doc/stdenv/meta.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,74 @@ Code to be executed on a peripheral device or embedded controller, built by a th
### `lib.sourceTypes.binaryBytecode` {#lib.sourceTypes.binaryBytecode}

Code 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.

## Software identifiers {#sec-meta-identifiers}

Package's `meta.identifiers` attribute specifies information about software identifiers associated with this package. Software identifiers are used, for example:
* 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;
* to lookup software in different vulnerability databases or report new vulnerabilities to them.

Overriding the default `meta.identifiers` attribute is optional, but it is recommended to fill in pieces to help tools mentioned above get precise data.
For example, we could get automatic notifications about potential vulnerabilities for users in the future.
All identifiers specified in `meta.identifiers` are expected to be unambiguous and valid.

`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`.

### CPE {#sec-meta-identifiers-cpe}

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.

Current version of CPE 2.3 consists of 13 parts:

```
cpe:2.3:a:<vendor>:<product>:<version>:<update>:<edition>:<language>:<sw_edition>:<target_sw>:<target_hw>:<other>
```

Some of them are as follows:

* *CPE version* - current version of CPE is `2.3`
* *part* - usually in Nixpkgs `a` for "application", can also be `o` for "operating system" or `h` for "hardware"
* *vendor* - can point to the source of the package, or to Nixpkgs itself
* *product* - name of the package
* *version* - version of the package
* *update* - name of the latest update, can be a patch version for semantically versioned packages
* *edition* - any additional specification about the version

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).

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.

For example, for glibc 2.40.1 CPE would be `cpe:2.3:a:gnu:glibc:2.40:1:*:*:*:*:*:*`.

#### `meta.identifiers.cpeParts` {#var-meta-identifiers-cpeParts}

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:

* `part` defaults to `a` (application), can also be set to `o` for operating systems, for example, Linux kernel, or to `h` for hardware
* `vendor` cannot be deduced from other sources, so it must be specified by the package author
* `product` defaults to provided derivation's `pname` attribute and must be provided explicitly if `pname` is missing
* `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.

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 `*`.

Following functions help with filling out `version` and `update` fields:

* [`lib.meta.cpeFullVersionWithVendor`](#function-library-lib.meta.cpeFullVersionWithVendor)
* [`lib.meta.cpePatchVersionInUpdateWithVendor`](#function-library-lib.meta.cpePatchVersionInUpdateWithVendor)

For many packages to make CPE available it should be enough to specify only:

```nix
{
# ...
meta.identifiers.cpeParts = lib.meta.cpePatchVersionInUpdateWithVendor vendor version;
}
```

#### `meta.identifiers.cpe` {#var-meta-identifiers-cpe}

A readonly attribute that concatenates all CPE parts in one string.

#### `meta.identifiers.possibleCPEs` {#var-meta-identifiers-possibleCPEs}

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.
189 changes: 188 additions & 1 deletion lib/meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ let
assertMsg
;
inherit (lib.attrsets) mapAttrs' filterAttrs;
inherit (builtins) isString match typeOf;
inherit (builtins)
isString
match
typeOf
elemAt
;

in
rec {
Expand Down Expand Up @@ -484,4 +489,186 @@ rec {
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}";

/**
Generate [CPE parts](#var-meta-identifiers-cpeParts) from inputs. Copies `vendor` and `version` to the output, and sets `update` to `*`.

# Inputs

`vendor`

: package's vendor

`version`

: package's version

# Type

```
cpeFullVersionWithVendor :: string -> string -> AttrSet
```

# Examples
:::{.example}
## `lib.meta.cpeFullVersionWithVendor` usage example

```nix
lib.meta.cpeFullVersionWithVendor "gnu" "1.2.3"
=> {
vendor = "gnu";
version = "1.2.3";
update = "*";
}
```

:::
:::{.example}
## `lib.meta.cpeFullVersionWithVendor` usage in derivations

```nix
mkDerivation rec {
version = "1.2.3";
# ...
meta = {
# ...
identifiers.cpeParts = lib.meta.cpeFullVersionWithVendor "gnu" version;
};
}
```
:::
*/
cpeFullVersionWithVendor = vendor: version: {
inherit vendor version;
update = "*";
};

/**
Alternate version of [`lib.meta.cpePatchVersionInUpdateWithVendor`](#function-library-lib.meta.cpePatchVersionInUpdateWithVendor).
If `cpePatchVersionInUpdateWithVendor` succeeds, returns an attribute set with `success` set to `true` and `value` set to the result.
Otherwise, `success` is set to `false` and `error` is set to the string representation of the error.

# Inputs

`vendor`

: package's vendor

`version`

: package's version

# Type

```
tryCPEPatchVersionInUpdateWithVendor :: string -> string -> AttrSet
```

# Examples
:::{.example}
## `lib.meta.tryCPEPatchVersionInUpdateWithVendor` usage example

```nix
lib.meta.tryCPEPatchVersionInUpdateWithVendor "gnu" "1.2.3"
=> {
success = true;
value = {
vendor = "gnu";
version = "1.2";
update = "3";
};
}
```

:::
:::{.example}
## `lib.meta.cpePatchVersionInUpdateWithVendor` error example

```nix
lib.meta.tryCPEPatchVersionInUpdateWithVendor "gnu" "5.3p0"
=> {
success = false;
error = "version 5.3p0 doesn't match regex `([0-9]+\\.[0-9]+)\\.([0-9]+)`";
}
```

:::
*/
tryCPEPatchVersionInUpdateWithVendor =
vendor: version:
let
regex = "([0-9]+\\.[0-9]+)\\.([0-9]+)";
# we have to call toString here in case version is an attrset with __toString attribute
versionMatch = builtins.match regex (toString version);
in
if versionMatch == null then
{
success = false;
error = "version ${version} doesn't match regex `${regex}`";
}
else
{
success = true;
value = {
inherit vendor;
version = elemAt versionMatch 0;
update = elemAt versionMatch 1;
};
};

/**
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.

# Inputs

`vendor`

: package's vendor

`version`

: package's version

# Type

```
cpePatchVersionInUpdateWithVendor :: string -> string -> AttrSet
```

# Examples
:::{.example}
## `lib.meta.cpePatchVersionInUpdateWithVendor` usage example

```nix
lib.meta.cpePatchVersionInUpdateWithVendor "gnu" "1.2.3"
=> {
vendor = "gnu";
version = "1.2";
update = "3";
}
```

:::
:::{.example}
## `lib.meta.cpePatchVersionInUpdateWithVendor` usage in derivations

```nix
mkDerivation rec {
version = "1.2.3";
# ...
meta = {
# ...
identifiers.cpeParts = lib.meta.cpePatchVersionInUpdateWithVendor "gnu" version;
};
}
```

:::
*/
cpePatchVersionInUpdateWithVendor =
vendor: version:
let
result = tryCPEPatchVersionInUpdateWithVendor vendor version;
in
if result.success then result.value else throw result.error;
}
5 changes: 5 additions & 0 deletions pkgs/applications/networking/sync/rsync/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,10 @@ stdenv.mkDerivation rec {
ivan
];
platforms = platforms.unix;
identifiers.cpeParts = {
vendor = "samba";
inherit version;
update = "-";
};
};
}
1 change: 1 addition & 0 deletions pkgs/by-name/he/hello/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ stdenv.mkDerivation (finalAttrs: {
maintainers = with lib.maintainers; [ stv0g ];
mainProgram = "hello";
platforms = lib.platforms.all;
identifiers.cpeParts.vendor = "gnu";
};
})
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/common/meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ in
teams = [ teams.gcc ];
mainProgram = "${targetPrefix}gcc";

identifiers.cpeParts.vendor = "gnu";
}
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ pipe
platforms
teams
mainProgram
identifiers
;
};
}
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/llvm/common/common-let.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ rec {
++ lib.optionals (lib.versionAtLeast release_version "7") lib.platforms.riscv
++ lib.optionals (lib.versionAtLeast release_version "14") lib.platforms.m68k
++ lib.optionals (lib.versionAtLeast release_version "16") lib.platforms.loongarch64;

identifiers.cpeParts.vendor = "llvm";
};

releaseInfo =
Expand Down
7 changes: 7 additions & 0 deletions pkgs/os-specific/linux/kernel/manual-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,13 @@ lib.makeOverridable (
]
++ lib.optional (lib.versionOlder version "5.19") "loongarch64-linux";
timeout = 14400; # 4 hours
identifiers.cpeParts = {
part = "o";
vendor = "linux";
product = "linux_kernel";
inherit version;
update = "*";
};
}
// extraMeta;
};
Expand Down
10 changes: 10 additions & 0 deletions pkgs/shells/bash/5.nix
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,15 @@ lib.warnIf (withDocs != null)
badPlatforms = [ lib.systems.inspect.patterns.isMinGW ];
maintainers = [ ];
mainProgram = "bash";
identifiers.cpeParts =
let
versionSplit = lib.split "p" version;
in
{
vendor = "gnu";
product = "bash";
version = lib.elemAt versionSplit 0;
update = lib.elemAt versionSplit 2;
};
};
}
Loading
Loading