about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2023-12-13 21:30:13 +0100
committerGitHub <noreply@github.com>2023-12-13 21:30:13 +0100
commit39948a158c78f12b01a4f0271d8920dcd6384b50 (patch)
treeef26aeb9944a088a1a68ccbb7ded6c4ab5acbbbb /lib
parente9b49cdba86d8532856987ec846d16a16ecf44b2 (diff)
parent0286e3119e3c2035b8c1a4a0d61f2e1cf6e6f98f (diff)
downloadnixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.tar
nixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.tar.gz
nixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.tar.bz2
nixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.tar.lz
nixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.tar.xz
nixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.tar.zst
nixlib-39948a158c78f12b01a4f0271d8920dcd6384b50.zip
Merge pull request #273704 from adisbladis/lib-getexe-efficiency
lib.getExe: Make more efficient
Diffstat (limited to 'lib')
-rw-r--r--lib/meta.nix34
1 files changed, 15 insertions, 19 deletions
diff --git a/lib/meta.nix b/lib/meta.nix
index 1a43016d27c4..5d5f71d6c3cb 100644
--- a/lib/meta.nix
+++ b/lib/meta.nix
@@ -4,8 +4,8 @@
 { lib }:
 
 let
-  inherit (lib) matchAttrs any all;
-  inherit (builtins) isString;
+  inherit (lib) matchAttrs any all isDerivation getBin assertMsg;
+  inherit (builtins) isString match typeOf;
 
 in
 rec {
@@ -154,16 +154,12 @@ rec {
        getExe pkgs.mustache-go
        => "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache"
   */
-  getExe = x:
-    let
-      y = x.meta.mainProgram or (
-        # This could be turned into an error when 23.05 is at end of life
-        lib.warn "getExe: Package ${lib.strings.escapeNixIdentifier x.meta.name or x.pname or x.name} does not have the meta.mainProgram attribute. We'll assume that the main program has the same name for now, but this behavior is deprecated, because it leads to surprising errors when the assumption does not hold. If the package has a main program, please set `meta.mainProgram` in its definition to make this warning go away. Otherwise, if the package does not have a main program, or if you don't control its definition, use getExe' to specify the name to the program, such as lib.getExe' foo \"bar\"."
-          lib.getName
-          x
-      );
-    in
-    getExe' x y;
+  getExe = x: getExe' x (x.meta.mainProgram or (
+    # This could be turned into an error when 23.05 is at end of life
+    lib.warn "getExe: Package ${lib.strings.escapeNixIdentifier x.meta.name or x.pname or x.name} does not have the meta.mainProgram attribute. We'll assume that the main program has the same name for now, but this behavior is deprecated, because it leads to surprising errors when the assumption does not hold. If the package has a main program, please set `meta.mainProgram` in its definition to make this warning go away. Otherwise, if the package does not have a main program, or if you don't control its definition, use getExe' to specify the name to the program, such as lib.getExe' foo \"bar\"."
+    lib.getName
+    x
+  ));
 
   /* Get the path of a program of a derivation.
 
@@ -175,11 +171,11 @@ rec {
        => "/nix/store/5rs48jamq7k6sal98ymj9l4k2bnwq515-imagemagick-7.1.1-15/bin/convert"
   */
   getExe' = x: y:
-    assert lib.assertMsg (lib.isDerivation x)
-      "lib.meta.getExe': The first argument is of type ${builtins.typeOf x}, but it should be a derivation instead.";
-    assert lib.assertMsg (lib.isString y)
-     "lib.meta.getExe': The second argument is of type ${builtins.typeOf y}, but it should be a string instead.";
-    assert lib.assertMsg (builtins.length (lib.splitString "/" y) == 1)
-     "lib.meta.getExe': The second argument \"${y}\" is a nested path with a \"/\" character, but it should just be the name of the executable instead.";
-    "${lib.getBin x}/bin/${y}";
+    assert assertMsg (isDerivation x)
+      "lib.meta.getExe': The first argument is of type ${typeOf x}, but it should be a derivation instead.";
+    assert assertMsg (isString y)
+      "lib.meta.getExe': The second argument is of type ${typeOf y}, but it should be a string instead.";
+    assert assertMsg (match ".*\/.*" y == null)
+      "lib.meta.getExe': The second argument \"${y}\" is a nested path with a \"/\" character, but it should just be the name of the executable instead.";
+    "${getBin x}/bin/${y}";
 }