21 comments

[ 6.3 ms ] story [ 64.5 ms ] thread
why would you not just use docker
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
why would you? This seems much simpler.
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).
No need to worry about bind mounts.

Works for scripts that need to list/signal other processes.

I can't afford a bigger ssd every few months
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.
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

Flakes have better caching so offer a best-of-both-worlds solution here.
Is probably best to use the "result" symlink for that kind of use case.
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.
This is disgusting, I love it!
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
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/

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.
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!
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...

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.
> 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.