services/emacs: add option to set emacsclient as the default editor (#2545)

* services/emacs: add `defaultEditor` option

Sets `emacsclient` as the default editor using the `EDITOR` environment variable.
This commit is contained in:
rcerc 2022-01-10 02:17:13 -05:00 committed by GitHub
parent 05d655146b
commit f3be3cda6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 1 deletions

View file

@ -80,6 +80,16 @@ in {
socketActivation = { socketActivation = {
enable = mkEnableOption "systemd socket activation for the Emacs service"; enable = mkEnableOption "systemd socket activation for the Emacs service";
}; };
defaultEditor = mkOption rec {
type = types.bool;
default = false;
example = !default;
description = ''
Whether to configure <command>emacsclient</command> as the default
editor using the <envar>EDITOR</envar> environment variable.
'';
};
}; };
config = mkIf cfg.enable (mkMerge [ config = mkIf cfg.enable (mkMerge [
@ -139,7 +149,16 @@ in {
Install = { WantedBy = [ "default.target" ]; }; 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 { (mkIf cfg.socketActivation.enable {

View file

@ -3,4 +3,5 @@
emacs-service-28 = ./emacs-service-28.nix; emacs-service-28 = ./emacs-service-28.nix;
emacs-socket-27 = ./emacs-socket-27.nix; emacs-socket-27 = ./emacs-socket-27.nix;
emacs-socket-28 = ./emacs-socket-28.nix; emacs-socket-28 = ./emacs-socket-28.nix;
emacs-default-editor = ./emacs-default-editor.nix;
} }

View file

@ -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;
}
}";
}

View file

@ -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)"