user11810894
user11810894

Reputation:

add bash so that it's recognized by /usr/bin/env

When I do which bash on NixOS, I get:

/run/current-system/sw/bin/bash

but I get this error:

env: can't execute 'bash': No such file or directory

Which is surely because of the use of a hashbang:

#!/usr/bin/env bash

I tried this:

export PATH="/run/current-system/sw/bin:$PATH"

but that didn't help. Anyone know how I can get env to recognize the path to bash?

Upvotes: 2

Views: 1910

Answers (1)

nrdxp
nrdxp

Reputation: 812

Well first of all, your shebang should actually work. I have written a bunch of ad hoc scripts in NixOS using this exact shebang with no issue. In fact this use case is the reason the devs put env in /usr/bin in the first place (no other binaries live there).

The fact that it's not working is strange, perhaps due to something in your system packages. Have you tried running /usr/bin/env bash from cmdline to see what happens?

As a side note, your PATH should already contain /run/current-system/sw/bin by default, if it doesn't you've got a bigger problem on your hands. This is where symlinks to all the software defined in your configuration.nix get installed.

Workaround

There is however a workaround to explicitly declare the full path directly to the bash binary if you need this to work right now and don't have time to figure out what the root cause is. You could wrap your script in a nix expression. Here is a simple shell.nix that gives you a symlink to a script residing in the nix-store that has the full path to bash in its shebang:

{ pkgs ? import <nixpkgs> {} }:
let
  script = pkgs.writeScript "myscript.sh" script_;

  script_ = ''
    #!${pkgs.bash}/bin/bash
    echo hello world
  '';
in
pkgs.mkShell {
  shellHook = ''
    ln -s ${script} myscript
  '';
}

This isn't the best solution, but it'll get you up and running right away. You could also do something very similar in your configuration.nix.

I would have recommended nix-shell shebangs as a nicer solution, but since your original shebang isn't working, I figured this might be broken as well.

Upvotes: 2

Related Questions