Nix develop mit Zsh statt Bash nutzen: So klappt der Shell-Wechsel

Using Zsh Instead of Bash with Nix develop: How to Switch Shells

If you use the Nix package manager for development environments, you might run into a surprise right at the start: despite having Zsh configured system-wide, running nix develop launches Bash by default. This guide shows you how to use your preferred shell inside Nix environments.

Step 1: Test the launch via command-line argument

To start your own shell directly with Nix, pass the $SHELL environment variable to Nix using the -c flag:

nix develop -c $SHELL

This command launches the user shell defined in $SHELL right inside the development environment.

Step 2: Configure the shell in your project via flake.nix

If you maintain a project and want nix develop to launch your shell automatically, you can modify your repository’s flake.nix file.

Path: ./flake.nix

{
  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in {
      devShells.${system}.default = pkgs.mkShell {
        shellHook = ''
          $SHELL
        '';
      };
    };
}

Adding $SHELL to the shellHook of pkgs.mkShell ensures your preferred shell is executed when the dev environment starts.

Potential issues and quirks

  • Nested shells: Launching $SHELL inside shellHook may result in nested sessions, requiring you to press Ctrl+D twice when exiting to return to your previous terminal session.

Reverting changes

If you modified a local flake.nix, simply remove the $SHELL call from the shellHook block.

Conclusion

While Nix defaults to Bash for development environments, you can easily adapt it to your workflow using the -c flag or a shellHook. Alternatively, tools like direnv can be used, as they integrate environment variables without altering your current shell once cached.

See also  Unraid Hardware Requirements

Sources: Forum: NixOS – Forum, discourse.nixos.org

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top