Added nix-shell plugin to oh-my-zsh

This commit is contained in:
KoenDR06 2024-07-21 20:45:22 +02:00
parent 70e0c63886
commit 7712beeeb4
5 changed files with 249 additions and 0 deletions

View file

@ -26,6 +26,7 @@
"python"
"git-auto-fetch"
"wd"
"nix-shell"
];
custom = "/home/horseman/nix-config/pkgs/zsh/";
theme = "jonathan";

View file

@ -0,0 +1,27 @@
Copyright (c) 2018, Philipp Dargel
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Spencer Whitt nor the names of other
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -0,0 +1,113 @@
# zsh in nix-shell
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).
# Motivation
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.
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.
# Installation
## Oh-My-ZSH
Clone this repository into your plugins directory
```sh
git clone https://github.com/chisui/zsh-nix-shell.git $ZSH_CUSTOM/plugins/nix-shell
```
Then add `nix-shell` to the plugins list in `~/.zshrc`.
### NixOS
If you have installed `zsh` using `nix` the plugins directory is readonly since it's inside of the nix store.
To get around this you can override the [`ZSH_CUSTOM` directory](https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#using-another-customization-directory).
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.
```sh
ZSH_CUSTOM=$HOME/.config/oh-my-zsh
```
After that simply install for `oh-my-zsh` as normal.
## Plain ZSH
Clone this repository and add the following to your `~/.zshrc`.
```sh
source /path/to/zsh-nix-shell/nix-shell.plugin.zsh
```
## home-manager
Add this repository to the [`plugins` list of your configurations](https://rycee.gitlab.io/home-manager/options.html#opt-programs.zsh.plugins):
```nix
{
programs.zsh = {
enable = true;
enableCompletion = true;
plugins = [
{
name = "zsh-nix-shell";
file = "nix-shell.plugin.zsh";
src = pkgs.fetchFromGitHub {
owner = "chisui";
repo = "zsh-nix-shell";
rev = "v0.8.0";
sha256 = "1lzrn0n4fxfcgg65v0qhnj7wnybybqzs4adz7xsrkgmcsr0ii8b7";
};
}
];
};
}
```
## Plugin managers
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.
## MacOS
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)
# Usage
Use `nix-shell` as you did before.
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.
## `--pure`
If you use the `--pure` flag the interactive shell will be the default shell.
## Environment info
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`.
The `packages` argument is passed through as `NIX_SHELL_PACKAGES` to the shell.
If this Variable is empty `nix-shell` was called for a specific nix expression which is stored in the `name` environment variable.
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.
![example prompt](https://gist.githubusercontent.com/chisui/0d12bd51a5fd8e6bb52e6e6a43d31d5e/raw/8787d8e234e895b2c74194936290a0da9be539ff/example.png)
![example prompt for projects](https://gist.githubusercontent.com/chisui/0d12bd51a5fd8e6bb52e6e6a43d31d5e/raw/ea75cad507e2899b9b6d6ce423330641911110d8/exampleProject.png)
# Limitations
## Shell hooks
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.
## Zsh dotfiles
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.
# Contributing
Please do. Pull requests welcome.

View file

@ -0,0 +1,82 @@
NIX_SHELL_PLUGIN_DIR=${0:a:h}
which bash > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo
echo " WARNING: bash is not installed."
echo " for zsh-nix-shell to work bash has to be in PATH"
echo
fi
# extracts packages argument from args and passes them in $NIX_SHELL_PACKAGES variable.
function nix-shell() {
local -a ARGS; ARGS=("$@")
local NIX_SHELL_PACKAGES="${NIX_SHELL_PACKAGES}"
# extract -p|--packages argument into NIX_SHELL_PACKAGES
local IN_PACKAGES=0
local PURE=0
while [[ ${#ARGS[@]} -gt 0 ]]
do
key=${ARGS[1]}
# enter "--packages packages..." mode
if [[ $key = "-p" || $key = "--packages" ]]
then
IN_PACKAGES=1
NIX_SHELL_PACKAGES+=${NIX_SHELL_PACKAGES:+ }${ARGS[2]}
ARGS=("${ARGS[@]:1}")
# skip "--arg name value" argument
elif [[ $key = "--arg" ]]
then
IN_PACKAGES=0
ARGS=("${ARGS[@]:2}")
elif [[ $key = "--pure" ]]
then
PURE=1
# skip all other unary arguments
elif [[ $key == "-"* ]]
then
IN_PACKAGES=0
ARGS=("${ARGS[@]:1}")
# If we don't have any argument prefix we are either in package mode
# or we have encountered the path argument
elif [[ $IN_PACKAGES = 1 ]]
then
NIX_SHELL_PACKAGES+=" $key"
fi
ARGS=("${ARGS[@]:1}")
done
# call real nix shell
if [[ $PURE = 1 ]]
then
# if you use --pure you get bash
command nix-shell "$@"
else
NIX_EXECUTING_SHELL=$(readlink -f /proc/$$/exe)
if [[ -z "$NIX_EXECUTING_SHELL" ]] && command -v lsof &> /dev/null
then
NIX_EXECUTING_SHELL=$(lsof -p $$ | awk '$4=="txt" {print $9}' | head -n 1)
fi
if [[ -z "$NIX_EXECUTING_SHELL" ]] && command -v zsh &> /dev/null
then
NIX_EXECUTING_SHELL="zsh"
fi
if [[ -z "$NIX_EXECUTING_SHELL" ]]
then
echo "could not determine executing shell. please install `lsof` or `zsh` directly to your PATH"
echo "If this error persists create an issue at https://github.com/chisui/zsh-nix-shell/issues/new/choose"
return 1
fi
NIX_SHELL_PACKAGES="$NIX_SHELL_PACKAGES" \
NIX_BUILD_SHELL="$NIX_SHELL_PLUGIN_DIR/scripts/buildShellShim" \
NIX_EXECUTING_SHELL=$NIX_EXECUTING_SHELL \
command nix-shell "$@"
fi
}

View file

@ -0,0 +1,26 @@
#! /usr/bin/env sh
if [ "$1" = "--rcfile" ]; then
# This means the shell should stay open after executing. So we remove the last line which contains 'exit'
shift
tmp="$(cat $1)"
echo ${tmp%exit} > $1
cat >> $1 <<EOF
SHELL=$NIX_EXECUTING_SHELL
unset NIX_BUILD_SHELL
FPATH=\$(echo 'echo \$FPATH' | $NIX_EXECUTING_SHELL --stdin)
for path in \${PATH//:/ }; do
if [[ \$path = /nix/store/* ]]; then
path=\${path/%bin/share\/zsh\/site-functions}
if [ -d "\$path" ]; then
FPATH="\$path:\$FPATH"
fi
fi
done
FPATH=\$FPATH $NIX_EXECUTING_SHELL
EOF
/usr/bin/env bash $1
else
/usr/bin/env bash "$@"
fi