From 84a105254d52d8ded6c5365399b61bea14505cd7 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sun, 24 Nov 2019 16:19:32 +0000 Subject: [PATCH] lib: Add getName to mirror getVersion --- lib/default.nix | 3 ++- lib/strings.nix | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/default.nix b/lib/default.nix index 18d2dfae1e1..0e840dd6c9e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -84,7 +84,8 @@ let hasInfix hasPrefix hasSuffix stringToCharacters stringAsChars escape escapeShellArg escapeShellArgs replaceChars lowerChars upperChars toLower toUpper addContextFrom splitString - removePrefix removeSuffix versionOlder versionAtLeast getVersion + removePrefix removeSuffix versionOlder versionAtLeast + getName getVersion nameFromURL enableFeature enableFeatureAs withFeature withFeatureAs fixedWidthString fixedWidthNumber isStorePath toInt readPathsFromFile fileContents; diff --git a/lib/strings.nix b/lib/strings.nix index ae0d74c6721..6dbb3d3a3e8 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -472,6 +472,23 @@ rec { */ versionAtLeast = v1: v2: !versionOlder v1 v2; + /* This function takes an argument that's either a derivation or a + derivation's "name" attribute and extracts the name part from that + argument. + + Example: + getName "youtube-dl-2016.01.01" + => "youtube-dl" + getName pkgs.youtube-dl + => "youtube-dl" + */ + getName = x: + let + parse = drv: (builtins.parseDrvName drv).name; + in if isString x + then parse x + else x.pname or (parse x.name); + /* This function takes an argument that's either a derivation or a derivation's "name" attribute and extracts the version part from that argument.