Use --init-directory in Emacs 29+ (#191)

* Use `--init-directory` in Emacs 29+

This will allow a few advantages:
- We can load `early-init.el` file properly, fixing Flash Of Unstyled
Contents (FOUC) issues and improving the performance slightly
- Eventually we can drop `default.el` file loading in Home-Manager
module. Not done here since this would complicate the code without
necessity, however this can be done once Emacs 29+ is default

* Fix build in Emacs <29

* Add #checks.<arch>.init-example-el-emacsGit

* Add .#checks.x86_64-linux.init-example-el-emacsGit to CI

* Fix review issues

* Move config files to share/emacs.d

* Separate tests in two steps

* Add TODOS

* Simplify emacs-dir generation
This commit is contained in:
Thiago Kenji Okada
2022-07-10 18:46:49 +01:00
committed by GitHub
parent a59295c11e
commit 9111d480a8
4 changed files with 101 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
{ # The files would be going to ~/.config/doom (~/.doom.d)
doomPrivateDir
doomPrivateDir
/* Extra packages to install
Useful for non-emacs packages containing emacs bindings (e.g.
@@ -8,8 +8,7 @@ doomPrivateDir
Example:
extraPackages = epkgs: [ pkgs.mu ];
*/
, extraPackages ? epkgs:
[ ]
, extraPackages ? epkgs: [ ]
/* Extra configuration to source during initialization
Use this to refer other nix derivations.
@@ -36,8 +35,7 @@ doomPrivateDir
});
};
*/
, emacsPackagesOverlay ? self: super:
{ }
, emacsPackagesOverlay ? self: super: { }
/* Use bundled revision of github.com/nix-community/emacs-overlay
as `emacsPackages`.
*/
@@ -54,7 +52,8 @@ doomPrivateDir
"emacs-overlay" = fetchFromGitHub { owner = /* ...*\/; };
};
*/
, dependencyOverrides ? { }, lib, pkgs, stdenv, buildEnv, makeWrapper
, dependencyOverrides ? { }
, lib, pkgs, stdenv, buildEnv, makeWrapper
, runCommand, fetchFromGitHub, substituteAll, writeShellScript
, writeShellScriptBin, writeTextDir }:
@@ -63,6 +62,7 @@ assert (lib.assertMsg ((builtins.isPath doomPrivateDir)
"doomPrivateDir must be either a path, a derivation or a stringified store path");
let
isEmacs29 = lib.versionAtLeast emacsPackages.emacs.version "29";
flake =
(import
(let lock = with builtins; fromJSON (readFile ./flake.lock); in
@@ -193,7 +193,7 @@ let
};
# Stage 4: `extraConfig` is merged into private configuration
doomDir = pkgs.runCommand "doom-private" {
doomDir = runCommand "doom-private" {
inherit extraConfig;
passAsFile = [ "extraConfig" ];
} ''
@@ -202,23 +202,38 @@ let
chmod u+w $out/config.el
cat $extraConfigPath > $out/config.extra.el
cat > $out/config.el << EOF
(load "${builtins.toString doomPrivateDir}/config.el")
(load "${doomPrivateDir}/config.el")
(load "$out/config.extra.el")
EOF
'';
# Stage 5: catch-all wrapper capable to run doom-emacs even
# without installing ~/.emacs.d
# TODO: remove once Emacs 29+ is released and commonly available
emacs = let
load-config-from-site = writeTextDir "share/emacs/site-lisp/default.el" ''
(message "doom-emacs is not placed in `doom-private-dir',
loading from `site-lisp'")
(when (> emacs-major-version 26)
(load "${doom-emacs}/early-init.el"))
(load "${doom-emacs}/core/core-start.el")
${lib.optionalString (!isEmacs29) ''
(load "${doom-emacs}/early-init.el")
(load "${doom-emacs}/core/core-start.el")
''}
'';
in (emacsPackages.emacsWithPackages (epkgs: [ load-config-from-site ]));
# create a `emacs.d` dir to be loaded using `--init-directory` flag from Emacs 29+
# this will allow proper usage of `early-init.el`, fixing FOUC issues and improving
# startup performance
emacs-dir = runCommand "emacs-dir" { } ''
mkdir -p $out
cat > $out/early-init.el << EOF
(load "${doom-emacs}/early-init.el")
EOF
cat > $out/init.el << EOF
(load "${doom-emacs}/core/core-start.el")
EOF
'';
build-summary = writeShellScript "build-summary" ''
printf "\n${fmt.green}Successfully built nix-doom-emacs!${fmt.reset}\n"
printf "${fmt.bold} ==> doom-emacs is installed to ${doom-emacs}${fmt.reset}\n"
@@ -227,13 +242,23 @@ let
'';
in emacs.overrideAttrs (esuper:
let
# `--init-directory` is supported by Emacs 29+ only
initDirArgs = lib.optionalString isEmacs29 ''
if [[ $(basename $1) == emacs ]] || [[ $(basename $1) == emacs-* ]]; then
wrapArgs+=(--add-flags '--init-directory ${emacs-dir}')
fi
'';
cmd = ''
wrapEmacs() {
wrapProgram $1 \
--set DOOMDIR ${doomDir} \
--set NIX_DOOM_EMACS_BINARY $1 \
--set __DEBUG_doom_emacs_DIR ${doom-emacs} \
--set __DEBUG_doomLocal_DIR ${doomLocal}
local -a wrapArgs=(
--set DOOMDIR ${doomDir}
--set NIX_DOOM_EMACS_BINARY $1
--set __DEBUG_doom_emacs_DIR ${doom-emacs}
--set __DEBUG_doomLocal_DIR ${doomLocal}
)
${initDirArgs}
wrapProgram $1 "''${wrapArgs[@]}"
}
for prog in $out/bin/*; do