Emacs: Difference between revisions

imported>LoganWalls
Re-organized page and added additional info about installing Emacs, the community overlay, and managing packages
imported>LoganWalls
Added info about darwin-specific issues
Line 16: Line 16:


== Installation ==
== Installation ==
{{warning| Certain issues are possible, when mixing different versions of Emacs, in particular a configuration file tailored towards emacs with native compilation, may misbehave on non-native compiling versions, unless only the emacs lisp code is shared between them.}}
Several stable versions of emacs are available by default in nixpkgs, and unstable branches are also available via the community overaly. Also, see the [https://nixos.org/manual/nixos/stable/index.html#module-services-emacs NixOS manual entry for Emacs] for additional details about installation and configuration.
Several stable versions of emacs are available by default in nixpkgs, and unstable branches are also available via the community overaly. Also, see the [https://nixos.org/manual/nixos/stable/index.html#module-services-emacs NixOS manual entry for Emacs] for additional details about installation and configuration.


Line 55: Line 53:
</nowiki>}}
</nowiki>}}


=== Available Emacs variants ===
{{warning| Certain issues are possible, when mixing different versions of Emacs, in particular a configuration file tailored towards emacs with native compilation, may misbehave on non-native compiling versions, unless only the emacs lisp code is shared between them.}}


==== Available Emacs variants ====
==== Stable (nixpkgs) ====
===== Stable (nixpkgs) =====
Emacs is available in nixpkgs under the names <code>emacs</code> and <code>emacs-gtk</code>.
Emacs is available in nixpkgs under the names <code>emacs</code> and <code>emacs-gtk</code>.
[https://github.com/NixOS/nixpkgs/pull/189543 Since 2022-09], the package called <code>emacs</code> now installs the lucid toolkit instead of gtk. The reason is that emacs is less stable with gtk especially in daemon mode. However, the lucid flavor of emacs will not take into account the gtk theme (since it is not even gtk) and looks quite… ugly (see comparisons [https://emacs.stackexchange.com/questions/33065/on-linux-why-should-one-choose-lucid-over-gtk-gui-for-emacs here]). If you still prefer the gtk version of emacs, you can instead install <code>emacs-gtk</code> (before 2022-09 this package does not exist and emacs defaults to the gtk version).
[https://github.com/NixOS/nixpkgs/pull/189543 Since 2022-09], the package called <code>emacs</code> now installs the lucid toolkit instead of gtk. The reason is that emacs is less stable with gtk especially in daemon mode. However, the lucid flavor of emacs will not take into account the gtk theme (since it is not even gtk) and looks quite… ugly (see comparisons [https://emacs.stackexchange.com/questions/33065/on-linux-why-should-one-choose-lucid-over-gtk-gui-for-emacs here]). If you still prefer the gtk version of emacs, you can instead install <code>emacs-gtk</code> (before 2022-09 this package does not exist and emacs defaults to the gtk version).


===== Unstable (community overlay) =====
==== Unstable (community overlay) ====
The [https://github.com/nix-community/emacs-overlay community overlay] provides nightly versions of the Emacs unstable branches, ELPA / MELPA packages, and [https://github.com/ch11ng/exwm EXWM] + its dependencies. '''To use these, first apply the overlay (instructions below), which will make the packages available in nixpkgs.''' Then you can follow the normal nixpkgs installation instructions (above), but use your package of choice from the overlay (e.g. <code>pkgs.emacsGit</code>) in place of <code>pkgs.emacs</code>. See the [https://github.com/nix-community/emacs-overlay#emacs-overlay README] for a complete list of packages provided, and their differences.
The [https://github.com/nix-community/emacs-overlay community overlay] provides nightly versions of the Emacs unstable branches, ELPA / MELPA packages, and [https://github.com/ch11ng/exwm EXWM] + its dependencies. '''To use these, first apply the overlay (instructions below), which will make the packages available in nixpkgs.''' Then you can follow the normal nixpkgs installation instructions (above), but use your package of choice from the overlay (e.g. <code>pkgs.emacsGit</code>) in place of <code>pkgs.emacs</code>. See the [https://github.com/nix-community/emacs-overlay#emacs-overlay README] for a complete list of packages provided, and their differences.


====== With flakes ======
===== With flakes =====
Using a system flake, one can specify the specific revision of the overlay as a flake input, for example:
Using a system flake, one can specify the specific revision of the overlay as a flake input, for example:


Line 72: Line 71:
  nixpkgs.overlays = [ (import self.inputs.emacs-overlay) ];
  nixpkgs.overlays = [ (import self.inputs.emacs-overlay) ];


======  Without flakes ======
=====  Without flakes =====
For installing one of the unstable branches of emacs, add the following lines to {{ic|/etc/nixos/configuration.nix}}
For installing one of the unstable branches of emacs, add the following lines to {{ic|/etc/nixos/configuration.nix}}
{{file|configuration.nix|nix|<nowiki>
{{file|configuration.nix|nix|<nowiki>
Line 84: Line 83:
   ];
   ];
}
}
</nowiki>}}
=== Darwin (macOS) ===
Nixpkgs provides several of the "Mac Port" versions of Emacs, which have been patched to provide better integration with macOS (see the [https://nixos.org/manual/nixos/stable/index.html#module-services-emacs-releases NixOS manual entry for a full list of packages). However, those packages typically track the stable releases of Emacs.
If you would like to use the latest version of Emacs on Darwin, one option is to use a package like <code>emacsPgkt</code> from the community overlay (see above), and apply patches yourself via an override. For example, here is a derivation that applies the patches from the [https://github.com/d12frosted/homebrew-emacs-plus <code>emacs-plus</code> homebrew formula]:
<syntaxhighlight lang="nix">
pkgs.emacsPgtk.overrideAttrs (old: {
      patches =
        (old.patches or [])
        ++ [
          # Fix OS window role (needed for window managers like yabai)
          (fetchpatch {
            url = "https://raw.githubusercontent.com/d12frosted/homebrew-emacs-plus/master/patches/emacs-28/fix-window-role.patch";
            sha256 = "0c41rgpi19vr9ai740g09lka3nkjk48ppqyqdnncjrkfgvm2710z";
          })
          # Use poll instead of select to get file descriptors
          (fetchpatch {
            url = "https://raw.githubusercontent.com/d12frosted/homebrew-emacs-plus/master/patches/emacs-29/poll.patch";
            sha256 = "0j26n6yma4n5wh4klikza6bjnzrmz6zihgcsdx36pn3vbfnaqbh5";
          })
          # Enable rounded window with no decoration
          (fetchpatch {
            url = "https://raw.githubusercontent.com/d12frosted/homebrew-emacs-plus/master/patches/emacs-29/round-undecorated-frame.patch";
            sha256 = "111i0r3ahs0f52z15aaa3chlq7ardqnzpwp8r57kfsmnmg6c2nhf";
          })
          # Make Emacs aware of OS-level light/dark mode
          (fetchpatch {
            url = "https://raw.githubusercontent.com/d12frosted/homebrew-emacs-plus/master/patches/emacs-28/system-appearance.patch";
            sha256 = "14ndp2fqqc95s70fwhpxq58y8qqj4gzvvffp77snm2xk76c1bvnn";
          })
        ];
    });
  };
}
</syntaxhighlight>
==== Window manager integration ====
Out of the box, non-"Mac Port" versions of Emacs will not be picked up properly by window managers like [https://github.com/koekeishiya/yabai Yabai] because [https://github.com/koekeishiya/yabai/issues/86#issuecomment-507537023 Emacs does not set the correct macOS window role]. This can be fixed with a patch (e.g. the first patch in the example above). However, even with the patch, Yabai may not correctly pick up Emacs if you invoke the <code>emacs</code> binary directly from a shell. For Emacs to work properly with window managers you must invoke it by running the macOS app that is generated when you install Emacs with nix. You can setup an alias to do this like so (replace <code>pkgs.emacs</code> with the package you are using):
{{file|home.nix|nix|<nowiki>
programs.zsh = {
enable = true;
shellAliases = {
  emacs = "${pkgs.emacs}/Applications/Emacs.app/Contents/MacOS/Emacs";
};
};
</nowiki>}}
</nowiki>}}