about summary refs log tree commit diff
path: root/overlays/patches/emacs/overlay/elisp.nix
diff options
context:
space:
mode:
Diffstat (limited to 'overlays/patches/emacs/overlay/elisp.nix')
-rw-r--r--overlays/patches/emacs/overlay/elisp.nix68
1 files changed, 68 insertions, 0 deletions
diff --git a/overlays/patches/emacs/overlay/elisp.nix b/overlays/patches/emacs/overlay/elisp.nix
new file mode 100644
index 000000000000..1cc24f653f1d
--- /dev/null
+++ b/overlays/patches/emacs/overlay/elisp.nix
@@ -0,0 +1,68 @@
+/*
+Parse an emacs lisp configuration file to derive packages from
+use-package declarations.
+*/
+
+{ pkgs }:
+let
+  parse = pkgs.callPackage ./parse.nix { };
+  inherit (pkgs) lib;
+
+
+
+in
+{ config
+# emulate `use-package-always-ensure` behavior (defaulting to false)
+, alwaysEnsure ? null
+# emulate `#+PROPERTY: header-args:emacs-lisp :tangle yes`
+, alwaysTangle ? false
+, extraEmacsPackages ? epkgs: [ ]
+, package ? pkgs.emacs
+, override ? (epkgs: epkgs)
+}:
+let
+  ensureNotice = ''
+    Emacs-overlay API breakage notice:
+
+    Previously emacsWithPackagesFromUsePackage always added every use-package definition to the closure.
+    Now we will only add packages with `:ensure`, `:ensure t` or `:ensure <package name>`.
+
+    You can get back the old behaviour by passing `alwaysEnsure = true`.
+    For a more in-depth usage example see https://github.com/nix-community/emacs-overlay#extra-library-functionality
+  '';
+  doEnsure = if (alwaysEnsure == null) then builtins.trace ensureNotice false else alwaysEnsure;
+
+  isOrgModeFile =
+    let
+      ext = lib.last (builtins.split "\\." (builtins.toString config));
+      type = builtins.typeOf config;
+    in
+      type == "path" && ext == "org";
+
+  configText =
+    let
+      type = builtins.typeOf config;
+    in
+      if type == "string" then config
+      else if type == "path" then builtins.readFile config
+      else throw "Unsupported type for config: \"${type}\"";
+
+  packages = parse.parsePackagesFromUsePackage {
+    inherit configText isOrgModeFile alwaysTangle;
+    alwaysEnsure = doEnsure;
+  };
+  emacsPackages = pkgs.emacsPackagesFor package;
+  emacsWithPackages = emacsPackages.emacsWithPackages;
+  mkPackageError = name:
+    let
+      errorFun = if (alwaysEnsure != null && alwaysEnsure) then builtins.trace else throw;
+    in
+    errorFun "Emacs package ${name}, declared wanted with use-package, not found." null;
+in
+emacsWithPackages (epkgs:
+  let
+    overridden = override epkgs;
+    usePkgs = map (name: overridden.${name} or (mkPackageError name)) packages;
+    extraPkgs = extraEmacsPackages overridden;
+  in
+  usePkgs ++ extraPkgs)