2020-04-24 23:36:52 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
`emacs.pkgs.withPackages` takes a single argument: a function from a package
|
2020-04-24 23:36:52 +00:00
|
|
|
set to a list of packages (the packages that will be available in
|
|
|
|
Emacs). For example,
|
|
|
|
```
|
2021-01-15 22:18:51 +00:00
|
|
|
emacs.pkgs.withPackages (epkgs: [ epkgs.evil epkgs.magit ])
|
2020-04-24 23:36:52 +00:00
|
|
|
```
|
|
|
|
All the packages in the list should come from the provided package
|
|
|
|
set. It is possible to add any package to the list, but the provided
|
|
|
|
set is guaranteed to have consistent dependencies and be built with
|
|
|
|
the correct version of Emacs.
|
|
|
|
|
|
|
|
# Overriding
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
`emacs.pkgs.withPackages` inherits the package set which contains it, so the
|
2020-04-24 23:36:52 +00:00
|
|
|
correct way to override the provided package set is to override the
|
2021-01-15 22:18:51 +00:00
|
|
|
set which contains `emacs.pkgs.withPackages`. For example, to override
|
|
|
|
`emacs.pkgs.emacs.pkgs.withPackages`,
|
2020-04-24 23:36:52 +00:00
|
|
|
```
|
|
|
|
let customEmacsPackages =
|
2023-08-22 20:05:09 +00:00
|
|
|
emacs.pkgs.overrideScope (self: super: {
|
2020-04-24 23:36:52 +00:00
|
|
|
# use a custom version of emacs
|
|
|
|
emacs = ...;
|
|
|
|
# use the unstable MELPA version of magit
|
|
|
|
magit = self.melpaPackages.magit;
|
|
|
|
});
|
2021-08-05 21:33:18 +00:00
|
|
|
in customEmacsPackages.withPackages (epkgs: [ epkgs.evil epkgs.magit ])
|
2020-04-24 23:36:52 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
{ lib, lndir, makeBinaryWrapper, runCommand, gcc }:
|
2023-07-15 17:15:38 +00:00
|
|
|
self:
|
2020-12-25 13:55:36 +00:00
|
|
|
let
|
|
|
|
inherit (self) emacs;
|
2024-01-02 11:29:13 +00:00
|
|
|
withNativeCompilation = emacs.withNativeCompilation or false;
|
|
|
|
withTreeSitter = emacs.withTreeSitter or false;
|
2020-12-25 13:55:36 +00:00
|
|
|
in
|
2020-04-24 23:36:52 +00:00
|
|
|
packagesFun: # packages explicitly requested by the user
|
|
|
|
let
|
|
|
|
explicitRequires =
|
|
|
|
if lib.isFunction packagesFun
|
2023-07-15 17:15:38 +00:00
|
|
|
then packagesFun self
|
2020-04-24 23:36:52 +00:00
|
|
|
else packagesFun;
|
|
|
|
in
|
|
|
|
runCommand
|
2023-07-15 17:15:38 +00:00
|
|
|
(lib.appendToName "with-packages" emacs).name
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
inherit emacs explicitRequires;
|
2023-10-09 19:29:22 +00:00
|
|
|
nativeBuildInputs = [ emacs lndir makeBinaryWrapper ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
allowSubstitutes = false;
|
|
|
|
|
|
|
|
# Store all paths we want to add to emacs here, so that we only need to add
|
|
|
|
# one path to the load lists
|
|
|
|
deps = runCommand "emacs-packages-deps"
|
2023-04-12 12:48:02 +00:00
|
|
|
({
|
2021-07-16 19:40:57 +00:00
|
|
|
inherit explicitRequires lndir emacs;
|
2023-07-15 17:15:38 +00:00
|
|
|
nativeBuildInputs = lib.optional withNativeCompilation gcc;
|
|
|
|
} // lib.optionalAttrs withNativeCompilation {
|
2023-04-12 12:48:02 +00:00
|
|
|
inherit (emacs) LIBRARY_PATH;
|
|
|
|
})
|
2020-04-24 23:36:52 +00:00
|
|
|
''
|
|
|
|
findInputsOld() {
|
|
|
|
local pkg="$1"; shift
|
|
|
|
local var="$1"; shift
|
|
|
|
local propagatedBuildInputsFiles=("$@")
|
|
|
|
|
|
|
|
# TODO(@Ericson2314): Restore using associative array once Darwin
|
|
|
|
# nix-shell doesn't use impure bash. This should replace the O(n)
|
|
|
|
# case with an O(1) hash map lookup, assuming bash is implemented
|
|
|
|
# well :D.
|
|
|
|
local varSlice="$var[*]"
|
|
|
|
# ''${..-} to hack around old bash empty array problem
|
|
|
|
case "''${!varSlice-}" in
|
|
|
|
*" $pkg "*) return 0 ;;
|
|
|
|
esac
|
|
|
|
unset -v varSlice
|
|
|
|
|
|
|
|
eval "$var"'+=("$pkg")'
|
|
|
|
|
|
|
|
if ! [ -e "$pkg" ]; then
|
|
|
|
echo "build input $pkg does not exist" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local file
|
|
|
|
for file in "''${propagatedBuildInputsFiles[@]}"; do
|
|
|
|
file="$pkg/nix-support/$file"
|
|
|
|
[[ -f "$file" ]] || continue
|
|
|
|
|
|
|
|
local pkgNext
|
|
|
|
for pkgNext in $(< "$file"); do
|
|
|
|
findInputsOld "$pkgNext" "$var" "''${propagatedBuildInputsFiles[@]}"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
mkdir -p $out/bin
|
|
|
|
mkdir -p $out/share/emacs/site-lisp
|
2023-07-15 17:15:38 +00:00
|
|
|
${lib.optionalString withNativeCompilation ''
|
2020-12-25 13:55:36 +00:00
|
|
|
mkdir -p $out/share/emacs/native-lisp
|
|
|
|
''}
|
2023-07-15 17:15:38 +00:00
|
|
|
${lib.optionalString withTreeSitter ''
|
2023-03-15 16:39:30 +00:00
|
|
|
mkdir -p $out/lib
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
local requires
|
|
|
|
for pkg in $explicitRequires; do
|
|
|
|
findInputsOld $pkg requires propagated-user-env-packages
|
|
|
|
done
|
|
|
|
# requires now holds all requested packages and their transitive dependencies
|
|
|
|
|
|
|
|
linkPath() {
|
|
|
|
local pkg=$1
|
|
|
|
local origin_path=$2
|
|
|
|
local dest_path=$3
|
|
|
|
|
|
|
|
# Add the path to the search path list, but only if it exists
|
|
|
|
if [[ -d "$pkg/$origin_path" ]]; then
|
|
|
|
$lndir/bin/lndir -silent "$pkg/$origin_path" "$out/$dest_path"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
linkEmacsPackage() {
|
|
|
|
linkPath "$1" "bin" "bin"
|
|
|
|
linkPath "$1" "share/emacs/site-lisp" "share/emacs/site-lisp"
|
2023-07-15 17:15:38 +00:00
|
|
|
${lib.optionalString withNativeCompilation ''
|
2020-12-25 13:55:36 +00:00
|
|
|
linkPath "$1" "share/emacs/native-lisp" "share/emacs/native-lisp"
|
|
|
|
''}
|
2023-07-15 17:15:38 +00:00
|
|
|
${lib.optionalString withTreeSitter ''
|
2023-03-15 16:39:30 +00:00
|
|
|
linkPath "$1" "lib" "lib"
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Iterate over the array of inputs (avoiding nix's own interpolation)
|
|
|
|
for pkg in "''${requires[@]}"; do
|
|
|
|
linkEmacsPackage $pkg
|
|
|
|
done
|
|
|
|
|
|
|
|
siteStart="$out/share/emacs/site-lisp/site-start.el"
|
|
|
|
siteStartByteCompiled="$siteStart"c
|
|
|
|
subdirs="$out/share/emacs/site-lisp/subdirs.el"
|
|
|
|
subdirsByteCompiled="$subdirs"c
|
|
|
|
|
|
|
|
# A dependency may have brought the original siteStart or subdirs, delete
|
|
|
|
# it and create our own
|
|
|
|
# Begin the new site-start.el by loading the original, which sets some
|
|
|
|
# NixOS-specific paths. Paths are searched in the reverse of the order
|
|
|
|
# they are specified in, so user and system profile paths are searched last.
|
2021-03-09 03:18:52 +00:00
|
|
|
#
|
|
|
|
# NOTE: Avoid displaying messages early at startup by binding
|
|
|
|
# inhibit-message to t. This would prevent the Emacs GUI from showing up
|
|
|
|
# prematurely. The messages would still be logged to the *Messages*
|
|
|
|
# buffer.
|
2020-04-24 23:36:52 +00:00
|
|
|
rm -f $siteStart $siteStartByteCompiled $subdirs $subdirsByteCompiled
|
|
|
|
cat >"$siteStart" <<EOF
|
2021-03-09 03:18:52 +00:00
|
|
|
(let ((inhibit-message t))
|
2023-10-09 19:29:22 +00:00
|
|
|
(load "$emacs/share/emacs/site-lisp/site-start"))
|
|
|
|
;; "$out/share/emacs/site-lisp" is added to load-path in wrapper.sh
|
|
|
|
;; "$out/share/emacs/native-lisp" is added to native-comp-eln-load-path in wrapper.sh
|
2020-04-24 23:36:52 +00:00
|
|
|
(add-to-list 'exec-path "$out/bin")
|
2023-07-15 17:15:38 +00:00
|
|
|
${lib.optionalString withTreeSitter ''
|
2023-03-15 16:39:30 +00:00
|
|
|
(add-to-list 'treesit-extra-load-path "$out/lib/")
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
EOF
|
2021-12-06 16:07:01 +00:00
|
|
|
|
|
|
|
# Generate a subdirs.el that statically adds all subdirectories to load-path.
|
|
|
|
$emacs/bin/emacs \
|
|
|
|
--batch \
|
|
|
|
--load ${./mk-wrapper-subdirs.el} \
|
|
|
|
--eval "(prin1 (macroexpand-1 '(mk-subdirs-expr \"$out/share/emacs/site-lisp\")))" \
|
|
|
|
> "$subdirs"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Byte-compiling improves start-up time only slightly, but costs nothing.
|
2022-07-14 12:49:19 +00:00
|
|
|
$emacs/bin/emacs --batch -f batch-byte-compile "$siteStart" "$subdirs"
|
2020-12-25 13:55:36 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
${lib.optionalString withNativeCompilation ''
|
2020-12-25 13:55:36 +00:00
|
|
|
$emacs/bin/emacs --batch \
|
2021-05-20 23:08:51 +00:00
|
|
|
--eval "(add-to-list 'native-comp-eln-load-path \"$out/share/emacs/native-lisp/\")" \
|
2022-07-14 12:49:19 +00:00
|
|
|
-f batch-native-compile "$siteStart" "$subdirs"
|
2020-12-25 13:55:36 +00:00
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
inherit (emacs) meta;
|
|
|
|
}
|
|
|
|
''
|
|
|
|
mkdir -p "$out/bin"
|
|
|
|
|
|
|
|
# Wrap emacs and friends so they find our site-start.el before the original.
|
|
|
|
for prog in $emacs/bin/*; do # */
|
|
|
|
local progname=$(basename "$prog")
|
|
|
|
rm -f "$out/bin/$progname"
|
2020-12-25 13:55:36 +00:00
|
|
|
|
|
|
|
substitute ${./wrapper.sh} $out/bin/$progname \
|
|
|
|
--subst-var-by bash ${emacs.stdenv.shell} \
|
|
|
|
--subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \
|
2023-10-09 19:29:22 +00:00
|
|
|
--subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \
|
2020-12-25 13:55:36 +00:00
|
|
|
--subst-var prog
|
|
|
|
chmod +x $out/bin/$progname
|
2023-10-09 19:29:22 +00:00
|
|
|
# Create a “NOP” binary wrapper for the pure sake of it becoming a
|
|
|
|
# non-shebang, actual binary. See the makeBinaryWrapper docs for rationale
|
|
|
|
# (summary: it allows you to use emacs as a shebang itself on Darwin,
|
|
|
|
# e.g. #!$ {emacs}/bin/emacs --script)
|
|
|
|
wrapProgramBinary $out/bin/$progname
|
2020-04-24 23:36:52 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Wrap MacOS app
|
|
|
|
# this has to pick up resources and metadata
|
|
|
|
# to recognize it as an "app"
|
|
|
|
if [ -d "$emacs/Applications/Emacs.app" ]; then
|
|
|
|
mkdir -p $out/Applications/Emacs.app/Contents/MacOS
|
|
|
|
cp -r $emacs/Applications/Emacs.app/Contents/Info.plist \
|
|
|
|
$emacs/Applications/Emacs.app/Contents/PkgInfo \
|
|
|
|
$emacs/Applications/Emacs.app/Contents/Resources \
|
|
|
|
$out/Applications/Emacs.app/Contents
|
2020-12-25 13:55:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
substitute ${./wrapper.sh} $out/Applications/Emacs.app/Contents/MacOS/Emacs \
|
|
|
|
--subst-var-by bash ${emacs.stdenv.shell} \
|
|
|
|
--subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \
|
2023-10-09 19:29:22 +00:00
|
|
|
--subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \
|
2020-12-25 13:55:36 +00:00
|
|
|
--subst-var-by prog "$emacs/Applications/Emacs.app/Contents/MacOS/Emacs"
|
|
|
|
chmod +x $out/Applications/Emacs.app/Contents/MacOS/Emacs
|
2023-10-09 19:29:22 +00:00
|
|
|
wrapProgramBinary $out/Applications/Emacs.app/Contents/MacOS/Emacs
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p $out/share
|
|
|
|
# Link icons and desktop files into place
|
2023-10-09 19:29:22 +00:00
|
|
|
for dir in applications icons info man; do
|
2020-04-24 23:36:52 +00:00
|
|
|
ln -s $emacs/share/$dir $out/share/$dir
|
|
|
|
done
|
|
|
|
''
|