From 4d134f63fdebb45b9f723c99f635617c8acf50e9 Mon Sep 17 00:00:00 2001 From: Benjamin Sparks Date: Tue, 3 Jun 2025 18:57:45 +0200 Subject: [PATCH] buildGraalvmNativeImage: use lib.extendMkDerivation Co-authored-by: Philip Taron --- .../build-graalvm-native-image/default.nix | 170 ++++++++++-------- pkgs/top-level/all-packages.nix | 5 +- 2 files changed, 92 insertions(+), 83 deletions(-) diff --git a/pkgs/build-support/build-graalvm-native-image/default.nix b/pkgs/build-support/build-graalvm-native-image/default.nix index e1e4df52f02a..fc7bf7741601 100644 --- a/pkgs/build-support/build-graalvm-native-image/default.nix +++ b/pkgs/build-support/build-graalvm-native-image/default.nix @@ -2,95 +2,107 @@ lib, stdenv, glibcLocales, - # The GraalVM derivation to use - graalvmDrv, removeReferencesTo, - executable ? args.pname, - # JAR used as input for GraalVM derivation, defaults to src - jar ? args.src, - dontUnpack ? (jar == args.src), - # Default native-image arguments. You probably don't want to set this, - # except in special cases. In most cases, use extraNativeBuildArgs instead - nativeImageBuildArgs ? [ - (lib.optionalString stdenv.hostPlatform.isDarwin "-H:-CheckToolchain") - (lib.optionalString ( - stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64 - ) "-H:PageSize=64K") - "-H:Name=${executable}" - "-march=compatibility" - "--verbose" - ], - # Extra arguments to be passed to the native-image - extraNativeImageBuildArgs ? [ ], - # XMX size of GraalVM during build - graalvmXmx ? "-J-Xmx6g", - meta ? { }, - LC_ALL ? "en_US.UTF-8", - ... -}@args: + graalvmPackages, +}: -let - extraArgs = builtins.removeAttrs args [ - "lib" - "stdenv" - "glibcLocales" - "jar" - "dontUnpack" - "LC_ALL" - "meta" - "buildPhase" - "nativeBuildInputs" - "installPhase" - "postInstall" +lib.extendMkDerivation { + constructDrv = stdenv.mkDerivation; + + excludeDrvArgNames = [ + "executable" + "extraNativeImageBuildArgs" + "graalvmDrv" + "graalvmXmx" + "nativeImageBuildArgs" ]; -in -stdenv.mkDerivation ( - { - inherit dontUnpack jar; - env = { inherit LC_ALL; }; + extendDrvArgs = + finalAttrs: + { + dontUnpack ? true, + strictDeps ? true, + __structuredAttrs ? true, - nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ - graalvmDrv - glibcLocales - removeReferencesTo - ]; + # The GraalVM derivation to use + graalvmDrv ? graalvmPackages.graalvm-ce, - nativeImageBuildArgs = nativeImageBuildArgs ++ extraNativeImageBuildArgs ++ [ graalvmXmx ]; + executable ? finalAttrs.meta.mainProgram, - buildPhase = - args.buildPhase or '' - runHook preBuild + # Default native-image arguments. You probably don't want to set this, + # except in special cases. In most cases, use extraNativeBuildArgs instead + nativeImageBuildArgs ? [ + (lib.optionalString stdenv.hostPlatform.isDarwin "-H:-CheckToolchain") + (lib.optionalString ( + stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64 + ) "-H:PageSize=64K") + "-H:Name=${executable}" + "-march=compatibility" + "--verbose" + ], - native-image -jar "$jar" ''${nativeImageBuildArgs[@]} + # Extra arguments to be passed to the native-image + extraNativeImageBuildArgs ? [ ], - runHook postBuild + # XMX size of GraalVM during build + graalvmXmx ? "-J-Xmx6g", + + env ? { }, + meta ? { }, + passthru ? { }, + ... + }@args: + { + env = { + LC_ALL = "en_US.UTF-8"; + } // env; + + inherit dontUnpack strictDeps __structuredAttrs; + + nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ + graalvmDrv + glibcLocales + removeReferencesTo + ]; + + # `nativeBuildInputs` does not allow `graalvmDrv`'s propagatedBuildInput to reach here this package. + # As its `propagatedBuildInputs` is required for the build process with `native-image`, we must add it here as well. + buildInputs = [ graalvmDrv ]; + + nativeImageArgs = nativeImageBuildArgs ++ extraNativeImageBuildArgs ++ [ graalvmXmx ]; + + buildPhase = + args.buildPhase or '' + runHook preBuild + + native-image -jar "$src" ''${nativeImageArgs[@]} + + runHook postBuild + ''; + + installPhase = + args.installPhase or '' + runHook preInstall + + install -Dm755 ${executable} -t $out/bin + + runHook postInstall + ''; + + postInstall = '' + remove-references-to -t ${graalvmDrv} $out/bin/${executable} + ${args.postInstall or ""} ''; - installPhase = - args.installPhase or '' - runHook preInstall + disallowedReferences = [ graalvmDrv ]; - install -Dm755 ${executable} -t $out/bin + passthru = { + inherit graalvmDrv; + } // passthru; - runHook postInstall - ''; - - postInstall = '' - remove-references-to -t ${graalvmDrv} $out/bin/${executable} - ${args.postInstall or ""} - ''; - - disallowedReferences = [ graalvmDrv ]; - - passthru = { inherit graalvmDrv; }; - - meta = { - # default to graalvm's platforms - platforms = graalvmDrv.meta.platforms; - # default to executable name - mainProgram = executable; - } // meta; - } - // extraArgs -) + meta = { + # default to graalvm's platforms + inherit (graalvmDrv.meta) platforms; + } // meta; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 899a667a781e..a8730f478569 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -5747,10 +5747,7 @@ with pkgs; openjdk_headless = jdk_headless; graalvmPackages = recurseIntoAttrs (callPackage ../development/compilers/graalvm { }); - buildGraalvmNativeImage = - (callPackage ../build-support/build-graalvm-native-image { - graalvmDrv = graalvmPackages.graalvm-ce; - }).override; + buildGraalvmNativeImage = callPackage ../build-support/build-graalvm-native-image { }; openshot-qt = libsForQt5.callPackage ../applications/video/openshot-qt { };