How to set up Tmux under Alacritty terminal shell config

I am new to Nix. My question is how to set up Tmux in Alacritty with a username independent approach. I put Alacritty configuration in alacrity.nix and import it to home.nix. In Alacritty.nix, I write the following to set up tmux

# alacritty.nix
{config, lib, pkgs, ...} :
{
  programs.alacritty = {
    enable = true;
    settings = {
        terminal.shell = {
        args = ["new-session"  "-A"  "-D" "-s" "main"];
        program = "/etc/profiles/per-user/ricky/bin/tmux";
      };
    };
}

where tmux locates in /etc/profiles/per-user/ricky/bin/tmux. The path of tmux is obtained from running whereis tmux inside terminal. In fact, this configuration works but the location of tmux is too specific for username = ricky. Imagine that I set up Alacritty based on this file in my new Mac and the username of the Mac is username = ricky-dev. If I run darwin-rebuild switch to config this new Mac, Alacritty will crash since /etc/profiles/per-user/ricky/bin/tmux is not the correct path of tmux in my new Mac now. Is it possible to avoid such “Path/ username dependent” approach to configure tmux inside Alacritty? Thank you.

P.S.
my home.nix config

{config, pkgs, ...} :
  {
    home.stateVersion = "24.11";

    programs.home-manager.enable = true;

    imports = [
    ./tmux.nix
    ./zsh.nix
    ./alacritty.nix
    ./starship.nix
  ];
}

I partially solve my problem my modifying the home.nix file. I add username as a input variable for home.nix

{config, pkgs,username, ...} :
  {
    home.username = "${username}";
    home.stateVersion = "24.11";
    home.file = {
    };

    programs.home-manager.enable = true;

    imports = [
    ./tmux.nix
    ./zsh.nix
    ./alacritty.nix
    ./starship.nix
  ];
}

I define the username inside flake.nix. Then in alacritty.nix, I can modify the tmux path by

# alacritty.nix
{config, lib, pkgs, ...} :
{
  programs.alacritty = {
    enable = true;
    settings = {
        terminal.shell = {
        args = ["new-session"  "-A"  "-D" "-s" "main"];
        program = "/etc/profiles/per-user/${config.home.username}/bin/tmux";
      };
    };
}

Now the tmux still works in my MacBook. I will test whether it works on my other MacBook with different username.

1 Like

You can use string interpolation to point to the tmux in the Nix store, which is user-independant:

$ nix repl

nix-repl> pkgs = import <nixpkgs> {}

nix-repl> program = "${pkgs.tmux}/bin/tmux"

nix-repl> program
"/nix/store/d8hzzxpnn22h6081a95r4889bc4zpb0y-tmux-3.5a/bin/tmux"

As such, in your config, you’d use:

# alacritty.nix
{config, lib, pkgs, ...} :
{
  programs.alacritty = {
    enable = true;
    settings = {
        terminal.shell = {
        args = ["new-session"  "-A"  "-D" "-s" "main"];
        program = "${pkgs.tmux}/bin/tmux";
      };
    };
}

You should note that interpolation won’t automatically add tmux to the store, so you’d still need to install it (which is what you’re doing in tmux.nix, I assume).

1 Like

Thank you. It works!
Yes, I config tmux installation in flake.nix. In tmux.nix I set enable = true and migrate my previous config from tmux.conf.

1 Like