diff options
author | Naïm Favier <n@monade.li> | 2022-12-27 17:10:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-27 17:10:43 +0100 |
commit | 3fc528ff7fa9d0de0343ffd877cdb76287be2549 (patch) | |
tree | fdddc2dadde85f4b516a4f3759a66c503a1dbb7e /lib | |
parent | b25ad6c15358d02fd3560f27ec9e545fa73c2b1f (diff) | |
parent | a8fd50b79c899b3b4958f8bb95bb5717906a37b6 (diff) |
Merge pull request #207095 from ncfavier/linux-custom-kernel
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tests/misc.nix | 15 | ||||
-rw-r--r-- | lib/versions.nix | 15 |
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index c719fcf5d4fae..faf2b96530c1c 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -212,6 +212,21 @@ runTests { expected = [ "1" "2" "3" ]; }; + testPadVersionLess = { + expr = versions.pad 3 "1.2"; + expected = "1.2.0"; + }; + + testPadVersionLessExtra = { + expr = versions.pad 3 "1.3-rc1"; + expected = "1.3.0-rc1"; + }; + + testPadVersionMore = { + expr = versions.pad 3 "1.2.3.4"; + expected = "1.2.3"; + }; + testIsStorePath = { expr = let goodPath = diff --git a/lib/versions.nix b/lib/versions.nix index 0e9d81ac78b1e..986e7e5f9b37d 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; + } |