emacs: correct the order of profiles and their sub dirs in load-path

This patch does two things:
1. making user profiles preferred over system profiles
2. putting sub dirs of one profile to the right place
- before this patch, they are appended to the end of load-path
- after this patch, they are inserted right after the profile

Example value of load-path before this patch:

/run/current-system/sw/share/emacs/site-lisp/
/etc/profiles/per-user/user/share/emacs/site-lisp/
/nix/store/hash1-emacs-packages-deps/share/emacs/site-lisp
/nix/store/hash1-emacs-packages-deps/share/emacs/site-lisp/elpa
/nix/store/hash1-emacs-packages-deps/share/emacs/site-lisp/elpa/wgrep-20230203.1214
/nix/store/hash2-emacs-29.1-rc1/share/emacs/29.1/site-lisp
/nix/store/hash2-emacs-29.1-rc1/share/emacs/site-lisp
/nix/store/hash2-emacs-29.1-rc1/share/emacs/29.1/lisp
/nix/store/hash2-emacs-29.1-rc1/share/emacs/29.1/lisp/vc
...
/etc/profiles/per-user/user/share/emacs/site-lisp/elpa
/etc/profiles/per-user/user/share/emacs/site-lisp/elpa/jinx-20230730.1200
/run/current-system/sw/share/emacs/site-lisp/elpa
/run/current-system/sw/share/emacs/site-lisp/elpa/repology-1.2.3

after this patch:

/etc/profiles/per-user/user/share/emacs/site-lisp
/etc/profiles/per-user/user/share/emacs/site-lisp/elpa
/etc/profiles/per-user/user/share/emacs/site-lisp/elpa/jinx-20230730.1200
/run/current-system/sw/share/emacs/site-lisp
/run/current-system/sw/share/emacs/site-lisp/elpa
/run/current-system/sw/share/emacs/site-lisp/elpa/repology-1.2.3
/nix/store/hash1-emacs-packages-deps/share/emacs/site-lisp
/nix/store/hash1-emacs-packages-deps/share/emacs/site-lisp/elpa
/nix/store/hash1-emacs-packages-deps/share/emacs/site-lisp/elpa/wgrep-20230203.1214
/nix/store/hash2-emacs-29.1-rc1/share/emacs/29.1/site-lisp
/nix/store/hash2-emacs-29.1-rc1/share/emacs/site-lisp
/nix/store/hash2-emacs-29.1-rc1/share/emacs/29.1/lisp
/nix/store/hash2-emacs-29.1-rc1/share/emacs/29.1/lisp/vc
...

Lin Jian 1506ab49 e8f6a5ce

Changed files
+5 -2
pkgs
applications
editors
+5 -2
pkgs/applications/editors/emacs/site-start.el
···
;;; Extend `load-path' to search for elisp files in subdirectories of all folders in `NIX_PROFILES'.
;;; Non-Nix distros have similar logic in /usr/share/emacs/site-lisp/subdirs.el.
;;; See https://www.gnu.org/software/emacs/manual/html_node/elisp/Library-Search.html
-
(dolist (profile (nix--profile-paths))
-
(let ((default-directory (expand-file-name "share/emacs/site-lisp/" profile)))
+
(dolist (profile (reverse (nix--profile-paths)))
+
;; `directory-file-name' is important to add sub dirs to the right place of `load-path'
+
;; see the source code of `normal-top-level-add-to-load-path'
+
(let ((default-directory (directory-file-name
+
(expand-file-name "share/emacs/site-lisp/" profile))))
(when (file-exists-p default-directory)
(setq load-path (cons default-directory load-path))
(normal-top-level-add-subdirs-to-load-path))))