# Building tmux RPMs for CentOS 6/7

John Simpson `<jms1@jms1.net>` 2017-08-02

Built on `jms1.net` server, running CentOS 6.

The `tmux` packages in the EPEL repo are hideously out of date... they are version 1.6, while certain features (such as support for iTerm2's integration) require version 1.8 or later.

## Set up RPM build environment

Create the usual `~/rpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS}` directories and `~/.rpmmacros` file.

```
mkdir -p ~/rpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
cat > ~/.rpmmacros <<EOF
%_topdir /home/jms1/rpm
%packager John Simpson <jms1@jms1.net>
%_signature gpg
%_gpg_name John Simpson (RPM signing key) <jms1@jms1.net>
%__gpg_sign_cmd %{__gpg} \
    gpg --force-v3-sigs --digest-algo=sha1 --batch --no-verbose --no-armor \
    --passphrase-fd 3 --no-secmem-warning -u "%{_gpg_name}" \
    -sbo %{__signature_filename} %{__plaintext_filename}
EOF
```

## Build `tmux` RPMs

### Download source RPMs

Download the latest "Fedora Rawhide" SOURCE RPMs for tmux.

* [tmux](https://rpmfind.net/linux/rpm2html/search.php?query=tmux)

### `libevent` and `libevent2`

Also, because I have `tor` installed on the server already, it has a dependency on `libevent2`, which is the CentOS name for v2 of `libevent`. If you don't already have it, you should install it (using `yum install libevent2-devel`, it's in the CentOS `base` repository.)

### Build `tmux` RPM

```
rpm -i tmux-2.5-1.src.rpm
```

```
cd ~/rpm/SPECS
mv tmux.spec tmux-2.5-1.spec
```

* **If building on CentOS 6**

    ```
    sed -i -e '/^BuildRequires:/s/libevent\-/libevent2\-/' tmux-2.5-1.spec
    ```

```
rpmbuild -ba libevent.spec
```

## Sign the RPMs

```
cd ~/rpm
rpm --addsign \
    RPMS/x86_64/tmux-debuginfo-2.5-1.el7.centos.x86_64.rpm \
    RPMS/x86_64/tmux-2.5-1.el7.centos.x86_64.rpm \
    SRPMS/tmux-2.5-1.el7.centos.src.rpm
```

## Upload the RPMs to the repo

```
cd ~/rpm
scp \
    RPMS/x86_64/tmux-debuginfo-2.5-1.el7.centos.x86_64.rpm \
    RPMS/x86_64/tmux-2.5-1.el7.centos.x86_64.rpm \
    SRPMS/tmux-2.5-1.el7.centos.src.rpm \
    jms1@jms1.net:/var/www/repo.jms1.net/docs/centos/7/
```

```
(jms1@jms1.net) $ cd /var/www/repo.jms.net/docs/centos/7/
(jms1@jms1.net) $ make
```

## Install

```
sudo -i
```

```
cd ~jms1/rpm/RPMS/x86_64
rpm -Fvh tmux-2.5-1.el6.x86_64.rpm
```

# Other

## `~/bin/t`

[http://www.howardism.org/Technical/Linux/tmux.html](http://www.howardism.org/Technical/Linux/tmux.html) shows a shell script which attaches to an existing `tmux` session, or creates a new one with a specific set of 

```bash
#!/bin/bash

if [[ "$TERM" != "screen" ]]
then
    if type tmux >/dev/null 2>&1
    then
        tmux attach || tmux \
            new -s jms1 \; \
            neww -n top "top -d 1"
    fi
fi
```

Using, from a Mac:

```
ssh -t jms1.net t
```

# Dealing with the SSH Agent

When you attach to a formerly detached tmux session, the environment variables which make it possible to reach your SSH agent will still be pointing to the agent from the original connection. This makes it impossible to use your *current* SSH agent.

The `tmux showenv` command provides a way to access *some* of the environment variables from the now-current SSH connection underlying the `tmux` sessions.

```
$ tmux showenv
-DISPLAY
-SSH_AGENT_PID
-SSH_ASKPASS
SSH_AUTH_SOCK=/tmp/ssh-puovNaaLWp/agent.6152
SSH_CONNECTION=2001:470:db22:0:801e:6203:b554:c190 53344 2600:3c02::f03c:91ff:fe6e:79fe 922
-WINDOWID
-XAUTHORITY
```

The `SSH_AUTH_SOCK` variable is used by `ssh` to interact with the `ssh-agent` running on your local workstation. The `SSH_CONNECTION` variable provides information about the client's and server's IP addresses and TCP port numbers.

Newer versions of `tmux` have a "`-s`" option which prints the same information in a format which can be `eval`'d by a shell within `tmux`, to update the variables.

```
$ tmux showenv -s
unset DISPLAY;
unset SSH_AGENT_PID;
unset SSH_ASKPASS;
SSH_AUTH_SOCK="/tmp/ssh-puovNaaLWp/agent.6152"; export SSH_AUTH_SOCK;
SSH_CONNECTION="2001:470:db22:0:801e:6203:b554:c190 53344 2600:3c02::f03c:91ff:fe6e:79fe 922"; export SSH_CONNECTION;
unset WINDOWID;
unset XAUTHORITY;
```

SO, when you attach to an existing `tmux` session, you should immediately run this command in order to "connect" the shell to the SSH agent which is now available:

```
eval $( tmux showenv -s | egrep '^(unset )?SSH_' )
```

So the `/etc/profile.d/tmux.sh` file now becomes...

```bash
function t {
    if [[ "$TERM" != "screen" ]]
    then
        if type tmux >/dev/null 2>&1
        then
            echo attaching/starting tmux
            tmux attach || tmux
        else
            echo tmux not found
        fi
    else
        echo updating SSH_xxx variables
        eval $( tmux showenv -s | egrep '^(unset )?SSH_' )
    fi
}
```
