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