OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 034

Configuring a terminal environment with Home Manager

Use modules to configure Zsh, Starship, and common command-line tools.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSHome Manager: managing user configuration ↗
About 2 min readStart reading

Configuring Zsh with Nix#

Home Manager provides powerful programs.zsh options that let you manage all your plugins in Nix code.

{ pkgs, lib, ... }: {
  programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestion.enable = true;
    syntaxHighlighting.enable = true;

    # 别名 (Aliases)
    shellAliases = {
      ll = "ls -l";
      build-system = "nixos-rebuild build --flake .#my-host";
    };

    # 插件 (Plugins)
    plugins = [
      {
        name = "zsh-nix-shell";
        file = "nix-shell.plugin.zsh";
        src = pkgs.fetchFromGitHub {
          owner = "chisui";
          repo = "zsh-nix-shell";
          rev = "v0.8.0";
          hash = lib.fakeHash; # 占位;首次构建核验来源后填写实际固定输出 hash
        };
      }
    ];

    # 自定义 .zshrc 内容
    initExtra = ''
      export PATH=$HOME/.bin:$PATH
    '';
  };
}

The prompt: Starship#

If you like a modern terminal prompt, configuration is straightforward:

{
  programs.starship = {
    enable = true;
    # 这里可以自定义 starship.toml
    settings = {
      add_newline = false;
      character = {
        success_symbol = "[➜](bold green)";
        error_symbol = "[➜](bold red)";
      };
    };
  };
}

Common command-line tools (CLI)#

These tools also have dedicated configuration options in Home Manager:

  • programs.git: Configure your username, email address, and aliases.
  • programs.fzf: Automatically enable Zsh bindings.
  • programs.direnv: Automatically load the development environment when you enter a directory.
  • programs.bat: A cat with syntax highlighting.

Why do this?#

  1. Ready to use: Clone your configuration on a new machine and run a command to install all your shell plugins and themes automatically.
  2. Reusable configuration: You can easily put your Zsh configuration in a separate file and share it across machines, even on macOS.

Migration tip#

At the configuration.nix level, you only need to specify users.defaultUserShell = pkgs.zsh;; leave all the shell details to Home Manager.

Version compatibility and shell responsibilities#

The example retains the initExtra syntax from older Home Manager versions; newer versions may use initContent. Verify plugin versions and hashes against the actual pinned sources rather than treating placeholders as validated configuration. The system layer also needs to enable programs.zsh.enable and set the user’s login shell; Home Manager manages the user configuration.

References & further reading

  1. Home Manager manual ↗
  2. Home Manager command source ↗
  3. Home Manager file options source ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Home Manager: managing user configuration

  1. 01Home Manager integration and modular configuration
  2. 02Configuring a terminal environment with Home Manager
  3. 03Desktop and theme configuration with Home Manager
  4. 04Home Manager files and symbolic links
  5. 05Secrets management with Home Manager and NixOS
  6. 06A tutorial on user-level configuration with Home Manager
CONTINUE READING / NEXT ARTICLEDesktop and theme configuration with Home Manager

Separate system-session and user-appearance responsibilities when configuring Hyprland, Plasma, and GTK.