Skip to content

Commit

Permalink
feat: add nushell support (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill authored Aug 1, 2023
1 parent ea21a6b commit f11e015
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ if command -v nix-your-shell > /dev/null; then
fi
```

### Nushell

> [!IMPORTANT]
> Nushell version >=0.83.0 is required
> [!NOTE]
> Nushell requires sourced configuration files to exist before `nu` is started.
Add to your `~/.config/nushell/config.nu`:

```nu
source nix-your-shell.nu
```

To generate `nix-your-shell.nu` file:

Either manually generate it:

```sh
nix-your-shell nu | save nix-your-shell.nu
```

Or populate its content through flakes and home-manager:

```nix
{ config, pkgs, ... }: {
home.file."${config.xdg.configHome}/nushell/nix-your-shell.nu".source = pkgs.nix-your-shell.generate-config "nu";
}
```

## Installation

You can either install `nix-your-shell` from this repository or from `nixpkgs`.
Expand Down
20 changes: 20 additions & 0 deletions data/env.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# If you see this output, you probably forgot to pipe it into `source`:
# nix-your-shell nu | save nix-your-shell.nu

def call-wrapper (command: string, args: list<string>) {
if not (which nix-your-shell | is-empty) {
let args = ["--"] ++ $args

run-external nix-your-shell $command $args
} else {
run-external $command $args
}
}

extern-wrapped nix-shell (...args) {
call-wrapper nix-shell $args
}

extern-wrapped nix (...args) {
call-wrapper nix $args
}
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
cargo fmt --check && echo "\`cargo fmt\` is OK"
cargo clippy -- --deny warnings && echo "\`cargo clippy\` is OK"
'';

passthru.generate-config = shell: final.runCommand "nix-your-shell-config" { } ''
${final.nix-your-shell}/bin/nix-your-shell ${shell} >> $out
'';
};
}
);
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ fn main() -> eyre::Result<()> {
include_str!("../data/env.fish")
}

ShellKind::Nushell => {
include_str!("../data/env.nu")
}

ShellKind::Other(shell) => {
return Err(eyre!(
"I don't know how to generate a shell environment for `{shell}`"
))
.note("Supported shells are: `zsh`, `fish`, and `bash`")
.note("Supported shells are: `zsh`, `fish`, `nushell` and `bash`")
}
}
.to_owned();
Expand All @@ -110,8 +114,7 @@ fn main() -> eyre::Result<()> {
let current_exe =
current_exe().wrap_err("Unable to determine absolute path of `nix-your-shell`")?;
if args.absolute || !executable_is_on_path(&current_exe)? {
shell_code =
shell_code.replace("nix-your-shell", &format!("{current_exe} --absolute"));
shell_code = shell_code.replace("nix-your-shell", current_exe.as_str());
}

println!("{shell_code}");
Expand Down
7 changes: 7 additions & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub enum ShellKind {
/// <https://www.gnu.org/software/bash/>
Bash,

/// The `nu` shell
/// <https://www.nushell.sh/>
Nushell,

/// A different shell.
Other(String),
}
Expand All @@ -30,6 +34,7 @@ impl Display for ShellKind {
ShellKind::Zsh => write!(f, "zsh"),
ShellKind::Fish => write!(f, "fish"),
ShellKind::Bash => write!(f, "bash"),
ShellKind::Nushell => write!(f, "nu"),
ShellKind::Other(shell) => write!(f, "{shell}"),
}
}
Expand Down Expand Up @@ -57,6 +62,8 @@ impl Shell {
ShellKind::Fish
} else if file_name.starts_with("bash") {
ShellKind::Bash
} else if file_name.starts_with("nu") {
ShellKind::Nushell
} else {
ShellKind::Other(file_name.to_string())
};
Expand Down

0 comments on commit f11e015

Please sign in to comment.