From f2cff9a4f1627b069ccf8866c12b222b71679aeb Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 14 Aug 2024 04:48:00 +0800 Subject: [PATCH 1/2] rust: WIP improve composiability when the other builder defines a phase Previously, if the other builder defines a phase, Rust hooks do nothing, which is not desirable. With this patch applied, if the other builder defines a phase, Rust hooks will use pre hoooks instead. TODO: maybe also change the other 3 hooks --- .../rust/hooks/cargo-build-hook.sh | 20 ++++++++++++------ .../rust/hooks/cargo-check-hook.sh | 16 ++++++++++---- .../rust/hooks/cargo-install-hook.sh | 21 +++++++++++++------ 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/pkgs/build-support/rust/hooks/cargo-build-hook.sh b/pkgs/build-support/rust/hooks/cargo-build-hook.sh index 26dde914f22aa..b538cd040c858 100644 --- a/pkgs/build-support/rust/hooks/cargo-build-hook.sh +++ b/pkgs/build-support/rust/hooks/cargo-build-hook.sh @@ -3,8 +3,6 @@ declare -a cargoBuildFlags cargoBuildHook() { echo "Executing cargoBuildHook" - runHook preBuild - # Let stdenv handle stripping, for consistency and to not break # separateDebugInfo. export "CARGO_PROFILE_${cargoBuildType@U}_STRIP"=false @@ -50,11 +48,21 @@ cargoBuildHook() { popd fi - runHook postBuild - echo "Finished cargoBuildHook" } -if [ -z "${dontCargoBuild-}" ] && [ -z "${buildPhase-}" ]; then - buildPhase=cargoBuildHook +cargoBuildPhase() { + runHook preBuild + + cargoBuildHook + + runHook postBuild +} + +if [ -z "${dontCargoBuild-}" ]; then + if [ -z "${buildPhase-}" ]; then + buildPhase=cargoBuildPhase + else + preBuildHooks+=(cargoBuildHook) + fi fi diff --git a/pkgs/build-support/rust/hooks/cargo-check-hook.sh b/pkgs/build-support/rust/hooks/cargo-check-hook.sh index 96b87dbf15b45..2e06978531e23 100644 --- a/pkgs/build-support/rust/hooks/cargo-check-hook.sh +++ b/pkgs/build-support/rust/hooks/cargo-check-hook.sh @@ -4,8 +4,6 @@ declare -a cargoTestFlags cargoCheckHook() { echo "Executing cargoCheckHook" - runHook preCheck - if [[ -n "${buildAndTestSubdir-}" ]]; then pushd "${buildAndTestSubdir}" fi @@ -46,10 +44,20 @@ cargoCheckHook() { fi echo "Finished cargoCheckHook" +} + +cargoCheckPhase() { + runHook preCheck + + cargoCheckHook runHook postCheck } -if [ -z "${dontCargoCheck-}" ] && [ -z "${checkPhase-}" ]; then - checkPhase=cargoCheckHook +if [ -z "${dontCargoCheck-}" ]; then + if [ -z "${checkPhase-}" ]; then + checkPhase=cargoCheckPhase + else + preCheckHooks+=(cargoCheckHook) + fi fi diff --git a/pkgs/build-support/rust/hooks/cargo-install-hook.sh b/pkgs/build-support/rust/hooks/cargo-install-hook.sh index 24a6e6fa9eb3f..49d6ff349a687 100644 --- a/pkgs/build-support/rust/hooks/cargo-install-hook.sh +++ b/pkgs/build-support/rust/hooks/cargo-install-hook.sh @@ -17,8 +17,6 @@ cargoInstallPostBuildHook() { cargoInstallHook() { echo "Executing cargoInstallHook" - runHook preInstall - # rename the output dir to a architecture independent one releaseDir=target/@targetSubdirectory@/$cargoBuildType @@ -37,13 +35,24 @@ cargoInstallHook() { -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \ -print0 | xargs -r -0 cp -t $out/lib rmdir --ignore-fail-on-non-empty $out/lib $out/bin - runHook postInstall echo "Finished cargoInstallHook" } +cargoInstallPhase() { + runHook preInstall + + cargoInstallHook + + runHook postInstall +} + -if [ -z "${dontCargoInstall-}" ] && [ -z "${installPhase-}" ]; then - installPhase=cargoInstallHook - postBuildHooks+=(cargoInstallPostBuildHook) +if [ -z "${dontCargoInstall-}" ]; then + if [ -z "${installPhase-}" ]; then + installPhase=cargoInstallPhase + else + preInstallHooks+=(cargoInstallHook) + fi + postBuildHooks+=(cargoInstallPostBuildHook) fi From 153dcf6f47e866a25d3508837680e77604ce4e2d Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 14 Aug 2024 04:48:31 +0800 Subject: [PATCH 2/2] emacsPackages.lspce: rewrite it as a single derivation Previously, this package is implemented as two derivations, which is inconvenient for overriding. This patch uses hooks from rustPlatform to implement it as a single derivation, which can be overridden like any other Rust packages. --- .../manual-packages/lspce/module.nix | 40 ------- .../manual-packages/lspce/package.nix | 102 ++++++++++++++++-- 2 files changed, 92 insertions(+), 50 deletions(-) delete mode 100644 pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/module.nix diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/module.nix b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/module.nix deleted file mode 100644 index 1a1f8612f64ec..0000000000000 --- a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/module.nix +++ /dev/null @@ -1,40 +0,0 @@ -{ - lib, - fetchFromGitHub, - rustPlatform, -}: - -rustPlatform.buildRustPackage { - pname = "lspce-module"; - version = "1.1.0-unstable-2024-07-14"; - - src = fetchFromGitHub { - owner = "zbelial"; - repo = "lspce"; - rev = "fd320476df89cfd5d10f1b70303c891d3b1e3c81"; - hash = "sha256-KnERYq/CvJhJIdQkpH/m82t9KFMapPl+CyZkYyujslU="; - }; - - cargoHash = "sha256-I2OobRu1hc6xc4bRrIO1FImPYBbFy1jXPcTsivbbskk="; - - checkFlags = [ - # flaky test - "--skip=msg::tests::serialize_request_with_null_params" - ]; - - # rename module without changing either suffix or location - # use for loop because there seems to be two modules on darwin systems - # https://github.com/zbelial/lspce/issues/7#issue-1783708570 - postInstall = '' - for f in $out/lib/*; do - mv --verbose $f $out/lib/lspce-module.''${f##*.} - done - ''; - - meta = { - homepage = "https://github.com/zbelial/lspce"; - description = "LSP Client for Emacs implemented as a module using Rust"; - license = lib.licenses.gpl3Only; - maintainers = with lib.maintainers; [ AndersonTorres ]; - }; -} diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/package.nix b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/package.nix index 78325d90bf78d..1046c57013943 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/package.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/lspce/package.nix @@ -1,19 +1,27 @@ { lib, - callPackage, f, markdown-mode, melpaBuild, nix-update-script, yasnippet, + fetchFromGitHub, + rustPlatform, }: let - lspce-module = callPackage ./module.nix { }; + src = fetchFromGitHub { + owner = "zbelial"; + repo = "lspce"; + rev = "fd320476df89cfd5d10f1b70303c891d3b1e3c81"; + hash = "sha256-KnERYq/CvJhJIdQkpH/m82t9KFMapPl+CyZkYyujslU="; + }; in melpaBuild { pname = "lspce"; - inherit (lspce-module) version src meta; + version = "1.1.0-unstable-2024-07-14"; + + inherit src; packageRequires = [ f @@ -21,14 +29,88 @@ melpaBuild { yasnippet ]; - # to compile lspce.el, it needs lspce-module.so - files = ''(:defaults "${lib.getLib lspce-module}/lib/lspce-module.*")''; + cargoDeps = rustPlatform.fetchCargoTarball { + inherit src; + hash = "sha256-BMbkdWsVSXRNt8kqOs05376cGnGnivHGvEugX0p3bVc="; + }; + + nativeBuildInputs = [ + rustPlatform.cargoSetupHook + rustPlatform.cargoBuildHook + rustPlatform.cargoInstallHook + rustPlatform.cargoCheckHook + ]; + + cargoBuildType = "release"; + + # Copy the default buildPhase here and switch cd to pushd/popd in + # order to not cause rebuild of other Emacs lisp packages. + buildPhase = '' + runHook preBuild + + pushd "$NIX_BUILD_TOP" + + emacs --batch -Q \ + -L "$NIX_BUILD_TOP/package-build" \ + -l "$melpa2nix" \ + -f melpa2nix-build-package \ + $ename $melpaVersion $commit + + popd + + runHook postBuild + ''; + + # Copy the default installPhase here because adding the dynamic + # module to the package tarball needs to be done at a specific time, + # i.e., between cargoInstallHook and elpa2nix-install-package. + # It cannot be done in preInstall because preInstall, as an implicit + # string hook, runs before cargoInstallHook, as a hook in + # preInstallHooks. + installPhase = '' + runHook preInstall + + archive="$NIX_BUILD_TOP/packages/$ename-$melpaVersion.el" + if [ ! -f "$archive" ]; then + archive="$NIX_BUILD_TOP/packages/$ename-$melpaVersion.tar" + fi + + echo "add the dynamic module to the package tarball because it is needed for compilation" + # rename module without changing suffix + # use for loop because there seem to be two modules on darwin systems + # https://github.com/zbelial/lspce/issues/7#issue-1783708570 + tmp_content_directory=$ename-$melpaVersion + mkdir $tmp_content_directory + for f in $out/lib/*; do + mv --verbose $f $tmp_content_directory/lspce-module.''${f##*.} + tar --verbose --append --file=$archive $tmp_content_directory/lspce-module.''${f##*.} + done + rmdir --verbose $out/lib + unset tmp_content_directory + + emacs --batch -Q \ + -l "$elpa2nix" \ + -f elpa2nix-install-package \ + "$archive" "$out/share/emacs/site-lisp/elpa" + + runHook postInstall + ''; + + doCheck = true; + cargoCheckType = "release"; + checkFlags = [ + # flaky test + "--skip=msg::tests::serialize_request_with_null_params" + ]; passthru = { - inherit lspce-module; - updateScript = nix-update-script { - attrPath = "emacsPackages.lspce.lspce-module"; - extraArgs = [ "--version=branch" ]; - }; + updateScript = nix-update-script { extraArgs = [ "--version=branch" ]; }; + }; + + meta = { + homepage = "https://github.com/zbelial/lspce"; + description = "LSP Client for Emacs implemented as a module using Rust"; + license = lib.licenses.gpl3Only; + maintainers = with lib.maintainers; [ AndersonTorres ]; }; }