···32323333*/
34343535-{ lib, lndir, makeWrapper, stdenv }: self:
3535+{ lib, lndir, makeWrapper, runCommand, stdenv }: self:
36363737with lib; let inherit (self) emacs; in
3838···4949 name = (appendToName "with-packages" emacs).name;
5050 nativeBuildInputs = [ emacs lndir makeWrapper ];
5151 inherit emacs explicitRequires;
5252- phases = [ "installPhase" ];
5353- installPhase = ''
5454- readonly SHARE="share/emacs-with-packages"
55525656- mkdir -p "$out/bin"
5757- mkdir -p "$out/$SHARE/bin"
5858- mkdir -p "$out/$SHARE/site-lisp"
5353+ # Store all paths we want to add to emacs here, so that we only need to add
5454+ # one path to the load lists
5555+ deps = runCommand "emacs-packages-deps"
5656+ { inherit explicitRequires lndir emacs; }
5757+ ''
5858+ mkdir -p $out/bin
5959+ mkdir -p $out/share/emacs/site-lisp
59606060- local requires
6161- for pkg in $explicitRequires; do
6262- findInputs $pkg requires propagated-user-env-packages
6363- done
6464- # requires now holds all requested packages and their transitive dependencies
6161+ local requires
6262+ for pkg in $explicitRequires; do
6363+ findInputs $pkg requires propagated-user-env-packages
6464+ done
6565+ # requires now holds all requested packages and their transitive dependencies
65666666- siteStart="$out/$SHARE/site-lisp/site-start.el"
6767+ linkPath() {
6868+ local pkg=$1
6969+ local origin_path=$2
7070+ local dest_path=$3
67716868- # Begin the new site-start.el by loading the original, which sets some
6969- # NixOS-specific paths. Paths are searched in the reverse of the order
7070- # they are specified in, so user and system profile paths are searched last.
7171- cat >"$siteStart" <<EOF
7272-(load-file "$emacs/share/emacs/site-lisp/site-start.el")
7373-(add-to-list 'load-path "$out/$SHARE/site-lisp")
7474-(add-to-list 'exec-path "$out/$SHARE/bin")
7575-EOF
7272+ # Add the path to the search path list, but only if it exists
7373+ if [[ -d "$pkg/$origin_path" ]]; then
7474+ $lndir/bin/lndir -silent "$pkg/$origin_path" "$out/$dest_path"
7575+ fi
7676+ }
76777777- linkPath() {
7878- local pkg=$1
7979- local origin_path=$2
8080- local dest_path=$3
7878+ linkEmacsPackage() {
7979+ linkPath "$1" "bin" "bin"
8080+ linkPath "$1" "share/emacs/site-lisp" "share/emacs/site-lisp"
8181+ }
81828282- # Add the path to the search path list, but only if it exists
8383- if [[ -d "$pkg/$origin_path" ]]; then
8484- lndir -silent "$pkg/$origin_path" "$out/$dest_path"
8585- fi
8686- }
8383+ for pkg in $requires; do
8484+ linkEmacsPackage $pkg
8585+ done
87868888- # Add a package's paths to site-start.el
8989- linkEmacsPackage() {
9090- linkPath "$1" "bin" "$SHARE/bin"
9191- linkPath "$1" "share/emacs/site-lisp" "$SHARE/site-lisp"
9292- }
8787+ siteStart="$out/share/emacs/site-lisp/site-start.el"
93889494- # First, link all the explicitly-required packages.
9595- for pkg in $explicitRequires; do
9696- linkEmacsPackage $pkg
9797- done
8989+ # A dependency may have brought the original siteStart, delete it and
9090+ # create our own
9191+ # Begin the new site-start.el by loading the original, which sets some
9292+ # NixOS-specific paths. Paths are searched in the reverse of the order
9393+ # they are specified in, so user and system profile paths are searched last.
9494+ rm -f $siteStart
9595+ cat >"$siteStart" <<EOF
9696+(load-file "$emacs/share/emacs/site-lisp/site-start.el")
9797+(add-to-list 'load-path "$out/share/emacs/site-lisp")
9898+(add-to-list 'exec-path "$out/bin")
9999+EOF
981009999- # Next, link all the dependencies.
100100- for pkg in $requires; do
101101- linkEmacsPackage $pkg
102102- done
101101+ # Byte-compiling improves start-up time only slightly, but costs nothing.
102102+ $emacs/bin/emacs --batch -f batch-byte-compile "$siteStart"
103103+ '';
103104104104- # Byte-compiling improves start-up time only slightly, but costs nothing.
105105- emacs --batch -f batch-byte-compile "$siteStart"
105105+ phases = [ "installPhase" ];
106106+ installPhase = ''
107107+ mkdir -p "$out/bin"
106108107109 # Wrap emacs and friends so they find our site-start.el before the original.
108110 for prog in $emacs/bin/*; do # */
109111 local progname=$(basename "$prog")
110112 rm -f "$out/bin/$progname"
111113 makeWrapper "$prog" "$out/bin/$progname" \
112112- --suffix EMACSLOADPATH ":" "$out/$SHARE/site-lisp:"
114114+ --suffix EMACSLOADPATH ":" "$deps/share/emacs/site-lisp:"
113115 done
114116115117 mkdir -p $out/share