2KAbhishek / tmux-tea

tmux sessions as easy as tea ☕🪟
GNU General Public License v3.0
20 stars 1 forks source link

[ENH]: add to nixpkgs for easier installation on NixOS #4

Open f4z3r opened 1 month ago

f4z3r commented 1 month ago

Description

It would be great if the plugin could be natively installed via NixOS (see https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=tmuxPlugins).

Additional Information

This can easily be achieved by coping a short block into this file: https://github.com/NixOS/nixpkgs/blob/master/pkgs/misc/tmux-plugins/default.nix

tmux-tea = mkTmuxPlugin rec {
  pluginName = "tea";
  version = "unstable-2024-06-02";
  src = pkgs.fetchFromGitHub {
    owner = "2KAbhishek";
    repo = "tmux-tea";
    rev = "8a150154a68a1a1d0c575098a1aa649da4090371";
    hash = "sha256-NGD2raQyhaeLRLvApyX/eX5y//rsaK/DuB0UTv4LkBw=";
  };
  meta = {
    homepage = "https://github.com/2KAbhishek/tmux-tea";
    description = "tmux sessions as easy as tea";
    license = lib.licenses.gpl3Only;
  };
};

This should work fine. I am currently already testing it with such an install on my setup: https://github.com/f4z3r/nix/blob/bd8b91c4c1eaf779a6f9ffbfb0761c3baf629bee/home/apps/tmux/default.nix#L98

2KAbhishek commented 3 weeks ago

Hey @f4z3r thanks for setting up this issue (along with the others)

They are on my radar, will try to get to those as soon as I get some time

Appreciate it

f4z3r commented 3 weeks ago

Hi @2KAbhishek , no worries. Let me know if I can help in some way. I just didn't want to open PRs to publish your software in some registries without your consent.

2KAbhishek commented 3 weeks ago

@f4z3r I am new to adding nixpkgs, was wondering about the hash in the pkg definition, how do you generate this?

hash = "sha256-NGD2raQyhaeLRLvApyX/eX5y//rsaK/DuB0UTv4LkBw=";

f4z3r commented 3 weeks ago

You can either try and build with a wrong hash to get the package manager to tell you which one is the real one, or you can rely on nix hash-path to get the hash:

git clone <repo> /tmp/repo
cd /tmp/repo
git checkout <revision>
rm -rf .git
cd /tmp
nix hash-path /tmp/repo

The last command should then return the hash of the repo at the specified revision.

2KAbhishek commented 3 weeks ago

Oh, I am not running a nix system right now, can you please send me the latest hash for this with the latest pull from main

f4z3r commented 3 weeks ago

The hash of the latest commit on main is sha256-WctBTsmuycbzTTiVqFlv+ttlELNn6sATbA/uJ/CcbXo=.

Note that you don't need to run a nix system to have nix installed. It is a standalone package manager that you can install on pretty much any Linux distro to be used side-by-side to your standard distro package manager.

2KAbhishek commented 3 weeks ago

I think we also need to add the dependencies for fzf and zoxide with this.

  tmux-tea = mkTmuxPlugin rec {
    pluginName = "tea";
    version = "unstable-2024-06-02";
    src = pkgs.fetchFromGitHub {
      owner = "2KAbhishek";
      repo = "tmux-tea";
      rev = "101ed293407914b6651f9a2a2079d988e03ac8d9";
      hash = "sha256-NGD2raQyhaeLRLvApyX/eX5y//rsaK/DuB0UTv4LkBw=";
    };
    meta = {
      homepage = "https://github.com/2KAbhishek/tmux-tea";
      description = "tmux sessions as easy as tea";
      license = lib.licenses.gpl3Only;
    };
    nativeBuildInputs = [ pkgs.makeWrapper ];
    postInstall = ''
      wrapProgram $out/share/tmux-plugins/tmux-tea/bin/tea.sh \
          --prefix PATH : ${with pkgs; lib.makeBinPath (
            [ pkgs.fzf pkgs.zoxide ]
          )}

      find $target -type f -print0 | xargs -0 sed -i -e 's|fzf |${pkgs.fzf}/bin/fzf |g'
      find $target -type f -print0 | xargs -0 sed -i -e 's|zoxide |${pkgs.zoxide}/bin/zoxide |g'
    '';
  };

How does this look to you?

Also is there a possibility to symlink the file bin/tea.sh as tea somewhere accessible to $PATH, do you know how that can be done?

f4z3r commented 3 weeks ago

Looks very good! There are a few small things I found out while testing this:

  1. rec in the function call of mkTmuxPlugin is superfluous.
  2. In ${with pkgs; lib.makeBinPath ([ pkgs.fzf pkgs.zoxide ])} you can either drop with pkgs; or drop the pkgs prefixes for fzf and zoxide.
  3. tea.sh needs to be an executable to be wrapped by wrapProgram. You therefore need something like chmod +x $target/bin/tea.sh before calling the wrapper.

Your usage of the postInstall is very nice to ensure fzf and zoxide are available for the plugin 👍🏽 I tested that this does what is expected, and it seemed to work.

I would also use $target mostly rather than recreating the $out/share/tmux-plugins path, since that is created in the mkTmuxPlugin function and might be subject to change. I would therefore have something like:

tmux-tea = mkTmuxPlugin {
  pluginName = "tea";
  version = "unstable-2024-06-02";
  src = pkgs.fetchFromGitHub {
    owner = "2KAbhishek";
    repo = "tmux-tea";
    rev = "101ed293407914b6651f9a2a2079d988e03ac8d9";
    hash = "sha256-NGD2raQyhaeLRLvApyX/eX5y//rsaK/DuB0UTv4LkBw=";
  };
  meta = {
    homepage = "https://github.com/2KAbhishek/tmux-tea";
    description = "tmux sessions as easy as tea";
    license = lib.licenses.gpl3Only;
  };
  nativeBuildInputs = [ pkgs.makeWrapper ];
  postInstall = ''
    chmod +x $target/bin/tea.sh
    wrapProgram $target/bin/tea.sh \
        --prefix PATH : ${with pkgs; lib.makeBinPath (
          [ fzf zoxide ]
        )}

    find $target -type f -print0 | xargs -0 sed -i -e 's|fzf |${pkgs.fzf}/bin/fzf |g'
    find $target -type f -print0 | xargs -0 sed -i -e 's|zoxide |${pkgs.zoxide}/bin/zoxide |g'
  '';
};

Regarding making tea available as a binary in the path, I am currently trying out various things.

I tried various things in the postInstall hook such as:

mkdir -p $out/bin
cp $target/bin/tea.sh $out/bin/tea

As typically nix will automatically pick up "binaries" in the $out/bin directory. However, that did not end up in my path when I built it this way. I will continue to have a look on how this can be achieved. I should get some time on the weekend at the latest to check this out in more detail.

2KAbhishek commented 3 weeks ago

Thanks, I'm planning to setup nix on my system this weekend as well, can do more experiments then :)

f4z3r commented 2 weeks ago

I have invested a little more into it, but still didn't manage to get the binary to be exposed under /run/current-system/sw/bin ...

Maybe a good idea would be to just open a PR and ask for support from the person that will review it on the nixpkgs repo. They really know their $hit and will be able to tell you in no time.