BACK TO THE HOMEPAGE

Feb 03, 2026 3 min read

Nix Tmux

A quick blog post on using nix with Tmux.

Tmux is one of my favourite tools. Recently I have been playing with Go Networking which often requires multiple terminals. Fortunately Tmux is great for this experience, but I often want to have a pre-defined setup.

Enter Nix and Tmux to manage the environment effortlessly. You can amend the configuration used to setup the screen however you wish. Just amend the dev.tmux script to realign your screen layout however you want.

Nix Scripts

We can define a script to represent the environment requirement.

  1. Lets create a tmux layout script dev.tmux:

    # dev.tmux
    SESSION="go-dev"
    
    # Check if the session already exists
    tmux has-session -t $SESSION 2>/dev/null
    
    if [ $? != 0 ]; then
      # Create session, split panes
      tmux new-session -d -s $SESSION
      tmux split-window -h -t $SESSION
      tmux split-window -v -t $SESSION
    fi
    
    # Attach
    tmux attach-session -t $SESSION
    

    In the above dev.tmux script we create a new session, then split the window both vertically and horizontally.

  2. We need to tell Nix to add the dev.tmux external script as a dependency.

    let
      # External Script: This reads './dev.tmux' and puts it into a binary named 'dev-tmux'
      tmuxLayout   = pkgs.writeShellScriptBin "dev-tmux" (builtins.readFile ./dev.tmux);
    in
    

    writeShellScriptBin lets us define dev-tmux wrapping the local script dev.tmux

  3. Now we can create a shell.nix script:

    with import <nixpkgs> {};
    
    let
      # External Script: This reads './dev.tmux' and puts it into a binary named 'dev-tmux'
      tmuxLayout   = pkgs.writeShellScriptBin "dev-tmux" (builtins.readFile ./dev.tmux);
    in
    pkgs.mkShell {
    
      name = "go-dev";
      nativeBuildInputs = with pkgs; [
        go
        tmux
        tmuxLayout   # Custom script with Tmux layout 
      ];
    
      LANGUAGE = "Go";
      VERSION  = "go version";
    
      shellHook = ''
        # Optional: Script environment start up
        echo "Welcome to $LANGUAGE Development Environment"
        $VERSION
    
        # The Trap: This kills the tmux session as soon as you exit the nix-shell
        # It ensures no "ghost" sessions stay running in the background.
        trap "tmux kill-session -t go-dev" EXIT
    
        # Perform Tmux Dev Layout
        exec dev-tmux
      '';
    }
    

    The above script will still give us a tmux environment. When the nix script executes it will automatically call the dev-tmux to handle the screen layout. In addition there is a trap defined to kill existing sessions on exit. Finally we use exec to initiate an isolated session.

  4. Run the script:

    nix-shell --pure
    

    Remember a pure environment tries to ignore the existing host configuration 😂 Nix will automatically look for the file shell.nix so you dont need to added this parameter 🥰

    Expected Output:

    Welcome to Go Development Environment
    go version go1.25.5 darwin/arm64
    

    Awesome, Tmux is configured and ready to go!

  5. Exit the Nix Shell

    To exit, you will need to exit each screen individually.

    exit
    

Nice we now have a super easy way to run Tmux on a Nix machine.