1;; -*- lexical-binding: t; -*-
2(defun nix--profile-paths ()
3 "Return a list of all paths in NIX_PROFILES.
4The list is ordered from more-specific (the user profile) to the
5least specific (the system profile)"
6 (reverse (split-string (or (getenv "NIX_PROFILES") ""))))
7
8;;; Extend `load-path' to search for elisp files in subdirectories of all folders in `NIX_PROFILES'.
9;;; Non-Nix distros have similar logic in /usr/share/emacs/site-lisp/subdirs.el.
10;;; See https://www.gnu.org/software/emacs/manual/html_node/elisp/Library-Search.html
11(dolist (profile (reverse (nix--profile-paths)))
12 ;; `directory-file-name' is important to add sub dirs to the right place of `load-path'
13 ;; see the source code of `normal-top-level-add-to-load-path'
14 (let ((default-directory (directory-file-name
15 (expand-file-name "share/emacs/site-lisp/" profile))))
16 (when (file-exists-p default-directory)
17 (setq load-path (cons default-directory load-path))
18 (normal-top-level-add-subdirs-to-load-path))))
19
20;;; Remove wrapper site-lisp from EMACSLOADPATH so it's not propagated
21;;; to any other Emacsen that might be started as subprocesses.
22(let ((wrapper-site-lisp (getenv "emacsWithPackages_siteLisp"))
23 (env-load-path (getenv "EMACSLOADPATH")))
24 (when wrapper-site-lisp
25 (setenv "emacsWithPackages_siteLisp" nil))
26 (when (and wrapper-site-lisp env-load-path)
27 (let* ((env-list (split-string env-load-path ":"))
28 (new-env-list (delete wrapper-site-lisp env-list)))
29 (setenv "EMACSLOADPATH" (when new-env-list
30 (mapconcat 'identity new-env-list ":"))))))
31
32(let ((wrapper-site-lisp (getenv "emacsWithPackages_siteLispNative"))
33 (env-load-path (getenv "EMACSNATIVELOADPATH")))
34 (when wrapper-site-lisp
35 (setenv "emacsWithPackages_siteLispNative" nil))
36 (when (and wrapper-site-lisp env-load-path)
37 (let* ((env-list (split-string env-load-path ":"))
38 (new-env-list (delete wrapper-site-lisp env-list)))
39 (setenv "EMACSNATIVELOADPATH" (when new-env-list
40 (mapconcat 'identity new-env-list ":"))))))
41
42(let ((wrapper-invocation-directory (getenv "emacsWithPackages_invocationDirectory")))
43 (when wrapper-invocation-directory
44 (setq invocation-directory (file-name-as-directory wrapper-invocation-directory))
45 (setenv "emacsWithPackages_invocationDirectory" nil)))
46
47(let ((wrapper-invocation-name (getenv "emacsWithPackages_invocationName")))
48 (when wrapper-invocation-name
49 (setq invocation-name wrapper-invocation-name)
50 (setenv "emacsWithPackages_invocationName" nil)))
51
52;;; Set up native-comp load path.
53(when (featurep 'native-compile)
54 ;; Append native-comp subdirectories from `NIX_PROFILES'.
55 ;; Emacs writes asynchronous native-compilation files to the first writable directory[1].
56 ;; At this time, (car native-comp-eln-load-path) is a writable one in `user-emacs-directory'[2].
57 ;; So we keep that one unchanged.
58 ;; [1]: info "(elisp) Native-Compilation Variables"
59 ;; [2]: https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/startup.el?id=3685387e609753293c4518be75e77c659c3b2d8d#n601
60 (setq native-comp-eln-load-path
61 (append (list (car native-comp-eln-load-path))
62 (mapcar (lambda (profile-dir)
63 (concat profile-dir "/share/emacs/native-lisp/"))
64 (nix--profile-paths))
65 (cdr native-comp-eln-load-path))))
66
67;;; Make `woman' find the man pages
68(defvar woman-manpath)
69(eval-after-load 'woman
70 '(setq woman-manpath
71 (append (mapcar (lambda (x) (concat x "/share/man/"))
72 (nix--profile-paths))
73 woman-manpath)))
74
75;;; Make tramp work for remote NixOS machines
76(defvar tramp-remote-path)
77(eval-after-load 'tramp
78 ;; TODO: We should also add the other `NIX_PROFILES' to this path.
79 ;; However, these are user-specific, so we would need to discover
80 ;; them dynamically after connecting via `tramp'
81 '(progn
82 (add-to-list 'tramp-remote-path "/run/current-system/sw/bin")
83 (add-to-list 'tramp-remote-path "/run/wrappers/bin")))
84
85;;; C source directory
86;;;
87;;; Computes the location of the C source directory from the path of
88;;; the current file:
89;;; from: /nix/store/<hash>-emacs-<version>/share/emacs/site-lisp/site-start.el
90;;; to: /nix/store/<hash>-emacs-<version>/share/emacs/<version>/src/
91(defvar find-function-C-source-directory)
92(let ((emacs
93 (file-name-directory ; .../emacs/
94 (directory-file-name ; .../emacs/site-lisp
95 (file-name-directory load-file-name)))) ; .../emacs/site-lisp/
96 (version
97 (file-name-as-directory
98 emacs-version))
99 (src (file-name-as-directory "src")))
100 (setq find-function-C-source-directory (concat emacs version src)))