Skip to content

Commit

Permalink
Merge pull request #330589 from linj-fork/pr/emacs-builder-finaAttrs
Browse files Browse the repository at this point in the history
emacs: teach elisp builders the finalAttrs pattern
  • Loading branch information
jian-lin committed Aug 15, 2024
2 parents 688ae1b + bdd7734 commit 4d80793
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 43 deletions.
19 changes: 11 additions & 8 deletions pkgs/applications/editors/emacs/build-support/elpa.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
{ lib, stdenv, emacs, texinfo, writeText }:

let
handledArgs = [ "meta" ];
genericBuild = import ./generic.nix { inherit lib stdenv emacs texinfo writeText; };
libBuildHelper = import ./lib-build-helper.nix;

in

libBuildHelper.extendMkDerivation' genericBuild (finalAttrs:

{ pname
, version
, src
, dontUnpack ? true
, meta ? {}
, ...
}@args:

genericBuild ({
{

elpa2nix = args.elpa2nix or ./elpa2nix.el;

dontUnpack = true;
inherit dontUnpack;

installPhase = ''
installPhase = args.installPhase or ''
runHook preInstall
emacs --batch -Q -l ${./elpa2nix.el} \
emacs --batch -Q -l "$elpa2nix" \
-f elpa2nix-install-package \
"$src" "$out/share/emacs/site-lisp/elpa"
Expand All @@ -34,4 +37,4 @@ genericBuild ({
} // meta;
}

// removeAttrs args handledArgs)
)
32 changes: 16 additions & 16 deletions pkgs/applications/editors/emacs/build-support/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

let
inherit (lib) optionalAttrs;
handledArgs = [ "buildInputs" "nativeBuildInputs" "packageRequires" "propagatedUserEnvPkgs" "meta" ]
++ lib.optionals (emacs.withNativeCompilation or false) [ "postInstall" ];

setupHook = writeText "setup-hook.sh" ''
source ${./emacs-funcs.sh}
Expand All @@ -21,13 +19,16 @@ let
fi
'';

libBuildHelper = import ./lib-build-helper.nix;

in

{ pname
, version
, buildInputs ? []
libBuildHelper.extendMkDerivation' stdenv.mkDerivation (finalAttrs:

{ buildInputs ? []
, nativeBuildInputs ? []
, packageRequires ? []
, propagatedBuildInputs ? []
, propagatedUserEnvPkgs ? []
, postInstall ? ""
, meta ? {}
Expand All @@ -36,10 +37,10 @@ in
, ...
}@args:

stdenv.mkDerivation (finalAttrs: ({
name = "emacs-${pname}-${finalAttrs.version}";
{
name = args.name or "emacs-${finalAttrs.pname}-${finalAttrs.version}";

unpackCmd = ''
unpackCmd = args.unpackCmd or ''
case "$curSrc" in
*.el)
# keep original source filename without the hash
Expand All @@ -55,14 +56,13 @@ stdenv.mkDerivation (finalAttrs: ({
esac
'';

buildInputs = packageRequires ++ buildInputs;
inherit packageRequires;
buildInputs = finalAttrs.packageRequires ++ buildInputs;
nativeBuildInputs = [ emacs texinfo ] ++ nativeBuildInputs;
propagatedBuildInputs = packageRequires;
propagatedUserEnvPkgs = packageRequires ++ propagatedUserEnvPkgs;

inherit setupHook;
propagatedBuildInputs = finalAttrs.packageRequires ++ propagatedBuildInputs;
propagatedUserEnvPkgs = finalAttrs.packageRequires ++ propagatedUserEnvPkgs;

doCheck = false;
setupHook = args.setupHook or setupHook;

meta = {
broken = false;
Expand All @@ -74,7 +74,7 @@ stdenv.mkDerivation (finalAttrs: ({

// optionalAttrs (emacs.withNativeCompilation or false) {

addEmacsNativeLoadPath = true;
addEmacsNativeLoadPath = args.addEmacsNativeLoadPath or true;

inherit turnCompilationWarningToError ignoreCompilationError;

Expand All @@ -97,4 +97,4 @@ stdenv.mkDerivation (finalAttrs: ({
'' + postInstall;
}

// removeAttrs args handledArgs))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
extendMkDerivation' =
mkDerivationBase: attrsOverlay: fpargs:
(mkDerivationBase fpargs).overrideAttrs attrsOverlay;
}
32 changes: 18 additions & 14 deletions pkgs/applications/editors/emacs/build-support/melpa.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{ lib, stdenv, fetchFromGitHub, emacs, texinfo, writeText }:

let
handledArgs = [ "meta" "preUnpack" "postUnpack" ];
genericBuild = import ./generic.nix { inherit lib stdenv emacs texinfo writeText; };
libBuildHelper = import ./lib-build-helper.nix;

packageBuild = stdenv.mkDerivation {
name = "package-build";
Expand All @@ -29,6 +29,8 @@ let

in

libBuildHelper.extendMkDerivation' genericBuild (finalAttrs:

{ /*
pname: Nix package name without special symbols and without version or
"emacs-" prefix.
Expand All @@ -51,7 +53,7 @@ in
This will be written into the generated package but it is not needed during
the build process.
*/
, commit ? (args.src.rev or "unknown")
, commit ? (finalAttrs.src.rev or "unknown")
/*
files: Optional recipe property specifying the files used to build the package.
If null, do not set it in recipe, keeping the default upstream behaviour.
Expand All @@ -62,24 +64,26 @@ in
recipe: Optional MELPA recipe.
Default: a minimally functional recipe
*/
, recipe ? (writeText "${pname}-recipe" ''
(${ename} :fetcher git :url ""
${lib.optionalString (files != null) ":files ${files}"})
, recipe ? (writeText "${finalAttrs.pname}-recipe" ''
(${finalAttrs.ename} :fetcher git :url ""
${lib.optionalString (finalAttrs.files != null) ":files ${finalAttrs.files}"})
'')
, preUnpack ? ""
, postUnpack ? ""
, meta ? {}
, ...
}@args:

genericBuild ({
{

elpa2nix = args.elpa2nix or ./elpa2nix.el;
melpa2nix = args.melpa2nix or ./melpa2nix.el;

elpa2nix = ./elpa2nix.el;
melpa2nix = ./melpa2nix.el;
inherit commit ename files recipe;

inherit packageBuild commit ename recipe;
packageBuild = args.packageBuild or packageBuild;

melpaVersion =
melpaVersion = args.melpaVersion or (
let
parsed = lib.flip builtins.match version
# match <version>-unstable-YYYY-MM-DD format
Expand All @@ -90,7 +94,7 @@ genericBuild ({
in
if unstableVersionInNixFormat
then date + "." + time
else version;
else finalAttrs.version);

preUnpack = ''
mkdir -p "$NIX_BUILD_TOP/recipes"
Expand All @@ -108,7 +112,7 @@ genericBuild ({
ln -s "$NIX_BUILD_TOP/$sourceRoot" "$NIX_BUILD_TOP/working/$ename"
'' + postUnpack;

buildPhase = ''
buildPhase = args.buildPhase or ''
runHook preBuild
pushd "$NIX_BUILD_TOP"
Expand All @@ -124,7 +128,7 @@ genericBuild ({
runHook postBuild
'';

installPhase = ''
installPhase = args.installPhase or ''
runHook preInstall
archive="$NIX_BUILD_TOP/packages/$ename-$melpaVersion.el"
Expand All @@ -145,4 +149,4 @@ genericBuild ({
} // meta;
}

// removeAttrs args handledArgs)
)
15 changes: 10 additions & 5 deletions pkgs/applications/editors/emacs/build-support/trivial.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@

{ callPackage, lib, ... }@envargs:

let
libBuildHelper = import ./lib-build-helper.nix;
in

libBuildHelper.extendMkDerivation' (callPackage ./generic.nix envargs) (finalAttrs:

args:

callPackage ./generic.nix envargs ({
buildPhase = ''
{
buildPhase = args.buildPhase or ''
runHook preBuild
emacs -L . --batch -f batch-byte-compile *.el
runHook postBuild
'';

installPhase = ''
installPhase = args.installPhase or ''
runHook preInstall
LISPDIR=$out/share/emacs/site-lisp
install -d $LISPDIR
install *.el *.elc $LISPDIR
emacs --batch -l package --eval "(package-generate-autoloads \"${args.pname}\" \"$LISPDIR\")"
runHook postInstall
'';
}

// args)
)

0 comments on commit 4d80793

Please sign in to comment.