summaryrefslogtreecommitdiffstats
path: root/lib/versions.nix
diff options
context:
space:
mode:
authorNaïm Favier <n@monade.li>2022-12-20 18:38:47 +0100
committerNaïm Favier <n@monade.li>2022-12-21 12:58:21 +0100
commit03554797153aa90263161784c296ef6518af3358 (patch)
treef7ddfee6960e6d04a4696ea9c58195412c0e5b85 /lib/versions.nix
parent3ff39f984faa5f528f7ac5e548110d4e20327aa1 (diff)
lib/versions: add `pad`
Pad a version string with zeros to match a given number of components.
Diffstat (limited to 'lib/versions.nix')
-rw-r--r--lib/versions.nix15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/versions.nix b/lib/versions.nix
index 0e9d81ac78b1..986e7e5f9b37 100644
--- a/lib/versions.nix
+++ b/lib/versions.nix
@@ -46,4 +46,19 @@ rec {
builtins.concatStringsSep "."
(lib.take 2 (splitVersion v));
+ /* Pad a version string with zeros to match the given number of components.
+
+ Example:
+ pad 3 "1.2"
+ => "1.2.0"
+ pad 3 "1.3-rc1"
+ => "1.3.0-rc1"
+ pad 3 "1.2.3.4"
+ => "1.2.3"
+ */
+ pad = n: version: let
+ numericVersion = lib.head (lib.splitString "-" version);
+ versionSuffix = lib.removePrefix numericVersion version;
+ in lib.concatStringsSep "." (lib.take n (lib.splitVersion numericVersion ++ lib.genList (_: "0") n)) + versionSuffix;
+
}