5 ms·
Nix-shell shebang: run any script with arbitrary packages, self-contained
- ranger_danger 3y agowhy would you not just use docker
- catlover76 3y agoI think it's the other way around; if someone was using Docker to isolate environment for individual scripts, it's like, why not use something more light-weight, such as this
- enriquto 3y agowhy would you? This seems much simpler.
- elbear 3y agothis option is more flexible. for example, you can get a Python interpreter with whatever packages you want installed (the same for any other interpreter or compiler of almost any language).
- dave4420 3y agoNo need to worry about bind mounts. Works for scripts that need to list/signal other processes.
- exe34 3y agoI can't afford a bigger ssd every few months
- rnhmjoj 3y agoAnother fun thing you could do (I'm not saying it's a good idea) with nix-shell is this: install command-not-found and set this function in your shell command_not_found_handle() { eval "$(command-not-found $1 2>&1 | sed -n 3p) --run '$@'" } Now you have any command available, no matter if installed or not.
- lambdaba 3y agoNice. There's also "comma" (https://github.com/nix-community/comma https://github.com/nix-community/comma) that allows running any executable from nixpkgs by prepending ",".
- EnergyAmy 3y agoThanks for posting that, that looks super useful. A lot of my nix-shell usage is one-off commands like this
- exe34 3y agoThis is disgusting, I love it!
- gipp 3y agoI actually did do this on my personal machine. Works pretty well tbh. Of course you have to worry about accidentally starting some install (or, just evaluation and fetching I guess) process every time you have a typo in your command, but that'll be obvious immediately and you just Ctrl c out of it. Still waiting for it to bite me somehow but hasn't yet
- alchemist1e9 3y agoThis is extremely useful but has one downside people should consider. It needs to evaluate the full nix expressions on each evaluation which is expensive so it’s most appropriate for launching long running processes and should be avoided for anything called often and expected to be quickly executed.
- elbear 3y agoI would say that this is better for experiments rather than anything you would use in production. Regarding your observation, the bottom of the wiki page references a project that addresses the issue with the slowness of nix-shell: https://github.com/xzfc/cached-nix-shell https://github.com/xzfc/cached-nix-shell
- anotherhue 3y agoFlakes have better caching so offer a best-of-both-worlds solution here.
- soraminazuki 3y agoIs probably best to use the "result" symlink for that kind of use case.
- Arcuru 3y agoI used to use this, but it won't work if you want to share the script with someone who doesn't use Nix. My preferred option recently has been to detect the presence of Nix in the shell script, and if so just rerun the script with the deps installed. That way it still works for anybody not using Nix, but also has pinned versioning for Nix users. I haven't used this for anything complex, so it may need a little bit of tweaking. [0] - https://jackson.dev/post/a-portable-nix-shell-shebang/ https://jackson.dev/post/a-portable-nix-shell-shebang/
- plagiarist 3y agoAnother similar idea that people here might be interested in is pkgx: pkgx.sh. From what I understand, pkgx is less of a script setup and more of a quick semi-consistent environment setup without the overhead or root access of a tool like docker.
- linsomniac 3y agoI just started dabbling with Nix a couple weeks ago, and I'm loving the declarative style configuraation. Switch to unstable version, switch back, try a new windowing environment and revert when I don't love it. You get grub menu items that let you boot into a rootfs of older configurations. Pretty slick!
- dang 3y agoRelated ongoing thread: Scriptisto: "Shebang interpreter" that enables writing scripts in compiled langs - https://news.ycombinator.com/item?id=39272890 https://news.ycombinator.com/item?id=39272890 - Feb 2024 (14 comments) There's nothing wrong with the current submission in principle, but it's best not to post follow-up links [1] as top-level submissions—rather, they should be comments in the original thread, as you did here: https://news.ycombinator.com/item?id=39292955 https://news.ycombinator.com/item?id=39292955. The reason is that frontpage slots are the scarcest resource HN has [2], so we want to keep them as uncorrelated with each other as possible. When they aren't, readers soon start to complain about the repetition [3]. Not a criticism! Just want to get the word out. [1] https://hn.algolia.com/?dateRange=all&page=0&prefix=true&query=follow-up%20downweight&sort=byDate&type=comment https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que... [2] https://hn.algolia.com/?dateRange=all&page=0&prefix=true&query=by%3Adang%20scarce%20front&sort=byDate&type=comment https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que... [3] https://hn.algolia.com/?dateRange=all&page=0&prefix=false&sort=byDate&type=comment&query=curiosity%20repetition%20by:dang https://hn.algolia.com/?dateRange=all&page=0&prefix=false&so...
- lambdaba 3y agoOh, sorry, I had no idea, my impression was I often see correlated stories but I didn't know it's something to be avoided.
- soraminazuki 3y ago> As of Nix 2.19.0 you can also use the new CLI nix shell and flakes to define shebangs. I wasn't aware of this, it's nice to know.