#!/usr/bin/env bash # Given a to a link plugin on GitHub, add the plugin to nixpkgs # - Run from the `nixpkgs` repository # - Provide plugin names in the author/plugin format # Requires git and nix to be in the environment # Example usage: # vim-plugin-add.sh nvim-lua/plenary.nvim letieu/btw.nvim set -eu msg() { echo ' ' "$@"; } err() { msg "$@" >&2;} dbg() { if [ "${DEBUG:-false}" = true ]; then echo; echo " DEBUG:" "$@"; echo; fi; } nix_build_vimplug() { for p; do nix build --quiet --no-link .#vimPlugins."$p" done } nix_test_build() { if nix_build_vimplug "$1"; then msg msg "Build of $1 succeeded!" return 0 else msg msg "Build of $1 failed." return 1 fi } if [ $# -lt 1 ]; then err 'Please pass the plugins you want to add as arguments, like so:' err "$0 Zeioth/compiler.nvim jmbuhr/otter.nvim" exit 1 fi if ! git remote get-url origin | grep --silent -P 'nixpkgs.git$'; then err 'Please execute this from the root of the nixpkgs repository' exit 1 fi if [ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]; then msg 'WARNING: currently not on master branch!' fi nixpkgs_dir="$(git rev-parse --show-toplevel)" nixpkgs_vim_plugin_dir=pkgs/applications/editors/vim/plugins tmp_dir="$(mktemp -d)" tmp_generated_nix="$(mktemp)" tmp_ending_nix="$(mktemp)" urls=() for p; do msg "Adding $p" # set basic variables plugin_name="${p##*/}" plugin_author="${p%/*}" repo_url="https://github.com/$p" nixpkgs_plugin_name="$(tr . - <<< "$plugin_name")" # the plugin name, as nixpkgs will know it plugin_repo_tmp_dir="$tmp_dir/$plugin_name" # get info from plugin repository dbg "Getting plugin git repository" git clone --quiet --depth=1 "$repo_url" "$plugin_repo_tmp_dir" cd "$plugin_repo_tmp_dir" git_rev_hash="$(git rev-parse HEAD)" git_date_version="$(git show -s --format=%cs "$git_rev_hash")" rm -rf .git repo_hash="$(nix hash path .)" # add the plugin with vim-plugins-updater dbg "Running vimPluginsUpdater" cd "$nixpkgs_dir" rm -rf "$plugin_repo_tmp_dir" git checkout --quiet -b "$plugin_name" dbg "actually running the updater" # nix-shell -p vimPluginsUpdater --command "vim-plugins-updater add $p" vim-plugins-updater add "$p" # Append the new plugin to generated.nix dbg "Writing generated.nix" cd ./"$nixpkgs_vim_plugin_dir" head -n -2 generated.nix > "$tmp_generated_nix" editor_start_line="$(wc -l "$tmp_generated_nix")" cat <<- EOF > "$tmp_ending_nix" $nixpkgs_plugin_name = buildVimPlugin { pname = "$nixpkgs_plugin_name"; version = "$git_date_version"; src = fetchFromGitHub { owner = "$plugin_author"; repo = "$plugin_name"; rev = "$git_rev_hash"; sha256 = "$repo_hash"; }; meta.homepage = "$repo_url"; }; } EOF cat "$tmp_generated_nix" "$tmp_ending_nix" > generated.nix first=true until nix_test_build "$nixpkgs_plugin_name" && ! $first; do if [ $first = true ]; then msg "Please make any needed changes and then exit to automatically commit continue." msg "Such as adding dependencies to $nixpkgs_vim_plugin_dir/overrides.nix" msg "Exit to commit and push your changes" first=false fi $SHELL done git commit -a --amend git push --set-upstream origin "$plugin_name" --force git switch - # shellcheck disable=SC2016 urls+=("https://$(git remote get-url origin | sed -r 's/^[^@]+@//g; s_:_/_g; s_\.git$__g')/pull/new/$plugin_name") echo done echo "Pull Request URLs:" for url in "${urls[@]}"; do echo " - $url" done # Cleanup rm -rf "$tmp_dir" "$tmp_ending_nix" "$tmp_generated_nix" # vim: sw=3 ts=3 et