Merge pull request #69045 from rycee/emacs-wrapper-using-runcommand

emacs: use runCommand to generate wrapper
This commit is contained in:
adisbladis 2019-09-27 12:58:38 +01:00 committed by GitHub
commit 17a388d9dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 89 deletions

View File

@ -32,7 +32,7 @@ in customEmacsPackages.emacsWithPackages (epkgs: [ epkgs.evil epkgs.magit ])
*/ */
{ lib, lndir, makeWrapper, runCommand, stdenv }: self: { lib, lndir, makeWrapper, runCommand }: self:
with lib; let inherit (self) emacs; in with lib; let inherit (self) emacs; in
@ -45,105 +45,110 @@ let
else packagesFun; else packagesFun;
in in
stdenv.mkDerivation { runCommand
name = (appendToName "with-packages" emacs).name; (appendToName "with-packages" emacs).name
nativeBuildInputs = [ emacs lndir makeWrapper ]; {
inherit emacs explicitRequires; nativeBuildInputs = [ emacs lndir makeWrapper ];
inherit emacs explicitRequires;
# Store all paths we want to add to emacs here, so that we only need to add preferLocalBuild = true;
# one path to the load lists allowSubstitutes = false;
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 # Store all paths we want to add to emacs here, so that we only need to add
# nix-shell doesn't use impure bash. This should replace the O(n) # one path to the load lists
# case with an O(1) hash map lookup, assuming bash is implemented deps = runCommand "emacs-packages-deps"
# well :D. { inherit explicitRequires lndir emacs; }
local varSlice="$var[*]" ''
# ''${..-} to hack around old bash empty array problem findInputsOld() {
case "''${!varSlice-}" in local pkg="$1"; shift
*" $pkg "*) return 0 ;; local var="$1"; shift
esac local propagatedBuildInputsFiles=("$@")
unset -v varSlice
eval "$var"'+=("$pkg")' # 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
if ! [ -e "$pkg" ]; then eval "$var"'+=("$pkg")'
echo "build input $pkg does not exist" >&2
exit 1
fi
local file if ! [ -e "$pkg" ]; then
for file in "''${propagatedBuildInputsFiles[@]}"; do echo "build input $pkg does not exist" >&2
file="$pkg/nix-support/$file" exit 1
[[ -f "$file" ]] || continue fi
local pkgNext local file
for pkgNext in $(< "$file"); do for file in "''${propagatedBuildInputsFiles[@]}"; do
findInputsOld "$pkgNext" "$var" "''${propagatedBuildInputsFiles[@]}" file="$pkg/nix-support/$file"
done [[ -f "$file" ]] || continue
done
}
mkdir -p $out/bin
mkdir -p $out/share/emacs/site-lisp
local requires local pkgNext
for pkg in $explicitRequires; do for pkgNext in $(< "$file"); do
findInputsOld $pkg requires propagated-user-env-packages findInputsOld "$pkgNext" "$var" "''${propagatedBuildInputsFiles[@]}"
done done
# requires now holds all requested packages and their transitive dependencies done
}
mkdir -p $out/bin
mkdir -p $out/share/emacs/site-lisp
linkPath() { local requires
local pkg=$1 for pkg in $explicitRequires; do
local origin_path=$2 findInputsOld $pkg requires propagated-user-env-packages
local dest_path=$3 done
# requires now holds all requested packages and their transitive dependencies
# Add the path to the search path list, but only if it exists linkPath() {
if [[ -d "$pkg/$origin_path" ]]; then local pkg=$1
$lndir/bin/lndir -silent "$pkg/$origin_path" "$out/$dest_path" local origin_path=$2
fi local dest_path=$3
}
linkEmacsPackage() { # Add the path to the search path list, but only if it exists
linkPath "$1" "bin" "bin" if [[ -d "$pkg/$origin_path" ]]; then
linkPath "$1" "share/emacs/site-lisp" "share/emacs/site-lisp" $lndir/bin/lndir -silent "$pkg/$origin_path" "$out/$dest_path"
} fi
}
# Iterate over the array of inputs (avoiding nix's own interpolation) linkEmacsPackage() {
for pkg in "''${requires[@]}"; do linkPath "$1" "bin" "bin"
linkEmacsPackage $pkg linkPath "$1" "share/emacs/site-lisp" "share/emacs/site-lisp"
done }
siteStart="$out/share/emacs/site-lisp/site-start.el" # Iterate over the array of inputs (avoiding nix's own interpolation)
siteStartByteCompiled="$siteStart"c for pkg in "''${requires[@]}"; do
subdirs="$out/share/emacs/site-lisp/subdirs.el" linkEmacsPackage $pkg
subdirsByteCompiled="$subdirs"c done
# A dependency may have brought the original siteStart or subdirs, delete siteStart="$out/share/emacs/site-lisp/site-start.el"
# it and create our own siteStartByteCompiled="$siteStart"c
# Begin the new site-start.el by loading the original, which sets some subdirs="$out/share/emacs/site-lisp/subdirs.el"
# NixOS-specific paths. Paths are searched in the reverse of the order subdirsByteCompiled="$subdirs"c
# 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")
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. # A dependency may have brought the original siteStart or subdirs, delete
$emacs/bin/emacs --batch -f batch-byte-compile "$siteStart" "$subdirs" # 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")
EOF
# Link subdirs.el from the emacs distribution
ln -s $emacs/share/emacs/site-lisp/subdirs.el -T $subdirs
phases = [ "installPhase" ]; # Byte-compiling improves start-up time only slightly, but costs nothing.
installPhase = '' $emacs/bin/emacs --batch -f batch-byte-compile "$siteStart" "$subdirs"
'';
inherit (emacs) meta;
}
''
mkdir -p "$out/bin" mkdir -p "$out/bin"
# Wrap emacs and friends so they find our site-start.el before the original. # Wrap emacs and friends so they find our site-start.el before the original.
@ -172,6 +177,4 @@ EOF
for dir in applications icons info man; do for dir in applications icons info man; do
ln -s $emacs/share/$dir $out/share/$dir ln -s $emacs/share/$dir $out/share/$dir
done done
''; ''
inherit (emacs) meta;
}

View File

@ -60,7 +60,7 @@ let
}; };
emacsWithPackages = import ../build-support/emacs/wrapper.nix { emacsWithPackages = import ../build-support/emacs/wrapper.nix {
inherit lib lndir makeWrapper stdenv runCommand; inherit lib lndir makeWrapper runCommand;
}; };
mkManualPackages = import ../applications/editors/emacs-modes/manual-packages.nix { mkManualPackages = import ../applications/editors/emacs-modes/manual-packages.nix {