nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# Version string functions.
2{ lib }:
3
4rec {
5
6 inherit (builtins) compareVersions;
7
8 /**
9 Break a version string into its component parts.
10
11 # Examples
12 :::{.example}
13 ## `splitVersion` usage example
14
15 ```nix
16 splitVersion "1.2.3"
17 => ["1" "2" "3"]
18 ```
19
20 :::
21 */
22 splitVersion = builtins.splitVersion;
23
24 /**
25 Get the major version string from a string.
26
27 # Inputs
28
29 `v`
30
31 : 1\. Function argument
32
33 # Examples
34 :::{.example}
35 ## `major` usage example
36
37 ```nix
38 major "1.2.3"
39 => "1"
40 ```
41
42 :::
43 */
44 major = v: builtins.elemAt (splitVersion v) 0;
45
46 /**
47 Get the minor version string from a string.
48
49 # Inputs
50
51 `v`
52
53 : 1\. Function argument
54
55 # Examples
56 :::{.example}
57 ## `minor` usage example
58
59 ```nix
60 minor "1.2.3"
61 => "2"
62 ```
63
64 :::
65 */
66 minor = v: builtins.elemAt (splitVersion v) 1;
67
68 /**
69 Get the patch version string from a string.
70
71 # Inputs
72
73 `v`
74
75 : 1\. Function argument
76
77 # Examples
78 :::{.example}
79 ## `patch` usage example
80
81 ```nix
82 patch "1.2.3"
83 => "3"
84 ```
85
86 :::
87 */
88 patch = v: builtins.elemAt (splitVersion v) 2;
89
90 /**
91 Get string of the first two parts (major and minor)
92 of a version string.
93
94 # Inputs
95
96 `v`
97
98 : 1\. Function argument
99
100 # Examples
101 :::{.example}
102 ## `majorMinor` usage example
103
104 ```nix
105 majorMinor "1.2.3"
106 => "1.2"
107 ```
108
109 :::
110 */
111 majorMinor = v: builtins.concatStringsSep "." (lib.take 2 (splitVersion v));
112
113 /**
114 Pad a version string with zeros to match the given number of components.
115
116 # Inputs
117
118 `n`
119
120 : 1\. Function argument
121
122 `version`
123
124 : 2\. Function argument
125
126 # Examples
127 :::{.example}
128 ## `pad` usage example
129
130 ```nix
131 pad 3 "1.2"
132 => "1.2.0"
133 pad 3 "1.3-rc1"
134 => "1.3.0-rc1"
135 pad 3 "1.2.3.4"
136 => "1.2.3"
137 ```
138
139 :::
140 */
141 pad =
142 n: version:
143 let
144 numericVersion = lib.head (lib.splitString "-" version);
145 versionSuffix = lib.removePrefix numericVersion version;
146 in
147 lib.concatStringsSep "." (lib.take n (lib.splitVersion numericVersion ++ lib.genList (_: "0") n))
148 + versionSuffix;
149
150}