From 634be4904992a35e65f8a5f49cd95fc7de7dbc2e Mon Sep 17 00:00:00 2001 From: Marien Zwart Date: Tue, 26 Mar 2024 22:28:43 +1100 Subject: [PATCH] Write a Doom profile --- cli.el => cli1.el | 0 cli2.el | 20 ++++++++++++++++++++ package.nix | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 4 deletions(-) rename cli.el => cli1.el (100%) create mode 100644 cli2.el diff --git a/cli.el b/cli1.el similarity index 100% rename from cli.el rename to cli1.el diff --git a/cli2.el b/cli2.el new file mode 100644 index 0000000..40baa86 --- /dev/null +++ b/cli2.el @@ -0,0 +1,20 @@ +;;; cli.el -*- lexical-binding: t; -*- + +;; We skip Doom's normal install and initialization. +(require 'straight) + +(defadvice! nix-doom-skip-core-packages (orig-fn &rest args) + "HACK: don't install straight and core packages. + +`doom-initialize-core-packages' would no-op out if +`straight-recipe-repositories' is set, but we do not want to set +it. Just skip it entirely." + :override #'doom-initialize-core-packages + t) + +(defcli! build-profile-for-nix-build () + "Write a Doom profile." + ;; HACK: this initializes enough of straight (particularly + ;; straight--build-cache) to make Doom work (I hope...). + (straight-prune-build-cache) + (doom-profile-generate)) diff --git a/package.nix b/package.nix index 82fbf57..26d989b 100644 --- a/package.nix +++ b/package.nix @@ -12,6 +12,7 @@ full ? false, callPackages, + git, emacsPackagesFor, lib, linkFarm, @@ -31,8 +32,8 @@ let # # Uses Doom's CLI framework, which does not require anything else is installed # (not even straight). - initialDoomDir = linkFarm "minimal-doom-dir" ( - [{ name = "cli.el"; path = ./cli.el; }] + stage1DoomDir = linkFarm "doom-dir-stage1" ( + [{ name = "cli.el"; path = ./cli1.el; }] ++ optional (doomInitFile != null) { name = "init.el"; path = doomInitFile; } ++ optional (doomPrivateModule != null) { name = "packages.el"; path = doomPrivateModule; } ); @@ -47,7 +48,7 @@ let { env = { EMACS = lib.getExe emacs; - DOOMDIR = initialDoomDir; + DOOMDIR = stage1DoomDir; }; } '' mkdir $out @@ -73,6 +74,7 @@ let undo-fu-session = "https://codeberg.org/ideasman42/emacs-undo-fu-session.git"; }; + # Step 2: override Emacs packages to respect Doom's pins. doomEmacsPackages = (emacsPackagesFor emacs).overrideScope ( eself: esuper: let @@ -208,6 +210,41 @@ let lib.mapAttrs makePackage doomPackageSet ); + # Step 3: Build an emacsWithPackages, pulling all packages from step 1 from + # the set from step 2. emacsWithPackages = doomEmacsPackages.emacsWithPackages (epkgs: (map (p: epkgs.${p}) (builtins.attrNames doomPackageSet))); + + # Step 4: build a Doom profile. + stage2DoomDir = linkFarm "doom-dir-stage2" ( + [{ name = "cli.el"; path = ./cli2.el; } + { name = "packages.el"; path = "${doomIntermediates}/packages.el"; }] + ++ optional (doomInitFile != null) { name = "init.el"; path = doomInitFile; } + ); + + # XXX runCommandLocal? (See doomIntermediates.) + doomProfile = runCommand "doom-profile" + { + env = { + EMACS = lib.getExe emacsWithPackages; + DOOMDIR = stage2DoomDir; + # Enable this to troubleshoot failures at this step. + #DEBUG = "1"; + }; + # Required to avoid Doom erroring out at startup. + nativeBuildInputs = [ git ]; + } + # Explicitly create `straight` to get an empty build cache written there + # instead of an error printed on Emacs shutdown. + '' + mkdir -p $out/straight + export DOOMLOCALDIR=$out + ${runtimeShell} ${doomSource}/bin/doom build-profile-for-nix-build + rm -rf $out/state/logs + ''; + + # TODO: this generates a blank 90-loaddefs-packages.auto.el, because + # `doom-profile--generate-package-autoloads` uses straight--build-cache to + # enumerate packages. Do we care, or are package.el autoloads sufficient? + in -emacsWithPackages +doomProfile