lib.getExe: Make more efficient

This commit is contained in:
adisbladis 2023-12-12 16:42:45 +13:00
parent 30ce319f0e
commit 0286e3119e

View file

@ -4,8 +4,8 @@
{ lib }: { lib }:
let let
inherit (lib) matchAttrs any all; inherit (lib) matchAttrs any all isDerivation getBin assertMsg;
inherit (builtins) isString; inherit (builtins) isString match typeOf;
in in
rec { rec {
@ -154,16 +154,12 @@ rec {
getExe pkgs.mustache-go getExe pkgs.mustache-go
=> "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache" => "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache"
*/ */
getExe = x: getExe = x: getExe' x (x.meta.mainProgram or (
let # This could be turned into an error when 23.05 is at end of life
y = x.meta.mainProgram or ( 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\"."
# This could be turned into an error when 23.05 is at end of life lib.getName
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\"." x
lib.getName ));
x
);
in
getExe' x y;
/* Get the path of a program of a derivation. /* Get the path of a program of a derivation.
@ -175,11 +171,11 @@ rec {
=> "/nix/store/5rs48jamq7k6sal98ymj9l4k2bnwq515-imagemagick-7.1.1-15/bin/convert" => "/nix/store/5rs48jamq7k6sal98ymj9l4k2bnwq515-imagemagick-7.1.1-15/bin/convert"
*/ */
getExe' = x: y: getExe' = x: y:
assert lib.assertMsg (lib.isDerivation x) assert assertMsg (isDerivation x)
"lib.meta.getExe': The first argument is of type ${builtins.typeOf x}, but it should be a derivation instead."; "lib.meta.getExe': The first argument is of type ${typeOf x}, but it should be a derivation instead.";
assert lib.assertMsg (lib.isString y) assert assertMsg (isString y)
"lib.meta.getExe': The second argument is of type ${builtins.typeOf y}, but it should be a string instead."; "lib.meta.getExe': The second argument is of type ${typeOf y}, but it should be a string instead.";
assert lib.assertMsg (builtins.length (lib.splitString "/" y) == 1) 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."; "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}"; "${getBin x}/bin/${y}";
} }