In a previous blog post I’ve described how to set up and customize Vim on NixOS. One of the open questions was, whether one can use the same configuration also on OS X. Turns out that one can. Later I also found a way to share my zsh configuration across my NixOS server and my MacBook.

Vim

In the previous post I’ve already described how one can set up and customize Vim on NixOS. As I’d like to have the Vim configuration on my NixOS server and my MacBook, my new goal was to replace my local .vimrc with the nix based Vim version I was already using on my NixOS server.

The nix configuration on the NixOS server is described in a vim.nix file, it basically looks like this:

with import <nixpkgs> {};
vim_configurable.customize {
 name = "vim";
 vimrcConfig.customRC = ''
   filetype plugin indent on
   set nocompatible
   syntax enable
   set background=dark
   colorscheme solarized
   set nostartofline
   " and some more stuff...
 '';
 vimrcConfig.vam.knownPlugins = pkgs.vimPlugins;
 vimrcConfig.vam.pluginDictionaries = [ { names = [ "Syntastic" "ctrlp" "colors-solarized" "supertab" "nerdtree" "vim-closetag" ]; } ];
}

This file describes my whole Vim configuration, including .vimrc and plugins. Turns out there are no further changes required to this vim.nix file to use it on OS X instead of just my NixOS server. I just had to run nix-env -f vim.nix -i vim on OS X. This installs Vim, configures my custom vimrc and installs the specified plugins.

Now I could remove my ~/.vimrc and uninstall the old vim, previously managed by homebrew.

Zsh and Oh My Zsh

Next I also wanted to manage my zsh and zsh configuration with nix.

Installing zsh is pretty easy: nix-env -i zsh. Then I added /Users/marc/.nix-profile/bin/zsh to /etc/shells. Now I could change the default shell to the nix managed shell by running chsh -s /Users/marc/.nix-profile/bin/zsh.

Like with Vim I’d also like to have a single .zshrc for OS X and my NixOS server. First I created a zsh-config.nix containing my zsh configuration:

with import <nixpkgs> {};

pkgs.writeText "zshrc" ''
export ZSH=${pkgs.oh-my-zsh}/share/oh-my-zsh/

if [[ "$OSTYPE" == "darwin"* ]]; then
    ZSH_THEME="theunraveler"
else
    ZSH_THEME="gentoo"
fi

plugins=(git)

if [[ "$OSTYPE" == "darwin"* ]]; then
    # Nix
    export NIX_PATH=nixpkgs=/Users/marc/nixpkgs
    source ~/.nix-profile/etc/profile.d/*.sh
fi

source $ZSH/oh-my-zsh.sh

export EDITOR=vim
bindkey -v
bindkey '^R' history-incremental-search-backward

alias gitc="git checkout"
alias gits="git status"
''

As you can see, this zsh-config.nix has a dependency to the oh-my-zsh package. The ${pkgs.oh-my-zsh} will be resolved to the path where oh-my-zsh is installed (somewhere in /nix/store). As you can see there are also some OS specific things, e.g. I use a different theme on OS X than on the NixOS server.

To install this zsh-config.nix, I just had to run nix-env -f zsh-config.nix -i. Then I just had to symlink this configuration to ~/.zshrc by running ln -s "$(nix-env -q zshrc --out-path --no-name)" "~/.zshrc".

On NixOS I use this zsh-config.nix like this:

programs.zsh.interactiveShellInit = ''
    cat << EOF > $HOME/.zshrc
        . ${import ./zsh-config.nix}
    EOF
  '';

It’s pretty awesome to have the same shell configuration on all your hosts, and nix makes it pretty easy to manage.

Thanks for reading :) Follow me on twitter if you’re interested in more of this stuff!