From 0286e3119e3c2035b8c1a4a0d61f2e1cf6e6f98f Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 12 Dec 2023 16:42:45 +1300 Subject: lib.getExe: Make more efficient --- lib/meta.nix | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'lib/meta.nix') 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}"; } -- cgit 1.4.1