Files
sydnix/users/crumb/programs/emacs.nix
Madeleine Sydney e102d66797 refactor,feat(emacs): Refactor emacs.nix
- Added Tree-sitter grammars.
- Attempting to do so led to discovering how janky the previous setup was.
- Previous package "bootstrapping" process looked like

    emacsPackage (The selected Emacs build from Nixpkgs)
         ↓
    emacs-wrapper (My wrapper that set envvars and included external deps)
         ↓
    programs.emacs.finalPackage (The package created by the HM module, which
                                 included Nix-managed Emacs packages)

  Now, we have

    emacsBasePackage (The selected build from Nixpkgs)
         ↓
    my-emacs (My wrapper that manages envvars, external packages, Nix-managed
              Emacs packages, and everything else)
   
  `programs.emacs.finalPackage` still exists, but it doesn't do anything that
  `my-emacs` doesn't.
2025-03-06 08:48:37 -07:00

128 lines
4.0 KiB
Nix
Executable File

{ config, lib, pkgs, inputs, ... }@args:
let
emacsBasePackage = pkgs.emacs30-pgtk;
emacsConfigDir = "/persist/dots/users/crumb/programs/emacs";
emacs-overlay =
import (builtins.fetchTarball {
url =
"https://github.com/nix-community/emacs-overlay/archive/master.tar.gz";
sha256 = "sha256:02aln37ch9isz8h7dlm9v6jkl60g923z0dij0rjsxq7xi61fas2j";
});
# Create a new instance of nixpkgs with emacs-overlay applied. This is a
# little unorthodox, but we do it
# 1. for the sake of organisation — For pure aesthetics and a clean
# codebase, I want everything Emacs to stay in this file;
# 2. and, there's simply no need to leak emacs-overlay's packages into the
# global nixpkgs instance when nothing else is using it!
pkgs' = import inputs.nixpkgs {
system = args.system;
overlays = [ emacs-overlay ];
};
emacsDataDir = "${config.xdg.dataHome}/emacs";
emacsCacheDir = "${emacsDataDir}/cache";
straightBaseDir = "${emacsDataDir}/straight";
fontPackages = [
pkgs.julia-mono
pkgs.nerd-fonts.victor-mono
pkgs.ibm-plex
];
my-aspell = pkgs.aspellWithDicts
(dicts: with dicts; [
en en-computers en-science
]);
my-emacs =
let ewp = (pkgs.emacsPackagesFor emacsBasePackage).emacsWithPackages
(epkgs: with epkgs; [
jinx
pdf-tools
treesit-grammars.with-all-grammars
]);
in pkgs.symlinkJoin {
name = "sydmacs";
paths = [ ewp ];
nativeBuildInputs = [ pkgs.makeWrapper ];
buildInputs = [
pkgs.git # Dependency of Straight.el.
my-aspell
pkgs.direnv
];
postBuild = ''
# We specify `-type f` because `emacs` is sometimes a symlink to
# `emacs-«version»`. If we were to wrap the symlink, Emacs would be
# wrapped *twice*.
find "$out" -type f -name emacs -or -name "emacs-*" \
| while IFS= read -r emacs; do
wrapProgram "$emacs" \
--add-flags "--init-directory \"${emacsConfigDir}\"" \
--set EMACS_STRAIGHT_BASE_DIR "${straightBaseDir}" \
--set EMACS_CACHE_DIR "${emacsCacheDir}" \
--set EMACS_DATA_DIR "${emacsDataDir}" \
--prefix PATH : "${pkgs.git}/bin" \
--prefix PATH : "${my-aspell}/bin" \
--prefix PATH : "${pkgs.direnv}/bin" \
--prefix PATH : "${pkgs.texliveFull}/bin" \
--set ASPELL_CONF "dict-dir ${my-aspell}/lib/aspell"
done
'';
meta = emacsBasePackage.meta;
version = emacsBasePackage.version;
};
emacsclient-or-emacs = pkgs.writeShellScriptBin "emacsclient-or-emacs" ''
emacsclient --alternate-editor=${config.programs.emacs.finalPackage}/bin/emacs "$@"
'';
in {
programs.emacs = {
enable = true;
package = my-emacs;
};
sydnix.impermanence.cache.directories =
# Impermanence expects the path to be relative to ~.
map (lib.removePrefix config.home.homeDirectory) [
straightBaseDir
emacsCacheDir
emacsDataDir
];
# Set emacsclient as the default editor for the time being.
home.sessionVariables =
let e = "${emacsclient-or-emacs}/bin/emacsclient-or-emacs";
in {
"EDITOR" = e;
"VISUAL" = e;
};
home.file =
let default =
lib.removePrefix "${config.home.homeDirectory}/"
"${straightBaseDir}/straight/versions/default.el";
in {
${default}.source =
config.lib.file.mkOutOfStoreSymlink
"/persist/dots/users/crumb/programs/emacs/straight-lockfile.el";
};
home.packages = [
emacsclient-or-emacs
] ++ fontPackages;
# There's probably a better place to put this, but the current setup demands
# Fontconfig for Emacs to find my fonts.
fonts.fontconfig.enable = true;
# TODO: Make sure this is using the right package for Emacs...
services.emacs = {
enable = true;
# Generate a desktop entry for emacsclient.
client.enable = true;
};
}