Skip to content

Commit

Permalink
Support literate config / doom +org tangle
Browse files Browse the repository at this point in the history
Adds `tangleArgs`. When set, runs `doom +org tangle` in a symlinked copy
of DOOMDIR, using that instead of the original for everything else.

I expect this to be usable as an alternative to `doom :config literate`,
which (because it does its work from `doom-before-sync-hook`) has no
effect.

We currently (somewhat hackishly) run `doom` with just an `init.el`
enabling `:lang org` (the bare minimum for Doom to provide the `+org
tangle` command), not a full profile. This means the user can tangle
their init.el, but also that Doom uses org-mode from upstream emacs, not
its pinned version. If this turns out to be a problem, we should be able
to build an actual Doom profile and `doom` wrapper first.
  • Loading branch information
marienz committed Dec 1, 2024
1 parent be94e8d commit 2312e72
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ support use without flakes.
efficient but considered experimental in Nix (subject to changes which might
break fetches).

- `tangleArgs`: When set, Unstraightened runs `doom +org tangle` in `doomDir`.
See `doom +org tangle --help` for the arguments you can use here, and see the
[Org-mode manual](https://orgmode.org/manual/Extracting-Source-Code.html) for
more information.

> [!NOTE]
> The `:config literate` module has no effect when using Unstraightened.
> Use this instead.
> [!NOTE]
> Currently, this feature uses the version of Org-mode that comes with Emacs
> when tangling, not the version installed by Doom. This limitation could be
> lifted: file an issue if this limitation is a problem for you.
There are a few other settings but they are not typically useful. See the
source.

Expand Down
38 changes: 36 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
fd
ripgrep
],
# Args to pass to `doom +org tangle`.
tangleArgs ? null,

callPackage,
callPackages,
Expand All @@ -47,13 +49,45 @@
makeBinaryWrapper,
stdenv,
stdenvNoCC,
toInit,
writeTextDir,
}:
let
inherit (lib) optionalAttrs optionalString;
inherit (import ./fetch-overrides.nix) extraPins extraUrls;

nonEmptyProfileName = if profileName != "" then profileName else "nix";

tangleDoomDir = writeTextDir "init.el" (toInit {
lang.org = true;
});

# Preprocess DOOMDIR with `doom +org tangle` if requested.
doomDir' =
if tangleArgs != null then
runCommandLocal "tangled-doomdir"
{
inherit
tangleArgs
doomDir
doomSource
runtimeShell
;
EMACS = lib.getExe emacs;
DOOMDIR = tangleDoomDir;
# Enable this to troubleshoot failures at this step.
#DEBUG = "1";
}
''
mkdir $out doomlocaldir
export DOOMLOCALDIR=$PWD/doomlocaldir
cd $out
ln -s $doomDir/* ./
$runtimeShell $doomSource/bin/doom +org tangle $tangleArgs
''
else
doomDir;

# Step 1: determine which Emacs packages to pull in.
#
# Inputs: Doom, original DOOMDIR (only init.el and packages.el are used).
Expand All @@ -69,7 +103,7 @@ let
name = "doom-intermediates";
inherit doomSource emacs;
extraArgs = {
DOOMDIR = "${doomDir}";
DOOMDIR = "${doomDir'}";
};
script = ./build-helpers/dump;
scriptArgs = "-o $out";
Expand Down Expand Up @@ -358,11 +392,11 @@ let
buildCommandPath = ./build-helpers/build-doom-profile.sh;

inherit
doomDir
doomIntermediates
doomSource
runtimeShell
;
doomDir = doomDir';
profileName = nonEmptyProfileName;
noProfileHack = profileName == "";
buildProfileLoader = ./build-helpers/build-profile-loader;
Expand Down
5 changes: 5 additions & 0 deletions doomdir/config.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#+title: Config

#+begin_src emacs-lisp :tangle init.el
(doom! :lang org)
#+end_src
8 changes: 7 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
# directly instead of having pkgs.callPackage do it.
inherit (emacs-overlay.overlays.package { } pkgs) emacsPackagesFor;
mergedArgs = args // {
inherit emacsPackagesFor;
inherit emacsPackagesFor toInit;
doomSource = doomemacs;
};
in
Expand Down Expand Up @@ -126,6 +126,12 @@
doomLocalDir = "~/.local/share/nix-doom-unstraightened";
profileName = "";
}).doomEmacs;
doom-emacs-tangle =
(doomFromPackages pkgs {
doomDir = ./doomdir;
doomLocalDir = "~/.local/share/nix-doom-unstraightened";
tangleArgs = ".";
}).doomEmacs;
cachix-packages =
let
doomDirs = pkgs.callPackages ./build-helpers/doomdirs.nix {
Expand Down
11 changes: 11 additions & 0 deletions home-manager.nix
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ in
description = "Extra packages to add to Doom's $PATH.";
};

tangleArgs = mkOption {
default = null;
example = ".";
type = types.orNull types.str;
defaultText = literalExpression null;
description = ''
When set, run `doom +org tangle $tangleArgs` in `doomDir`.
'';
};

# Home Manager-specific options.
provideEmacs = mkOption {
type = types.bool;
Expand Down Expand Up @@ -172,6 +182,7 @@ in
experimentalFetchTree
extraPackages
extraBinPackages
tangleArgs
;
};
in
Expand Down

0 comments on commit 2312e72

Please sign in to comment.