News for hackers. Stuff that matters.
Re:On env Shebangs by Filligree on Friday April 15, 2022 @08:10PM (#31047574)
And what of other dependencies? Imagemagick, say? There's another way to achieve portability and exactness, although it requires some setup.

  #!/usr/bin/env nix-shell
  #!nix-shell -i bash -p imagemagick
Voila. A (mostly) self-contained shell script, dependencies auto-installed when needed.
Re:On env Shebangs by tuatoru on Friday April 15, 2022 @11:38PM (#31049211)
> another way to achieve portability

    $ nix-shell
    nix-shell: command not found
Re:On env Shebangs by abathur on Friday April 15, 2022 @11:19PM (#31049096)
I came here to say this, too :)

But, of course, it isn't a silver bullet...

1. You still have to have a sane PATH. A fair amount of the Nix install-related issues that get opened are PATH problems, and you can also run into problems with PATH in cron/launchd (as @tyingq noted).

2. You still have to know what the script depends on. This can get tricky beyond small scripts you wrote yourself. (I write a tool for discovering and ~linking/resolving external dependencies in Shell scripts, https://github.com/abathur/resholve. As I've been working on converting some of nixpkgs' existing Shell packages to use it, I almost always find dependencies the initial packager missed.)

Re:On env Shebangs by Filligree on Saturday April 16, 2022 @04:05AM (#31050563)
Re. 2-

If you invoke nix-shell with --pure, then it'll erase most user state — PATH included — prior to invoking the script. I don't find much use for this, but it would be useful to ensure nothing that isn't an explicit dependency gets used.

Re:On env Shebangs by abathur on Saturday April 16, 2022 @08:56AM (#31051934)
Yes, `--pure` helps (and `env -i` can also help) but that's not the hard part of finding them all. It only really helps when there's no conditional logic or no options that can lead to the execution of different programs.
Re:On env Shebangs by watersb on Friday April 15, 2022 @11:02PM (#31048971)
Interesting.

I've resisted the pull of the Nix side so far...

There's a similar package that auto-pulls dependencies for Swift scripts, but it requires you to explicitly state the external dependencies as comment annotations. The example given is:

    #!/usr/bin/swift sh
    
    import Foundation
    import PromiseKit  // @mxcl ~> 6.5
    
    firstly {
        after(.seconds(2))
    }.then {
        after(.milliseconds(500))
    }.done {
        print("notice: two and a half seconds elapsed")
        exit(0)
    }
    
    RunLoop.main.run()


https://github.com/mxcl/swift-sh