Emacs: Difference between revisions

Layer-09 (talk | contribs)
mNo edit summary
Pigs (talk | contribs)
Configuration: provide a clearer list of possible ways to configure emacs
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{application infobox
{{application infobox
   |title=GNU Emacs
   |name=GNU Emacs
   |image=EmacsIcon.svg
   |image=EmacsIcon.svg
   |type=Text Editor
   |type=Text Editor
Line 9: Line 9:
   |github=
   |github=
}}
}}
<strong>Emacs</strong> is a free and open-source text editor known for its exceptional extensibility and adaptability. It can be customized into anything from a simple editor to a full development environment or productivity tool. Emacs features built-in self-documentation, syntax-aware editing, and a vast ecosystem of community-developed packages.<ref>https://www.gnu.org/software/emacs/</ref>
<strong>Emacs</strong> is a free and open-source text editor known for its exceptional extensibility and adaptability. It can be customized into anything from a simple editor to a full development environment or productivity tool. Emacs features built-in self-documentation, syntax-aware editing, and a vast ecosystem of community-developed packages.<ref>https://www.gnu.org/software/emacs/</ref>


Line 32: Line 30:


To install Emacs system-wide, making it available to all users, add the following to your configuration:
To install Emacs system-wide, making it available to all users, add the following to your configuration:
<syntaxhighlight lang="nix">
 
# Example for /etc/nixos/configuration.nix
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
environment.systemPackages = [
environment.systemPackages = [
   pkgs.emacs
   pkgs.emacs
];
];
</nowiki>
}}
Alternatively, Emacs can be installed specific to a user via [[Home Manager]]:


# User-specific installation (in ~/.config/nixpkgs/home.nix)
{{file|home.nix|nix|<nowiki>
home.packages = [
home.packages = [
   pkgs.emacs
   pkgs.emacs
];
];
</syntaxhighlight>
</nowiki>
}}
 
After rebuilding your system with <code>nixos-rebuild switch</code> or <code>home-manager switch</code>, Emacs will be installed and accessible.
After rebuilding your system with <code>nixos-rebuild switch</code> or <code>home-manager switch</code>, Emacs will be installed and accessible.


== Configuration ==
== Configuration ==
{{Note|Currently, configuring Emacs is possible by using Home Manager. A workaround for a global configuration is highlighted in the advanced section.}}


==== Basic ====
==== NixOS System Configuration ====
<syntaxhighlight lang="nix">
 
programs.emacs = {
System wide configuration of Emacs is limited to only the [https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html Emacs daemon]. To enable Emacs daemon user services system-wide and set as default editor:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.emacs = {
   enable = true;
   enable = true;
   defaultEditor = true;
   defaultEditor = true;
};
};
</syntaxhighlight>
</nowiki>
}}
 
Use <code>emacsclient</code> to connect to the daemon. For a full list of module configuration options, see {{nixos:option|services.emacs}}.
 
==== Home Manager ====
 
[[Home Manager]] provides a larger set of user-specific configuration options for Emacs.


==== Advanced ====
A minimal configuration that installs Emacs alongside <code>nix-mode</code> and <code>nixfmt</code> packages:
<syntaxhighlight lang="nix">
# Global Configuration
# Emacs is running as a daemon here, accesible via the "emacsclient" command
services.emacs = {
  enable = true;
  package = pkgs.emacs;
};


# Home Configuration
{{file|home.nix|nix|<nowiki>
programs.emacs = {
programs.emacs = {
   enable = true;
   enable = true;
   package = pkgs.emacs;  # replace with pkgs.emacs-gtk if desired
   package = pkgs.emacs;  # replace with pkgs.emacs-gtk if desired
   defaultEditor = true;
   extraPackages = epkgs: [
    epkgs.nix-mode
    epkgs.nixfmt
  ];
   extraConfig = ''
   extraConfig = ''
     (setq standard-indent 2)
     (setq standard-indent 2)
   '';
   '';
};
};
</syntaxhighlight>
</nowiki>
}}
 
{{note| An alternative option to setting the config inside a Nix string, you can load the config file by <code>extraConfig <nowiki>=</nowiki> builtins.readFile ./emacs_config.el</code>.}}
 
To search for Emacs plugins within the package set, see {{nixos:package|emacsPackages.*}}. A full list of Home Manager configuration module options can be found [https://home-manager-options.extranix.com/?query=programs.emacs here].
 
Home Manager also provides a configuration module for enabling the Emacs daemon:
 
{{file|home.nix|nix|<nowiki>
# Emacs is running as a daemon here, accesible via the "emacsclient" command
services.emacs = {
  enable = true;
  defaultEditor = true;
};
</nowiki>
}}
 
See [https://home-manager-options.extranix.com/?query=services.emacs the module options] for <code>services.emacs</code> configurations options.


== Tips and Tricks ==
== Tips and Tricks ==
Line 80: Line 108:
==== Installing Packages ====
==== Installing Packages ====
{{Note|Emacs, much like NixOS can rebuild and re-fetch all of its packages based on its initialization file alone, if one chooses to use an extension called "use-package". Such a configuration file can be version controlled and used in all compatible operating systems.}}
{{Note|Emacs, much like NixOS can rebuild and re-fetch all of its packages based on its initialization file alone, if one chooses to use an extension called "use-package". Such a configuration file can be version controlled and used in all compatible operating systems.}}
One can mix and match whether Emacs packages are installed by Nix or Emacs. This can be particularly useful for Emacs packages that need to be built, such as vterm. One way to install Emacs packages through Nix is by the following, replacing {{ic|emacsPgtkNativeComp}} with the variant in use:<syntaxhighlight lang="nix">
One can mix and match whether Emacs packages are installed by Nix or Emacs. This can be particularly useful for Emacs packages that need to be built, such as vterm. One way to install Emacs packages through Nix is by the following, replacing {{ic|emacs-pgtk}} with the variant in use:<syntaxhighlight lang="nix">
environment.systemPackages = with pkgs;
environment.systemPackages = with pkgs;
[ ...
[ ...
   ((emacsPackagesFor emacsPgtkNativeComp).emacsWithPackages (
   ((emacsPackagesFor emacs-pgtk).emacsWithPackages (
     epkgs: [ epkgs.vterm ]
     epkgs: [ epkgs.vterm ]
   ))
   ))
Line 91: Line 119:
# To make the packages available to emacsclient, one can do the following:
# To make the packages available to emacsclient, one can do the following:
services.emacs.package = with pkgs; (
services.emacs.package = with pkgs; (
   (emacsPackagesFor emacsPgtkNativeComp).emacsWithPackages (
   (emacsPackagesFor emacs-pgtk).emacsWithPackages (
     epkgs: [ epkgs.vterm ]
     epkgs: [ epkgs.vterm ]
   )
   )
Line 105: Line 133:




</syntaxhighlight>
</syntaxhighlight>Note that if the expression <code>(emacsPackagesFor emacs-pgtk)</code> is present, <code>emacs-pgtk</code> need not be listed separately in the list <code>environment.systemPackages</code>. Indeed, if one does that, <code>nixos-rebuild</code> will warn about link collisions when the configuration is rebuilt.
 
====== Alternative way of installation to ensuring consistent package management for emacs and emacsclient ======
If you plan to use the same packages for both emacs and emacsclient, you can define a custom emacs like this:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
  nixpkgs.config.packageOverrides = pkgs: rec {
    myEmacs = pkgs.emacs.pkgs.withPackages (epkgs: with epkgs; [
      org
      nixmode
      ... # list all your desired emacsPackages here
    ]);
  };
</nowiki>}}
 
You may then reference it twice:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
  ## if you want it per-user instead of system-wide use 'users.users.<name>.packages = with pkgs; [' instead
  environment.systemPackages = with pkgs; [
    myEmacs
  ];
 
  ## enabling emacsclient and making all the packages available
  services.emacs = {
    enable = true;
    package = pkgs.myEmacs;
  };
</nowiki>}}
 
Using this approach, there is no need to keep two lists of emacsPackages in sync.


==== Tree-sitter ====
==== Tree-sitter ====
Line 207: Line 264:
   environment.systemPackages = with pkgs;
   environment.systemPackages = with pkgs;
     [ ...
     [ ...
       ((emacsPackagesFor emacsPgtkNativeComp).emacsWithPackages (epkgs: [  
       ((emacsPackagesFor emacs-pgtk).emacsWithPackages (epkgs: [  
           epkgs.vterm  
           epkgs.vterm  
           (callPackage ./lambda-line.nix {
           (callPackage ./lambda-line.nix {