├── .github └── issue_template.md ├── .gitignore ├── LICENSE ├── README.MD ├── nix-shell.plugin.zsh └── scripts └── buildShellShim /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | ## Issue description 8 | 9 | Did you install the plugin according to the installation guide? If not, does the issue persist if you do install it that way? 10 | 11 | ### Steps to reproduce 12 | 13 | 14 | 15 | ## Technical details 16 | 17 | Please run the following commands and paste the results: 18 | 1. `command nix-shell -p nix-info --run "nix-info -m"` 19 | 2. `zsh --version` 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | *.swp 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018, Philipp Dargel 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of Spencer Whitt nor the names of other 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | 2 | # zsh in nix-shell 3 | 4 | This [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh/) plugin lets you use zsh as the default shell in a `nix-shell` environment. It's recommended to use this in conjunction with [nix-zsh-completions](https://github.com/spwhitt/nix-zsh-completions). 5 | 6 | # Motivation 7 | 8 | In theory all you need to do to use `zsh` in `nix-shell` is to set `NIX_BUILD_SHELL` to `zsh`. Unfortunatly nix assumes that the `NIX_BUILD_SHELL` is a bash variant and passes bash specific arguments to the shell. To fix this, we need a shim that translates these arguments. 9 | 10 | The information that `nix-shell` exposes to the environment is also pretty sparse. It would be nice to know what derivations are included in the current environment for example to display them in the shells prompt. 11 | 12 | # Installation 13 | 14 | ## Oh-My-ZSH 15 | 16 | Clone this repository into your plugins directory 17 | 18 | ```sh 19 | git clone https://github.com/chisui/zsh-nix-shell.git $ZSH_CUSTOM/plugins/nix-shell 20 | ``` 21 | 22 | Then add `nix-shell` to the plugins list in `~/.zshrc`. 23 | 24 | ### NixOS 25 | 26 | If you have installed `zsh` using `nix` the plugins directory is readonly since it's inside of the nix store. 27 | To get around this you can override the [`ZSH_CUSTOM` directory](https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#using-another-customization-directory). 28 | Simply create a writable directory inside of your home directory (e.g. `$HOME/.config/oh-my-zsh`) and set `ZSH_CUSTOM` to that path inside of your `.zshrc` file. 29 | 30 | ```sh 31 | ZSH_CUSTOM=$HOME/.config/oh-my-zsh 32 | ``` 33 | 34 | After that simply install for `oh-my-zsh` as normal. 35 | 36 | ## Plain ZSH 37 | 38 | Clone this repository and add the following to your `~/.zshrc`. 39 | 40 | ```sh 41 | source /path/to/zsh-nix-shell/nix-shell.plugin.zsh 42 | ``` 43 | 44 | ## home-manager 45 | 46 | Add this repository to the [`plugins` list of your configurations](https://rycee.gitlab.io/home-manager/options.html#opt-programs.zsh.plugins): 47 | 48 | ```nix 49 | { 50 | programs.zsh = { 51 | enable = true; 52 | enableCompletion = true; 53 | plugins = [ 54 | { 55 | name = "zsh-nix-shell"; 56 | file = "nix-shell.plugin.zsh"; 57 | src = pkgs.fetchFromGitHub { 58 | owner = "chisui"; 59 | repo = "zsh-nix-shell"; 60 | rev = "v0.8.0"; 61 | sha256 = "1lzrn0n4fxfcgg65v0qhnj7wnybybqzs4adz7xsrkgmcsr0ii8b7"; 62 | }; 63 | } 64 | ]; 65 | }; 66 | } 67 | ``` 68 | 69 | ## Plugin managers 70 | 71 | It should be possible to install this plugin through most `zsh` plugin managers. If the one of your choice is not supported, feel free to open an issue or even better create a pull request. 72 | 73 | ## MacOS 74 | 75 | On MacOS you have to have a `bash` with version `4` or greater. [See Issue 14](https://github.com/chisui/zsh-nix-shell/issues/14) 76 | 77 | # Usage 78 | 79 | Use `nix-shell` as you did before. 80 | 81 | Commands run with `--run` or `--command` argument are executed in `nix-shell`s default shell. In the case of `--command` you are put into a `zsh` shell afterwards. 82 | 83 | ## `--pure` 84 | 85 | If you use the `--pure` flag the interactive shell will be the default shell. 86 | 87 | ## Environment info 88 | 89 | If you are inside a `nix-shell` environment `IN_NIX_SHELL` will be set. The value will be `impure` or `pure` if you specified `--pure`. 90 | 91 | The `packages` argument is passed through as `NIX_SHELL_PACKAGES` to the shell. 92 | 93 | If this Variable is empty `nix-shell` was called for a specific nix expression which is stored in the `name` environment variable. 94 | 95 | These variables can now be used inside a theme to customize the prompt. Take a look at this [variant of the agnoster theme](https://gist.github.com/chisui/0d12bd51a5fd8e6bb52e6e6a43d31d5e#file-agnoster-nix-zsh-theme) for an example of how this might look. 96 | 97 | ![example prompt](https://gist.githubusercontent.com/chisui/0d12bd51a5fd8e6bb52e6e6a43d31d5e/raw/8787d8e234e895b2c74194936290a0da9be539ff/example.png) 98 | 99 | ![example prompt for projects](https://gist.githubusercontent.com/chisui/0d12bd51a5fd8e6bb52e6e6a43d31d5e/raw/ea75cad507e2899b9b6d6ce423330641911110d8/exampleProject.png) 100 | 101 | # Limitations 102 | 103 | ## Shell hooks 104 | 105 | Shell hooks are supported in general. Since they are executed inside of `bash` before the `zsh` shell is spawned they aren't executed in the same environment. This means that things like aliases won't work. 106 | 107 | ## Zsh dotfiles 108 | 109 | The normal zsh dotfiles are sourced after the nix-shell is opened. This means that you have to take into account that these files may override variables set by nix-shell. 110 | 111 | # Contributing 112 | 113 | Please do. Pull requests welcome. 114 | -------------------------------------------------------------------------------- /nix-shell.plugin.zsh: -------------------------------------------------------------------------------- 1 | 2 | NIX_SHELL_PLUGIN_DIR=${0:a:h} 3 | 4 | which bash > /dev/null 2>&1 5 | if [ "$?" -ne "0" ]; then 6 | echo 7 | echo " WARNING: bash is not installed." 8 | echo " for zsh-nix-shell to work bash has to be in PATH" 9 | echo 10 | fi 11 | 12 | 13 | # extracts packages argument from args and passes them in $NIX_SHELL_PACKAGES variable. 14 | function nix-shell() { 15 | local -a ARGS; ARGS=("$@") 16 | local NIX_SHELL_PACKAGES="${NIX_SHELL_PACKAGES}" 17 | 18 | # extract -p|--packages argument into NIX_SHELL_PACKAGES 19 | local IN_PACKAGES=0 20 | local PURE=0 21 | while [[ ${#ARGS[@]} -gt 0 ]] 22 | do 23 | key=${ARGS[1]} 24 | # enter "--packages packages..." mode 25 | if [[ $key = "-p" || $key = "--packages" ]] 26 | then 27 | IN_PACKAGES=1 28 | NIX_SHELL_PACKAGES+=${NIX_SHELL_PACKAGES:+ }${ARGS[2]} 29 | ARGS=("${ARGS[@]:1}") 30 | 31 | # skip "--arg name value" argument 32 | elif [[ $key = "--arg" ]] 33 | then 34 | IN_PACKAGES=0 35 | ARGS=("${ARGS[@]:2}") 36 | 37 | elif [[ $key = "--pure" ]] 38 | then 39 | PURE=1 40 | 41 | # skip all other unary arguments 42 | elif [[ $key == "-"* ]] 43 | then 44 | IN_PACKAGES=0 45 | ARGS=("${ARGS[@]:1}") 46 | 47 | # If we don't have any argument prefix we are either in package mode 48 | # or we have encountered the path argument 49 | elif [[ $IN_PACKAGES = 1 ]] 50 | then 51 | NIX_SHELL_PACKAGES+=" $key" 52 | fi 53 | ARGS=("${ARGS[@]:1}") 54 | done 55 | 56 | # call real nix shell 57 | if [[ $PURE = 1 ]] 58 | then 59 | # if you use --pure you get bash 60 | command nix-shell "$@" 61 | else 62 | NIX_EXECUTING_SHELL=$(readlink -f /proc/$$/exe) 63 | if [[ -z "$NIX_EXECUTING_SHELL" ]] && command -v lsof &> /dev/null 64 | then 65 | NIX_EXECUTING_SHELL=$(lsof -p $$ | awk '$4=="txt" {print $9}' | head -n 1) 66 | fi 67 | if [[ -z "$NIX_EXECUTING_SHELL" ]] && command -v zsh &> /dev/null 68 | then 69 | NIX_EXECUTING_SHELL="zsh" 70 | fi 71 | if [[ -z "$NIX_EXECUTING_SHELL" ]] 72 | then 73 | echo "could not determine executing shell. please install `lsof` or `zsh` directly to your PATH" 74 | echo "If this error persists create an issue at https://github.com/chisui/zsh-nix-shell/issues/new/choose" 75 | return 1 76 | fi 77 | NIX_SHELL_PACKAGES="$NIX_SHELL_PACKAGES" \ 78 | NIX_BUILD_SHELL="$NIX_SHELL_PLUGIN_DIR/scripts/buildShellShim" \ 79 | NIX_EXECUTING_SHELL=$NIX_EXECUTING_SHELL \ 80 | command nix-shell "$@" 81 | fi 82 | } 83 | -------------------------------------------------------------------------------- /scripts/buildShellShim: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | 3 | if [ "$1" = "--rcfile" ]; then 4 | # This means the shell should stay open after executing. So we remove the last line which contains 'exit' 5 | shift 6 | tmp="$(cat $1)" 7 | echo ${tmp%exit} > $1 8 | cat >> $1 <