diff --git a/doc/package-notes.xml b/doc/package-notes.xml
index 2b7b4b9bc51..d2c660e22a9 100644
--- a/doc/package-notes.xml
+++ b/doc/package-notes.xml
@@ -325,6 +325,18 @@ packageOverrides = pkgs: {
elm2nix.
+
+ Kakoune
+
+
+ Kakoune can be built to autoload plugins:
+(kakoune.override {
+ configure = {
+ plugins = with pkgs.kakounePlugins; [ parinfer-rust ];
+ };
+})
+
+
Interactive shell helpers
diff --git a/pkgs/applications/editors/kakoune/default.nix b/pkgs/applications/editors/kakoune/default.nix
index 16596056c9a..8dfcc485668 100644
--- a/pkgs/applications/editors/kakoune/default.nix
+++ b/pkgs/applications/editors/kakoune/default.nix
@@ -3,7 +3,7 @@
with stdenv.lib;
stdenv.mkDerivation rec {
- name = "kakoune-${version}";
+ pname = "kakoune-unwrapped";
version = "2019.01.20";
src = fetchFromGitHub {
repo = "kakoune";
diff --git a/pkgs/applications/editors/kakoune/plugins.nix b/pkgs/applications/editors/kakoune/plugins.nix
new file mode 100644
index 00000000000..d2f3607d63c
--- /dev/null
+++ b/pkgs/applications/editors/kakoune/plugins.nix
@@ -0,0 +1,5 @@
+{ parinfer-rust }:
+
+{
+ inherit parinfer-rust;
+}
diff --git a/pkgs/applications/editors/kakoune/wrapper.nix b/pkgs/applications/editors/kakoune/wrapper.nix
new file mode 100644
index 00000000000..b4cc823880e
--- /dev/null
+++ b/pkgs/applications/editors/kakoune/wrapper.nix
@@ -0,0 +1,44 @@
+{ stdenv, bash }:
+with stdenv.lib;
+
+kakoune:
+
+let
+ getPlugins = { plugins ? [] }: plugins;
+
+ wrapper = { configure ? {} }:
+ stdenv.mkDerivation rec {
+ pname = "kakoune";
+ version = getVersion kakoune;
+
+ src = ./.;
+ buildCommand = ''
+ mkdir -p $out/share/kak
+ for plugin in ${strings.escapeShellArgs (getPlugins configure)}; do
+ if [[ -d $plugin/share/kak/autoload ]]; then
+ find "$plugin/share/kak/autoload" -type f -name '*.kak'| while read rcfile; do
+ printf 'source "%s"\n' "$rcfile"
+ done
+ fi
+ done >>$out/share/kak/plugins.kak
+
+ mkdir -p $out/bin
+ substitute ${src}/wrapper.sh $out/bin/kak \
+ --subst-var-by bash "${bash}" \
+ --subst-var-by kakoune "${kakoune}" \
+ --subst-var-by out "$out"
+ chmod +x $out/bin/kak
+ '';
+
+ preferLocalBuild = true;
+ buildInputs = [ bash kakoune ];
+ passthru = { unwrapped = kakoune; };
+
+ meta = kakoune.meta // {
+ # prefer wrapper over the package
+ priority = (kakoune.meta.priority or 0) - 1;
+ hydraPlatforms = [];
+ };
+ };
+in
+ makeOverridable wrapper
diff --git a/pkgs/applications/editors/kakoune/wrapper.sh b/pkgs/applications/editors/kakoune/wrapper.sh
new file mode 100644
index 00000000000..48a971a10c6
--- /dev/null
+++ b/pkgs/applications/editors/kakoune/wrapper.sh
@@ -0,0 +1,30 @@
+#!@bash@/bin/bash
+
+# We use the -E option to load plugins. This only makes sense when we are
+# starting a new session, so we detect that. Also, Kakoune can only handle
+# one -E option, so we prepend loading plugins to an existing one.
+args=( "$@" )
+loadPlugins=true
+EValueOffset=-1
+pluginScript='@out@/share/kak/plugins.kak'
+
+for (( i = 0; i < ${#args[@]}; i++ )); do
+ case "${args[i]}" in
+ -n|-c|-l|-p|-clear|-version) loadPlugins=false;;
+ -E) EValueOffset=$(( i + 1 ));;
+ --) break;;
+ esac
+ case "${args[i]}" in
+ -E|-c|-e|-s|-p|-f|-i|-ui|-debug) i=$(( i + 1 ));;
+ esac
+done
+
+if [[ $loadPlugins = true ]]; then
+ if (( EValueOffset >= 0 )); then
+ args[EValueOffset]="source '$pluginScript'"$'\n'"${args[EValueOffset]}"
+ else
+ args=( "-E" "source '$pluginScript'" "${args[@]}" )
+ fi
+fi
+
+exec @kakoune@/bin/kak "${args[@]}"
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 50ff45cd0b5..b8384e792c7 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -3947,7 +3947,10 @@ in
kalibrate-hackrf = callPackage ../applications/radio/kalibrate-hackrf { };
- kakoune = callPackage ../applications/editors/kakoune { };
+ wrapKakoune = callPackage ../applications/editors/kakoune/wrapper.nix { };
+ kakounePlugins = callPackage ../applications/editors/kakoune/plugins.nix { };
+ kakoune-unwrapped = callPackage ../applications/editors/kakoune { };
+ kakoune = wrapKakoune kakoune-unwrapped { };
kbdd = callPackage ../applications/window-managers/kbdd { };