[HN Gopher] Nix-shell shebang: run any script with arbitrary pac...
___________________________________________________________________
Nix-shell shebang: run any script with arbitrary packages, self-
contained
Author : lambdaba
Score : 35 points
Date : 2024-02-07 19:23 UTC (3 hours ago)
(HTM) web link (nixos.wiki)
(TXT) w3m dump (nixos.wiki)
| ranger_danger wrote:
| why would you not just use docker
| catlover76 wrote:
| I 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 wrote:
| why would you? This seems much simpler.
| elbear wrote:
| this 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 wrote:
| No need to worry about bind mounts.
|
| Works for scripts that need to list/signal other processes.
| exe34 wrote:
| I can't afford a bigger ssd every few months
| alchemist1e9 wrote:
| This 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 wrote:
| I 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
| anotherhue wrote:
| Flakes have better caching so offer a best-of-both-worlds
| solution here.
| rnhmjoj wrote:
| Another 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 wrote:
| Nice. There's also "comma" (https://github.com/nix-
| community/comma) that allows running any executable from
| nixpkgs by prepending ",".
| EnergyAmy wrote:
| Thanks for posting that, that looks super useful. A lot of my
| nix-shell usage is one-off commands like this
| exe34 wrote:
| This is disgusting, I love it!
| gipp wrote:
| I 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
| Arcuru wrote:
| I 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/
| plagiarist wrote:
| Another 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 wrote:
| I 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 wrote:
| Related ongoing thread:
|
| _Scriptisto: "Shebang interpreter" that enables writing scripts
| in compiled langs_ -
| 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.
|
| 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&que...
|
| [2]
| https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que...
|
| [3]
| https://hn.algolia.com/?dateRange=all&page=0&prefix=false&so...
| lambdaba wrote:
| Oh, sorry, I had no idea, my impression was I often see
| correlated stories but I didn't know it's something to be
| avoided.
___________________________________________________________________
(page generated 2024-02-07 23:02 UTC)