doc/stdenv/platform-notes: fix typo
[NixPkgs.git] / lib / versions.nix
blob32b4b5fbf945da3dca7e0c3fa567f2534d1775c2
1 # Version string functions.
2 { lib }:
4 rec {
6   /*
7     Break a version string into its component parts.
9     Example:
10       splitVersion "1.2.3"
11       => ["1" "2" "3"]
12   */
13   splitVersion = builtins.splitVersion;
15   /*
16     Get the major version string from a string.
18     Example:
19       major "1.2.3"
20       => "1"
21   */
22   major = v: builtins.elemAt (splitVersion v) 0;
24   /*
25     Get the minor version string from a string.
27     Example:
28       minor "1.2.3"
29       => "2"
30   */
31   minor = v: builtins.elemAt (splitVersion v) 1;
33   /*
34     Get the patch version string from a string.
36     Example:
37       patch "1.2.3"
38       => "3"
39   */
40   patch = v: builtins.elemAt (splitVersion v) 2;
42   /*
43     Get string of the first two parts (major and minor)
44     of a version string.
46     Example:
47       majorMinor "1.2.3"
48       => "1.2"
49   */
50   majorMinor = v: builtins.concatStringsSep "." (lib.take 2 (splitVersion v));
52   /*
53     Pad a version string with zeros to match the given number of components.
55     Example:
56       pad 3 "1.2"
57       => "1.2.0"
58       pad 3 "1.3-rc1"
59       => "1.3.0-rc1"
60       pad 3 "1.2.3.4"
61       => "1.2.3"
62   */
63   pad =
64     n: version:
65     let
66       numericVersion = lib.head (lib.splitString "-" version);
67       versionSuffix = lib.removePrefix numericVersion version;
68     in
69     lib.concatStringsSep "." (lib.take n (lib.splitVersion numericVersion ++ lib.genList (_: "0") n))
70     + versionSuffix;