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.
-
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 $SESSIONIn the above
dev.tmuxscript we create a new session, then split the window both vertically and horizontally. -
We need to tell Nix to add the
dev.tmuxexternal 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); inwriteShellScriptBinlets us definedev-tmuxwrapping the local scriptdev.tmux -
Now we can create a
shell.nixscript: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
tmuxenvironment. When the nix script executes it will automatically call the dev-tmux to handle the screen layout. In addition there is atrapdefined to kill existing sessions on exit. Finally we useexecto initiate an isolated session. -
Run the script:
nix-shell --pureRemember a pure environment tries to ignore the existing host configuration 😂 Nix will automatically look for the file
shell.nixso you dont need to added this parameter 🥰Expected Output:
Welcome to Go Development Environment go version go1.25.5 darwin/arm64Awesome, Tmux is configured and ready to go!
-
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.