* emacsclient on NixOS: run Emacs as a server with services.emacs :properties: :export_file_name: generated/contents/blog/2026/7d3afe :export_date: 2026-11-27 19:03 :export_updated: 2026-11-27 19:03 :export_lang: en :export_original: /blog/2026/26583e :export_tags: emacs nix tutorial :end: As an Emacs user, you sometimes want to edit a file from the shell in the Emacs instance that is already running, and emacsclient lets you do that. This post sets up emacsclient on NixOS with services.emacs. You keep Emacs running as a server, and emacsclient connects to it as a client and opens the file at once. ref: [[https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/editors/emacs.md][nixpkgs/nixos/modules/services/editors/emacs.md at master / NixOS/nixpkgs / GitHub]] ** How emacsclient works Emacs supports a server/client model. Started the usual way, Emacs runs as a standalone process, but in server mode it stays resident in the background. The emacsclient command then connects to the running Emacs server and opens files in a new frame or buffer. ** Test environment So that you can try this without NixOS, I set up a Nix flake that boots a NixOS VM on QEMU. If you already use NixOS, add the settings to your own =configuration.nix= instead. The VM has a minimal Xfce desktop, which is enough to try the server and client features of Emacs. flake.nix #+begin_src nix { inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; outputs = { self, nixpkgs }: { nixosConfigurations.minimal-vm = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = [ ./configuration.nix ]; }; packages.x86_64-linux.default = self.nixosConfigurations.minimal-vm.config.system.build.vm; }; } #+end_src configuration.nix #+begin_src nix { modulesPath, pkgs, ... }: { imports = [ (modulesPath + "/virtualisation/qemu-vm.nix") ]; system.stateVersion = "24.11"; users.users.demo = { isNormalUser = true; extraGroups = [ "wheel" ]; initialPassword = "demo"; }; services.xserver.enable = true; services.xserver.desktopManager.xfce.enable = true; services.xserver.desktopManager.xfce.enableScreensaver = false; environment.systemPackages = with pkgs; [ git ]; } #+end_src With these two files in place, this command boots the VM. #+begin_src shell QEMU_OPTS="-m 8192 -smp 4" nix run #+end_src #+DOWNLOADED: screenshot @ 2026-01-24 14:40:56 [[file:resources/public/img/42afe4e0-4c5c-0b43-1d32-56bbbcc695ab.png]] From here on, you make your changes in configuration.nix. ** Configuration Enabling =services.emacs= starts the Emacs server when the user logs in. configuration.nix #+begin_src nix { services.emacs = { enable = true; defaultEditor = true; }; } #+end_src NixOS turns this setting into the [[https://github.com/NixOS/nixpkgs/blob/2786a2a72088ecf32aea5b3ec52099e6a7cff78f/nixos/modules/services/editors/emacs.nix#L68-L89][systemd user service]] =emacs=, which runs =emacs --fg-daemon= when you log in to the desktop. Setting =defaultEditor= as well points =EDITOR= at emacsclient, so programs that use =EDITOR= open files in emacsclient without any setup of their own. The option only sets an environment variable, so if you let =e= run =EDITOR=, =e something.txt= opens the file. #+begin_src nix { environment.shellAliases = { e = "$EDITOR"; }; } #+end_src *** services.emacs options =services.emacs= has a few more useful options. **** package This option selects the Emacs package to use. The default is =pkgs.emacs=, and you can also set emacs-nox, emacs-pgtk, or an Emacs package you customized yourself. #+begin_src nix { services.emacs = { enable = true; package = pkgs.emacs-pgtk; }; } #+end_src ** Usage *** After login The Emacs server starts when you log in to the desktop, so emacsclient can open a new frame right away. Earlier you made =e= an alias for =$EDITOR=, so running =e= alone opens a new frame. Add =&= so the command doesn't block the shell. #+begin_src shell e & #+end_src You can also run =emacsclient -c= yourself. #+begin_src shell emacsclient -c & #+end_src #+DOWNLOADED: screenshot @ 2026-01-25 10:51:15 [[file:resources/public/img/471b70d6-8461-e881-8367-ce86c4246ad3.png]] *** Opening a file In the same way, =e something.txt= opens a file. To give control back to the shell, press =C-x #= and tell Emacs you have finished editing. #+begin_src shell e something.txt #+end_src *** Main emacsclient options **** -c (=--create-frame=) Creates a new frame and opens the file in it. #+begin_src shell e -c file.txt #+end_src **** -t (=--tty=) Opens Emacs inside the terminal. #+begin_src shell e -t file.txt #+end_src **** -n (=--no-wait=) Gives control back to the shell as soon as the file opens. By default emacsclient waits until you finish editing, and with this option it opens the file in the background. #+begin_src shell e -n file.txt #+end_src *** Checking the server status Use =systemctl= to check that the Emacs server is running. #+begin_src shell systemctl --user status emacs #+end_src To restart the server by hand: #+begin_src shell systemctl --user restart emacs #+end_src *** Troubleshooting **** The server doesn't start Check the log first. #+begin_src shell journalctl --user -u emacs #+end_src In most cases, an error in your Emacs init file (init.el) causes this. Start Emacs the usual way, fix the configuration, and then restart the server. **** Several servers are running If several Emacs servers end up running for some reason, these commands stop all of them. #+begin_src shell systemctl --user stop emacs pkill -u $USER emacs #+end_src Then start the server again. #+begin_src shell systemctl --user start emacs #+end_src **** Sharing the clipboard To use text you copied in the QEMU VM on the host as well, use the GTK backend of QEMU. Add =-display gtk= to QEMU_OPTS when you boot the VM: #+begin_src shell QEMU_OPTS="-m 8192 -smp 4 -display gtk" nix run #+end_src Text you copy inside the VM then shows up in the clipboard of the host. **** tips It's fun to look at what this configuration sets up. Check the systemd unit file: #+begin_src shell systemctl --user cat emacs #+end_src Check the value of the EDITOR environment variable: #+begin_src shell $ echo $EDITOR emacseditor $ which $EDITOR /run/current-system/sw/bin/emacseditor $ cat $(which $EDITOR) #!/nix/store/lw117lsr8d585xs63kx5k233impyrq7q-bash-5.3p3/bin/bash if [ -z "$1" ]; then exec /nix/store/3gh804i02baai4yvn4vz99hs4ncvxjdd-emacs-30.2/bin/emacsclient --create-frame --alternate-editor /nix/store/3gh804i02baai4yvn4vz99hs4ncvxjdd-emacs-30.2/bin/emacs else exec /nix/store/3gh804i02baai4yvn4vz99hs4ncvxjdd-emacs-30.2/bin/emacsclient --alternate-editor /nix/store/3gh804i02baai4yvn4vz99hs4ncvxjdd-emacs-30.2/bin/emacs "$@" fi #+end_src You can see that =EDITOR= runs =emacseditor=, a small shell script that wraps =emacsclient=. ** Summary With =services.emacs= on NixOS, setting up emacsclient takes little effort. If you have wanted to use emacsclient but couldn't get the setup to work, give this a try.