fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . services . emacs ; 8emacsCfg = config . programs . emacs ; 9emacsBinPath = " ${ cfg . package } /bin" ; 10emacsVersion = getVersion cfg . package ; 11 12clientWMClass = 13if versionAtLeast emacsVersion "28" then "Emacsd" else "Emacs" ; 14 15# Adapted from upstream emacs.desktop 16clientDesktopItem = pkgs . writeTextDir "share/applications/emacsclient.desktop" 17( generators . toINI { } { 18"Desktop Entry" = { 19Type = "Application" ; 20Exec = " ${ emacsBinPath } /emacsclient ${ 21concatStringsSep " " cfg . client . arguments 22} %F" ; 23Terminal = false ; 24Name = "Emacs Client" ; 25Icon = "emacs" ; 26Comment = "Edit text" ; 27GenericName = "Text Editor" ; 28MimeType = 29"text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;" ; 30Categories = "Development;TextEditor;" ; 31Keywords = "Text;Editor;" ; 32StartupWMClass = clientWMClass ; 33}; 34}); 35 36# Match the default socket path for the Emacs version so emacsclient continues 37# to work without wrapping it. 38socketDir = "%t/emacs" ; 39socketPath = " ${ socketDir } /server" ; 40 41in { 42meta . maintainers = [ maintainers . tadfisher ]; 43 44options . services . emacs = { 45enable = mkEnableOption "the Emacs daemon" ; 46 47package = mkOption { 48type = types . package ; 49default = if emacsCfg . enable then emacsCfg . finalPackage else pkgs . emacs ; 50defaultText = literalExpression '' 51if config.programs.emacs.enable then config.programs.emacs.finalPackage 52else pkgs.emacs 53'' ; 54description = "The Emacs package to use." ; 55}; 56 57extraOptions = mkOption { 58type = with types ; listOf str ; 59default = [ ]; 60example = [ "-f" "exwm-enable" ]; 61description = '' 62Extra command-line arguments to pass to {command}`emacs`. 63'' ; 64}; 65 66client = { 67enable = mkEnableOption "generation of Emacs client desktop file" ; 68arguments = mkOption { 69type = with types ; listOf str ; 70default = [ "-c" ]; 71description = '' 72Command-line arguments to pass to {command}`emacsclient`. 73'' ; 74}; 75}; 76 77# Attrset for forward-compatibility; there may be a need to customize the 78# socket path, though allowing for such is not easy to do as systemd socket 79# units don't perform variable expansion for 'ListenStream'. 80socketActivation = { 81enable = mkEnableOption "systemd socket activation for the Emacs service" ; 82}; 83 84startWithUserSession = mkOption { 85type = with types ; either bool ( enum [ "graphical" ]); 86default = ! cfg . socketActivation . enable ; 87defaultText = 88literalExpression "!config.services.emacs.socketActivation.enable" ; 89example = "graphical" ; 90description = '' 91Whether to launch Emacs service with the systemd user session. If it is 92`true`, Emacs service is started by 93`default.target`. If it is 94`"graphical"`, Emacs service is started by 95`graphical-session.target`. 96'' ; 97}; 98 99defaultEditor = mkOption rec { 100type = types . bool ; 101default = false ; 102example = ! default ; 103description = '' 104Whether to configure {command}`emacsclient` as the default 105editor using the {env}`EDITOR` environment variable. 106'' ; 107}; 108}; 109 110config = mkIf cfg . enable ( mkMerge [ 111{ 112assertions = [ 113( lib . hm . assertions . assertPlatform "services.emacs" pkgs 114lib . platforms . linux ) 115]; 116 117systemd . user . services . emacs = { 118Unit = { 119Description = "Emacs text editor" ; 120Documentation = 121"info:emacs man:emacs(1) https://gnu.org/software/emacs/" ; 122 123After = optional ( cfg . startWithUserSession == "graphical" ) 124"graphical-session.target" ; 125PartOf = optional ( cfg . startWithUserSession == "graphical" ) 126"graphical-session.target" ; 127 128# Avoid killing the Emacs session, which may be full of 129# unsaved buffers. 130X-RestartIfChanged = false ; 131} // optionalAttrs ( cfg . socketActivation . enable ) { 132# Emacs deletes its socket when shutting down, which systemd doesn't 133# handle, resulting in a server without a socket. 134# See https://github.com/nix-community/home-manager/issues/2018 135RefuseManualStart = true ; 136}; 137 138Service = { 139Type = "notify" ; 140 141# We wrap ExecStart in a login shell so Emacs starts with the user's 142# environment, most importantly $PATH and $NIX_PROFILES. It may be 143# worth investigating a more targeted approach for user services to 144# import the user environment. 145ExecStart = '' 146${ pkgs . runtimeShell } -l -c " ${ emacsBinPath } /emacs --fg-daemon ${ 147# In case the user sets 'server-directory' or 'server-name' in 148# their Emacs config, we want to specify the socket path explicitly 149# so launching 'emacs.service' manually doesn't break emacsclient 150# when using socket activation. 151optionalString cfg . socketActivation . enable 152"= ${ escapeShellArg socketPath } " 153} ${ escapeShellArgs cfg . extraOptions } "'' ; 154 155# Emacs will exit with status 15 after having received SIGTERM, which 156# is the default "KillSignal" value systemd uses to stop services. 157SuccessExitStatus = 15 ; 158 159Restart = "on-failure" ; 160} // optionalAttrs ( cfg . socketActivation . enable ) { 161# Use read-only directory permissions to prevent emacs from 162# deleting systemd's socket file before exiting. 163ExecStartPost = 164" ${ pkgs . coreutils } /bin/chmod --changes -w ${ socketDir } " ; 165ExecStopPost = 166" ${ pkgs . coreutils } /bin/chmod --changes +w ${ socketDir } " ; 167}; 168} // optionalAttrs ( cfg . startWithUserSession != false ) { 169Install = { 170WantedBy = [ 171( if cfg . startWithUserSession == true then 172"default.target" 173else 174"graphical-session.target" ) 175]; 176}; 177}; 178 179home = { 180packages = optional cfg . client . enable ( hiPrio clientDesktopItem ); 181 182sessionVariables = mkIf cfg . defaultEditor { 183EDITOR = getBin ( pkgs . writeShellScript "editor" '' 184exec ${ 185getBin cfg . package 186} /bin/emacsclient "'' $ {@:---create-frame}"'' ); 187}; 188}; 189} 190 191( mkIf cfg . socketActivation . enable { 192systemd . user . sockets . emacs = { 193Unit = { 194Description = "Emacs text editor" ; 195Documentation = 196"info:emacs man:emacs(1) https://gnu.org/software/emacs/" ; 197}; 198 199Socket = { 200ListenStream = socketPath ; 201FileDescriptorName = "server" ; 202SocketMode = "0600" ; 203DirectoryMode = "0700" ; 204}; 205 206Install = { WantedBy = [ "sockets.target" ]; }; 207}; 208}) 209]); 210}