2015-12-17 14:08:18 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
|
|
|
`emacsWithPackages` takes a single argument: a function from a package
|
|
|
|
set to a list of packages (the packages that will be available in
|
|
|
|
Emacs). For example,
|
|
|
|
```
|
|
|
|
emacsWithPackages (epkgs: [ epkgs.evil epkgs.magit ])
|
|
|
|
```
|
|
|
|
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
|
|
|
|
|
|
|
|
`emacsWithPackages` inherits the package set which contains it, so the
|
|
|
|
correct way to override the provided package set is to override the
|
|
|
|
set which contains `emacsWithPackages`. For example, to override
|
2019-08-08 00:52:22 +02:00
|
|
|
`emacsPackages.emacsWithPackages`,
|
2015-12-17 14:08:18 +01:00
|
|
|
```
|
|
|
|
let customEmacsPackages =
|
2019-08-08 00:52:22 +02:00
|
|
|
emacsPackages.overrideScope' (self: super: {
|
2015-12-17 14:08:18 +01:00
|
|
|
# use a custom version of emacs
|
|
|
|
emacs = ...;
|
|
|
|
# use the unstable MELPA version of magit
|
|
|
|
magit = self.melpaPackages.magit;
|
|
|
|
});
|
|
|
|
in customEmacsPackages.emacsWithPackages (epkgs: [ epkgs.evil epkgs.magit ])
|
|
|
|
```
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-09-18 22:28:24 +02:00
|
|
|
{ lib, lndir, makeWrapper, runCommand }: self:
|
2015-04-17 18:24:27 +02:00
|
|
|
|
2020-11-16 20:24:29 +01:00
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (self) emacs;
|
|
|
|
|
|
|
|
nativeComp = emacs.nativeComp or false;
|
|
|
|
|
|
|
|
in
|
2015-04-17 18:24:27 +02:00
|
|
|
|
2016-08-19 22:09:41 +02:00
|
|
|
packagesFun: # packages explicitly requested by the user
|
2015-12-17 03:53:12 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
explicitRequires =
|
2018-01-31 20:02:19 +01:00
|
|
|
if lib.isFunction packagesFun
|
2015-12-17 03:53:12 +01:00
|
|
|
then packagesFun self
|
|
|
|
else packagesFun;
|
|
|
|
in
|
2015-04-17 18:24:27 +02:00
|
|
|
|
2019-09-18 22:28:24 +02:00
|
|
|
runCommand
|
|
|
|
(appendToName "with-packages" emacs).name
|
|
|
|
{
|
|
|
|
nativeBuildInputs = [ emacs lndir makeWrapper ];
|
|
|
|
inherit emacs explicitRequires;
|
|
|
|
|
|
|
|
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"
|
|
|
|
{ inherit explicitRequires lndir emacs; }
|
|
|
|
''
|
|
|
|
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
|
2020-12-20 21:15:18 +01:00
|
|
|
${optionalString nativeComp ''
|
2020-11-16 20:24:29 +01:00
|
|
|
mkdir -p $out/share/emacs/native-lisp
|
|
|
|
''}
|
2019-09-18 22:28:24 +02: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"
|
2020-11-16 20:24:29 +01:00
|
|
|
${optionalString nativeComp ''
|
|
|
|
linkPath "$1" "share/emacs/native-lisp" "share/emacs/native-lisp"
|
|
|
|
''}
|
2019-09-18 22:28:24 +02: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.
|
|
|
|
rm -f $siteStart $siteStartByteCompiled $subdirs $subdirsByteCompiled
|
|
|
|
cat >"$siteStart" <<EOF
|
|
|
|
(load-file "$emacs/share/emacs/site-lisp/site-start.el")
|
|
|
|
(add-to-list 'load-path "$out/share/emacs/site-lisp")
|
|
|
|
(add-to-list 'exec-path "$out/bin")
|
2020-11-16 20:24:29 +01:00
|
|
|
${optionalString nativeComp ''
|
|
|
|
(add-to-list 'comp-eln-load-path "$out/share/emacs/native-lisp/")
|
|
|
|
''}
|
2019-09-18 22:28:24 +02:00
|
|
|
EOF
|
|
|
|
# Link subdirs.el from the emacs distribution
|
|
|
|
ln -s $emacs/share/emacs/site-lisp/subdirs.el -T $subdirs
|
|
|
|
|
|
|
|
# Byte-compiling improves start-up time only slightly, but costs nothing.
|
|
|
|
$emacs/bin/emacs --batch -f batch-byte-compile "$siteStart" "$subdirs"
|
2020-11-16 20:24:29 +01:00
|
|
|
|
|
|
|
${optionalString nativeComp ''
|
|
|
|
$emacs/bin/emacs --batch \
|
|
|
|
--eval "(add-to-list 'comp-eln-load-path \"$out/share/emacs/native-lisp/\")" \
|
|
|
|
-f batch-native-compile "$siteStart" "$subdirs"
|
|
|
|
''}
|
2019-09-18 22:28:24 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
inherit (emacs) meta;
|
|
|
|
}
|
|
|
|
''
|
2016-05-12 22:15:40 +02:00
|
|
|
mkdir -p "$out/bin"
|
2015-04-17 18:24:27 +02:00
|
|
|
|
|
|
|
# Wrap emacs and friends so they find our site-start.el before the original.
|
|
|
|
for prog in $emacs/bin/*; do # */
|
2016-01-20 19:53:11 +01:00
|
|
|
local progname=$(basename "$prog")
|
|
|
|
rm -f "$out/bin/$progname"
|
2020-12-09 16:53:10 +01:00
|
|
|
|
|
|
|
substitute ${./wrapper.sh} $out/bin/$progname \
|
|
|
|
--subst-var-by bash ${emacs.stdenv.shell} \
|
|
|
|
--subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \
|
2020-11-16 20:24:29 +01:00
|
|
|
--subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp:" \
|
2020-12-09 16:53:10 +01:00
|
|
|
--subst-var prog
|
|
|
|
chmod +x $out/bin/$progname
|
2015-04-17 18:24:27 +02:00
|
|
|
done
|
2015-05-17 21:15:39 +02:00
|
|
|
|
2017-05-21 01:51:32 +02:00
|
|
|
# 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-09 16:53:10 +01:00
|
|
|
|
2020-11-16 20:24:29 +01:00
|
|
|
|
2020-12-09 16:53:10 +01:00
|
|
|
substitute ${./wrapper.sh} $out/Applications/Emacs.app/Contents/MacOS/Emacs \
|
|
|
|
--subst-var-by bash ${emacs.stdenv.shell} \
|
2020-12-12 05:20:00 +01:00
|
|
|
--subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \
|
|
|
|
--subst-var-by prog "$emacs/Applications/Emacs.app/Contents/MacOS/Emacs"
|
|
|
|
chmod +x $out/Applications/Emacs.app/Contents/MacOS/Emacs
|
2020-11-16 20:24:29 +01:00
|
|
|
|
|
|
|
makeWrapper $emacs/Applications/Emacs.app/Contents/MacOS/Emacs $out/Applications/Emacs.app/Contents/MacOS/Emacs \
|
|
|
|
--suffix EMACSLOADPATH ":" "$deps/share/emacs/site-lisp:" \
|
|
|
|
--suffix EMACSNATIVELOADPATH ":" "$deps/share/emacs/native-lisp:"
|
2017-05-21 01:51:32 +02:00
|
|
|
fi
|
|
|
|
|
2015-05-17 21:15:39 +02:00
|
|
|
mkdir -p $out/share
|
|
|
|
# Link icons and desktop files into place
|
2020-06-02 11:50:36 +02:00
|
|
|
for dir in applications icons info man emacs; do
|
2015-05-17 21:15:39 +02:00
|
|
|
ln -s $emacs/share/$dir $out/share/$dir
|
|
|
|
done
|
2019-09-18 22:28:24 +02:00
|
|
|
''
|