Coder Social home page Coder Social logo

crash's Introduction

crash

A user-configurable login shell wrapper.

Crash is a super lightweight shim that executes the shells that are seperated by : in your SHELLS environment variable in order, halting execution if one exits sucessfully (with a 0 exit code).

If you don't have anything in your SHELLS environment variable or all the ones that did exist failed to launch, Crash will use the fallback shell that is configured at compile time (by default, this is bashInteractive from nixpkgs, however you can change this by overriding the fallbackShell call option).

Why?

  • To allow users to configure their own shells without superuser access (You can set the SHELLS variable to something like .config/shell and let users change that file).
  • To be able to hotswap between shells when using SSH. This is even more useful if multiple people who use the same user account on a machine use different shells. See the "Tips & Tricks" section.
  • To have a fallback shell in case your primary one, which is your login shell, breaks and you don't want to get locked out (especially useful when using new unstable shells like Nushell).

Installation

Simply add this repository to your inputs like so:

{
  inputs.crash.url = "github:RGBCube/crash";
}

And then you can set the package as your default user shell like so, in a NixOS module:

{
  outputs = { nixpkgs, crash }: {
    nixosConfigurations.myhostname = nixpkgs.lib.nixosSystem {
      modules = [
        ({ pkgs, lib, ... }: {
          nixpkgs.overlays = [ crash.overlays.default ];

          users.defaultUserShell = pkgs.crash;

          # Here we set our default shells. Nushell will be tried first, if that
          # exits with an error, fish will be launched instead. And if fish fails, the
          # fallback shell, which is pkgs.bashInteractive will get run.
          environment.sessionVariables.SHELLS = "${lib.getExe pkgs.nushell}:${lib.getExe pkgs.fish}";

          # This would also work, as Crash searches $PATH:
          #
          # environment.sessionVariables.SHELLS = "nu:fish";
          #
          # However, just setting an absolute path is pretty easy and better.
        })

        # Uncomment to make the fallback shell of crash pkgs.dash. Will require a recompilation!
        # {
        #   nixpkgs.overlays = [(final: prev: {
        #     crash = prev.crash.override { fallbackShell = final.dash };
        #   })];
        # }
      ];
    };
  }
}

Installation without Nix

First, you have to compile the program (requires Zig 0.12.0):

zig build --release=safe -Dcpu=baseline -Dfallback_shell=/bin/<yourshell>

After that, the binary should be in zig-out/bin/crash. You can copy it to /bin like so:

cp zig-out/bin/crash /bin/

After that, you will need to edit PAM settings to set the SHELLS environment variable early on in the boot process. Consult your distros documentation on how to do this, as it may vary.

Tips & Tricks

You can control the default shell / program that will get launched by SSH using Crash. All you need to do it make OpenSSH accept the SHELLS environment variable and set it when SSH'ing in. Here is a NixOS module that does that:

{
  services.openssh = {
    enable = true;
    settings.AcceptEnv = "SHELLS";
  };
}

And you can utilize it by adding this to your .ssh/config:

Host myvps
    # ..snip..
    SetEnv SHELLS=fish:nu:bash:dash

Then just SSH in like normal. This will launch you into fish, if that fails, into nu and so on...

Common Mistakes

Using ~ in the SHELLS variable

So, the way SHELLS is handled is like so:

  1. Split the variable into a list of shells using :.
  2. Searches PATH for the shell, invokes it if it can find it there.
  3. If it can't find the shell there, assumes the shell is a file. If it is an absolute path, it directly invokes the executable, if it isn'it, joins the path with the current working directory before executing.

Did you notice something? Yup, it doesn't expand the tilde (~)! But no worries, you don't need it anyway as the PWD of your login shell (in this case, Crash) is always your home directory. So if you wanted to do

SHELLS=~/.config/shell # Won't work!

You can do:

SHELLS=.config/shell # WILL work!

Instead and it will work perfectly fine.

Credits

License

Copyright (c) 2024-present RGBCube

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

crash's People

Contributors

rgbcube avatar

Stargazers

Tim Kersey avatar alaska avatar Javier Tia avatar MOZGIII avatar  avatar  avatar Saulius Krasuckas avatar bri avatar cz avatar Ron Wolf avatar Christopher Lord avatar Alex Jackson avatar Victor Leite avatar George Kontridze avatar Antonio De Lucreziis avatar Anthony Scalisi avatar Rosario Pulella avatar  avatar

Watchers

Rosario Pulella avatar  avatar

crash's Issues

Building without nix

Hi,

Is there a way to build it without nix ? I can't change the default fallbackshell and so it fails to build
thanks

Shell env setting

Hi,
it's me again ;)
I'm not sure on how to set in a correct way the shells env, can you elaborate a bit more on this ?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.