4 ms·
And what of other dependencies? Imagemagick, say? There's another way to achieve portability and exactness, although it requires some setup. #!/usr/bin/env n
by Filligree 5y ago
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.
- watersb 5y agoInteresting. 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 https://github.com/mxcl/swift-sh
- abathur 5y agoI 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 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.)
- Filligree 5y agoRe. 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.
- abathur 5y agoYes, `--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.
- tuatoru 5y ago> another way to achieve portability $ nix-shell nix-shell: command not found