diff --git a/modules/services/emacs.nix b/modules/services/emacs.nix
index 1631275b..38267151 100644
--- a/modules/services/emacs.nix
+++ b/modules/services/emacs.nix
@@ -80,6 +80,16 @@ in {
socketActivation = {
enable = mkEnableOption "systemd socket activation for the Emacs service";
};
+
+ defaultEditor = mkOption rec {
+ type = types.bool;
+ default = false;
+ example = !default;
+ description = ''
+ Whether to configure emacsclient as the default
+ editor using the EDITOR environment variable.
+ '';
+ };
};
config = mkIf cfg.enable (mkMerge [
@@ -139,7 +149,16 @@ in {
Install = { WantedBy = [ "default.target" ]; };
};
- home.packages = optional cfg.client.enable (hiPrio clientDesktopItem);
+ home = {
+ packages = optional cfg.client.enable (hiPrio clientDesktopItem);
+
+ sessionVariables = mkIf cfg.defaultEditor {
+ EDITOR = getBin (pkgs.writeShellScript "editor" ''
+ exec ${
+ getBin cfg.package
+ }/bin/emacsclient "''${@:---create-frame}"'');
+ };
+ };
}
(mkIf cfg.socketActivation.enable {
diff --git a/tests/modules/services/emacs/default.nix b/tests/modules/services/emacs/default.nix
index 86f68c4c..47493207 100644
--- a/tests/modules/services/emacs/default.nix
+++ b/tests/modules/services/emacs/default.nix
@@ -3,4 +3,5 @@
emacs-service-28 = ./emacs-service-28.nix;
emacs-socket-27 = ./emacs-socket-27.nix;
emacs-socket-28 = ./emacs-socket-28.nix;
+ emacs-default-editor = ./emacs-default-editor.nix;
}
diff --git a/tests/modules/services/emacs/emacs-default-editor.nix b/tests/modules/services/emacs/emacs-default-editor.nix
new file mode 100644
index 00000000..77897a5b
--- /dev/null
+++ b/tests/modules/services/emacs/emacs-default-editor.nix
@@ -0,0 +1,24 @@
+{ config, lib, pkgs, ... }:
+
+{
+ nixpkgs.overlays = [
+ (self: super: {
+ # Use `cat` instead of `echo` to prevent arguments from being
+ # interpreted as an option.
+ emacs = pkgs.writeShellScriptBin "emacsclient"
+ ''${pkgs.coreutils}/bin/cat <<< "$*"'';
+ })
+ ];
+
+ services.emacs = {
+ defaultEditor = true;
+ enable = true;
+ };
+
+ nmt.script = "source ${
+ pkgs.substituteAll {
+ inherit (pkgs) coreutils;
+ src = ./emacs-default-editor.sh;
+ }
+ }";
+}
diff --git a/tests/modules/services/emacs/emacs-default-editor.sh b/tests/modules/services/emacs/emacs-default-editor.sh
new file mode 100644
index 00000000..b1aafaac
--- /dev/null
+++ b/tests/modules/services/emacs/emacs-default-editor.sh
@@ -0,0 +1,18 @@
+set +u
+source $TESTED/home-path/etc/profile.d/hm-session-vars.sh
+set -u
+
+check_arguments () {
+ if [ "$1" != "$2" ]; then
+ @coreutils@/bin/cat <<- EOF
+ Expected arguments:
+ $1
+ but got:
+ $2
+ EOF
+ exit 1
+ fi
+}
+
+check_arguments "--create-frame" "$($EDITOR)"
+check_arguments "foo bar baz" "$($EDITOR foo bar baz)"