Setup Zsh + Oh-my-zsh + PowerLevel10K - NixOS (without Home-manager)

Hello folks! Hope you’re doing good? :sunny:

Here’s my today’s challenge : How to setup complete terminal environment with Zsh + Oh-my-zsh + PowerLevel10K, using only nix derivation (only NixOS config, no Home-manager)

I’m not using Home-manager as I’m the only user (except root), and will ever be. I’m trying to achieve the most through Nix only, because I feel like it can do the job just fine.

Here’s my current config :

# shell.nix

{ pkgs, ... }: {
  environment.systemPackages = with pkgs; [ zsh-powerlevel10k ];
  programs.zsh = {
    enable = true;
    enableCompletion = true;
    enableBashCompletion = true;
    autosuggestions.enable = true;
    syntaxHighlighting.enable = true;
    histSize = 10000;
    shellAliases = {
      #...
    };
    setOptions = [
      "AUTO_CD"
    ]
    prompInit = ''
      source ${pkgs.zsh-powerlevel10k}/share/zsh-powerlevel10k/powerlevel10k.zsh-theme
    '';
    ohMyZsh = {
      enable = true;
      plugins = [ "git" "dirhistory" "history" ];
    };
  };
  users.defaultUserShell = pkgs.zsh;
  system.userActivationScripts.zshrc = "touch .zshrc";
  environment.shells = with pkgs; [ zsh ];
}

With the latter, I have all three components almost working as expected. What’s remaining is the declarative P10K config initialization… I’ve generated the (long) config (.p10k.zsh) through the “manual” (p10k configure) command, and since I’ve enabled the p10k “instant prompt” it added a code snippet to my .zshrc file too.

  • I’d like to provide/generate those two files declaratively when building generation (with the current config), how to achieve that? For both the default user (me) and root.
    • .p10k.zsh
    • .zshrc

Here’s some questions about my setup :

  • Is OMZ (Oh-my-zsh) required in my case?
  • Does the Zsh + OMZ + P10K stack makes sense out there, the way I use it?
    • I use zsh/omz to enjoy the plugins and theme eco-system around it, but if I can simplify it, let me know. I just want a CLI that makes my life easier.
  • Can root user inherit from those configs as well? When I sudo -i for example?
  • Can it integrate easily with nix-shell? how?

There’s a related thread, but it looks like home-manager config to me…

Thanks for your time! :pray:

I’ve finally found a way (probably not the best, but enough for now I guess)!

Here’s the corresponding NixOS configuration :

{ pkgs, ... }:
{
  environment.systemPackages = with pkgs; [
    zsh-powerlevel10k
    meslo-lgs-nf
  ];

  environment.etc."powerlevel10k/p10k.zsh".source = ./p10k.zsh;

  environment.shellAliases = {
    nshell = "nix-shell --run $SHELL"
  };

  programs.zsh = {
    enable = true;
    enableCompletion = true;
    enableBashCompletion = true;
    autosuggestions.enable = true;
    syntaxHighlighting.enable = true;
    histSize = 10000;
    promptInit = ''
      # this act as your ~/.zshrc but for all users (/etc/zshrc)
      source ${pkgs.zsh-powerlevel10k}/share/zsh-powerlevel10k/powerlevel10k.zsh-theme
      source /etc/powerlevel10k/p10k.zsh

      # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
      # Initialization code that may require console input (password prompts, [y/n]
      # confirmations, etc.) must go above this block; everything else may go below.
      # double single quotes ('') to escape the dollar char
      if [[ -r "''${XDG_CACHE_HOME:-''$HOME/.cache}/p10k-instant-prompt-''${(%):-%n}.zsh" ]]; then
        source "''${XDG_CACHE_HOME:-''$HOME/.cache}/p10k-instant-prompt-''${(%):-%n}.zsh"
      fi

      # uncomment if you want to customize your LS_COLORS
      # https://manpages.ubuntu.com/manpages/plucky/en/man5/dir_colors.5.html
      #LS_COLORS='...'
      #export LS_COLORS
    '';
  };

  users.defaultUserShell = pkgs.zsh;
  system.userActivationScripts.zshrc = "touch .zshrc"; # to avoid being prompted to generate the config for first time
  environment.shells = pkgs.zsh; # https://wiki.nixos.org/wiki/Zsh#GDM_does_not_show_user_when_zsh_is_the_default_shell
  environment.loginShellInit = ''
    # equivalent to .profile
    # https://search.nixos.org/options?show=environment.loginShellInit
  '';
};

:warning: Don’t forget to provide your custom p10k.zsh file (here, at the same level of the current file, place it wherever you want as long as the .source + programs.zsh.promptInit source point to it) :bulb:

Just wanted to share it here, in case someone else wants to achieve the same thing. If ever think there’s some improvements to be done in the above config, please let me know as I’m eager to learn :v:

Cheers!

Hello,

I used the command line from the powerlevel10k README file for Oh My Zsh, to get the powerlevel10k theme.

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"

I wrote that in my /etc/nixos/configuration.nix

  # Install zsh
  programs.zsh = {
    enable = true;
    enableBashCompletion = true;
    autosuggestions.enable = true;
    syntaxHighlighting.enable = true;
  };

  programs.zsh.ohMyZsh = {
    enable = true;
    plugins = [ "git" ];
    custom = "$HOME/.oh-my-zsh/custom/";
    theme = "powerlevel10k/powerlevel10k";
  };

It worked.

Source :
Appendix A. Configuration Options