Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ rec {
licstr: default: lowercaseLicenses.${lib.toLower licstr} or default;

/**
Get the path to the main program of a package based on meta.mainProgram
Get the path to the main program of a package based on `mainProgram`.

# Inputs

Expand Down Expand Up @@ -432,12 +432,12 @@ rec {
getExe =
x:
getExe' x (
x.meta.mainProgram or (
x.mainProgram or 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\"."
} does not have the 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 `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
)
Expand Down
10 changes: 10 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4082,6 +4082,16 @@ runTests {
};

testGetExeOutput = {
expr = getExe {
type = "derivation";
out = "somelonghash";
bin = "somelonghash";
mainProgram = "mainProgram";
};
expected = "somelonghash/bin/mainProgram";
};

testGetExeOutputMeta = {
expr = getExe {
type = "derivation";
out = "somelonghash";
Expand Down
5 changes: 3 additions & 2 deletions pkgs/by-name/he/hello/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ stdenv.mkDerivation (finalAttrs: {

# Give hello some install checks for testing purpose.
postInstallCheck = ''
stat "''${!outputBin}/bin/${finalAttrs.meta.mainProgram}"
stat "''${!outputBin}/bin/${finalAttrs.mainProgram}"
'';

passthru.tests = {
Expand All @@ -43,6 +43,8 @@ stdenv.mkDerivation (finalAttrs: {

passthru.tests.run = callPackage ./test.nix { hello = finalAttrs.finalPackage; };

mainProgram = "hello";

meta = {
description = "Program that produces a familiar, friendly greeting";
longDescription = ''
Expand All @@ -53,7 +55,6 @@ stdenv.mkDerivation (finalAttrs: {
changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v${finalAttrs.version}";
license = lib.licenses.gpl3Plus;
maintainers = with lib.maintainers; [ stv0g ];
mainProgram = "hello";
platforms = lib.platforms.all;
};
})
12 changes: 6 additions & 6 deletions pkgs/by-name/ve/versionCheckHook/hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ versionCheckHook(){
local cmdProgram cmdArg echoPrefix
if [[ ! -z "${versionCheckProgram-}" ]]; then
cmdProgram="$versionCheckProgram"
elif [[ ! -z "${NIX_MAIN_PROGRAM-}" ]]; then
cmdProgram="${!outputBin}/bin/${NIX_MAIN_PROGRAM}"
elif [[ ! -z "${mainProgram-}" ]]; then
cmdProgram="${!outputBin}/bin/${mainProgram}"
elif [[ ! -z "${pname-}" ]]; then
echo "versionCheckHook: Package \`${pname}\` does not have the \`meta.mainProgram\` attribute." \
echo "versionCheckHook: Package \`${pname}\` does not have the \`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." \
"If the package has a main program, please set \`mainProgram\` in its definition to make this warning go away." \
"Should the binary that outputs the intended version differ from \`meta.mainProgram\`, consider setting \`versionCheckProgram\` instead." >&2
cmdProgram="${!outputBin}/bin/${pname}"
else
echo "versionCheckHook: \$NIX_MAIN_PROGRAM, \$versionCheckProgram and \$pname are all empty, so" \
echo "versionCheckHook: \$mainProgram, \$versionCheckProgram and \$pname are all empty, so" \
"we don't know how to run the versionCheckPhase." \
"To fix this, set one of \`meta.mainProgram\` or \`versionCheckProgram\`." >&2
"To fix this, set one of \`mainProgram\` or \`versionCheckProgram\`." >&2
exit 2
fi

Expand Down
8 changes: 8 additions & 0 deletions pkgs/stdenv/generic/check-meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,14 @@ let
]
++ optional (hasOutput "man") "man";
}
// optionalAttrs (attrs ? mainProgram) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pull the same trick as #430969 here too.

# This is used within derivations, but is also useful metadata
# for external consumers.
#
# TODO: Phase out setting this in `meta` (but still keep
# inheriting it here).
inherit (attrs) mainProgram;
}
// (filterAttrs (_: v: v != null) {
# CI scripts look at these to determine pings. Note that we should filter nulls out of this,
# or nix-env complains: https://github.com/NixOS/nix/blob/2.18.8/src/nix-env/nix-env.cc#L963
Expand Down
9 changes: 3 additions & 6 deletions pkgs/stdenv/generic/make-derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,6 @@ let
);

let
mainProgram = meta.mainProgram or null;
env' = env // lib.optionalAttrs (mainProgram != null) { NIX_MAIN_PROGRAM = mainProgram; };

derivationArg = makeDerivationArgument (
removeAttrs attrs ([
"meta"
Expand All @@ -776,11 +773,11 @@ let

checkedEnv =
let
overlappingNames = attrNames (builtins.intersectAttrs env' derivationArg);
overlappingNames = attrNames (builtins.intersectAttrs env derivationArg);
prettyPrint = lib.generators.toPretty { };
makeError =
name:
" - ${name}: in `env`: ${prettyPrint env'.${name}}; in derivation arguments: ${
" - ${name}: in `env`: ${prettyPrint env.${name}}; in derivation arguments: ${
prettyPrint derivationArg.${name}
}";
errors = lib.concatMapStringsSep "\n" makeError overlappingNames;
Expand All @@ -794,7 +791,7 @@ let
assert assertMsg (isString v || isBool v || isInt v || isDerivation v)
"The `env` attribute set can only contain derivation, string, boolean or integer attributes. The `${n}` attribute is of type ${builtins.typeOf v}.";
v
) env';
) env;

# Fixed-output derivations may not reference other paths, which means that
# for a fixed-output derivation, the corresponding inputDerivation should
Expand Down
Loading