about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-07-31 18:56:19 +0200
committerRobert Hensing <robert@roberthensing.nl>2023-07-31 18:56:19 +0200
commite6e16bc11843045aa95dbe1194f9bf0cbb083e0e (patch)
tree2ea83018b8dd8c70d8d911ecf566a4fbb76a640e /lib
parent470d17ea5d8f6f9f3035bbaaa6d5123352d9b0ed (diff)
downloadnixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.tar
nixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.tar.gz
nixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.tar.bz2
nixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.tar.lz
nixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.tar.xz
nixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.tar.zst
nixlib-e6e16bc11843045aa95dbe1194f9bf0cbb083e0e.zip
lib.getExe: Do not make assumptions about the main program
Before this commit, getExe assumes that if `meta.mainProgram` is unset,
it has a main program that's named after the package name.

While this is probable, it leads to a bad error when the assumption does
not hold. If the user called `getExe` themselves, they might narrow down
the location of the assumption quite easily, but if that's not the case,
they'll have to go down the rabbit hole to figure out what went wrong.

For example, a NixOS module may use `lib.getExe` on a package-typed option
which is then used in the system configuration. This then typically leads
to a failure *after* deployment, which is bad, and it's quite likely that
the user will debug the package output contents before digging through the
NixOS module, which is bad.
Furthermore the `getExe` call is rather inconspicuous as it does not
contain something like "/bin/foo", which is bad.
Also modules can be hard to read for a newbie, which is bad.

All of this can be avoided by requiring `meta.mainProgram`.
Many packages already have the attribute, and I would expect 80% of
`getExe` usages to be covered by 20% of packages, because plenty of
packages aren't used with `getExe` anyway.

Finally we could make an effort to set `mainProgram` semi-automatically
using `nix-index`.
Diffstat (limited to 'lib')
-rw-r--r--lib/meta.nix6
1 files changed, 5 insertions, 1 deletions
diff --git a/lib/meta.nix b/lib/meta.nix
index 5fd55c4e90d6..d32a37fe61d7 100644
--- a/lib/meta.nix
+++ b/lib/meta.nix
@@ -144,5 +144,9 @@ rec {
        => "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache"
   */
   getExe = x:
-    "${lib.getBin x}/bin/${x.meta.mainProgram or (lib.getName x)}";
+    "${lib.getBin x}/bin/${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, specify the full path to the program, such as \"\${lib.getBin foo}/bin/bar\"."
+      lib.getName x
+    )}";
 }