at 23.11-beta 1.4 kB view raw
1/* Version string functions. */ 2{ lib }: 3 4rec { 5 6 /* Break a version string into its component parts. 7 8 Example: 9 splitVersion "1.2.3" 10 => ["1" "2" "3"] 11 */ 12 splitVersion = builtins.splitVersion or (lib.splitString "."); 13 14 /* Get the major version string from a string. 15 16 Example: 17 major "1.2.3" 18 => "1" 19 */ 20 major = v: builtins.elemAt (splitVersion v) 0; 21 22 /* Get the minor version string from a string. 23 24 Example: 25 minor "1.2.3" 26 => "2" 27 */ 28 minor = v: builtins.elemAt (splitVersion v) 1; 29 30 /* Get the patch version string from a string. 31 32 Example: 33 patch "1.2.3" 34 => "3" 35 */ 36 patch = v: builtins.elemAt (splitVersion v) 2; 37 38 /* Get string of the first two parts (major and minor) 39 of a version string. 40 41 Example: 42 majorMinor "1.2.3" 43 => "1.2" 44 */ 45 majorMinor = v: 46 builtins.concatStringsSep "." 47 (lib.take 2 (splitVersion v)); 48 49 /* Pad a version string with zeros to match the given number of components. 50 51 Example: 52 pad 3 "1.2" 53 => "1.2.0" 54 pad 3 "1.3-rc1" 55 => "1.3.0-rc1" 56 pad 3 "1.2.3.4" 57 => "1.2.3" 58 */ 59 pad = n: version: let 60 numericVersion = lib.head (lib.splitString "-" version); 61 versionSuffix = lib.removePrefix numericVersion version; 62 in lib.concatStringsSep "." (lib.take n (lib.splitVersion numericVersion ++ lib.genList (_: "0") n)) + versionSuffix; 63 64}