2 ms·
One issue I've had with personal shell scripts is that the dependencies are often implicit, or command line arguments change, leading to (sometimes silent and c
by siraben 6y ago
One issue I've had with personal shell scripts is that the dependencies are often implicit, or command line arguments change, leading to (sometimes silent and critical) behavior. With Nix you can write shell scripts with a nix-shell shebang[0], specify the dependencies and the rest of the script will run with the dependencies satisfied, for instance, this will execute GNU Hello regardless of whether it is already in the path or not
#! /usr/bin/env nix-shell
#! nix-shell --pure -i bash -p "hello"
hello
a more realistic example is[1] which generates[2], and if necessary, the Nixpkgs revision can be pinned to fix the dependencies in time. This approach also extends across languages and some of my scripts are written in Haskell which is interpreted!
The upshot of all this is that you can write reproducible shell scripts as if you had every package in Nixpkgs available to you, and share it easily with others.
[0] https://nix.dev/tutorials/ad-hoc-developer-environments.html#reproducible-executables https://nix.dev/tutorials/ad-hoc-developer-environments.html...
[1] https://edef.eu/~qyliss/nix/lib/gen.sh https://edef.eu/~qyliss/nix/lib/gen.sh
[2] https://edef.eu/~qyliss/nix/lib/ https://edef.eu/~qyliss/nix/lib/
- ianthehenry 6y agoI learned about this very recently[1] and it seems like an amazing use case that I wish got more play in the marketing of Nix. I have a lot of dumb "productivity" scripts that I wrote at work (Linux) that I can't run at home (macOS) because of differences in the behavior of `find` or `date` whatever. I plan on porting them to use `nixpkgs.findutils` etc. and never having to worry about this again. [1]: https://ianthehenry.com/posts/how-to-learn-nix/command-reference/ https://ianthehenry.com/posts/how-to-learn-nix/command-refer...
- rchoudhury 6y agoOh my goodness, your blog series looks incredible. I'm in sort of the same boat as you--I've been barely treading water with NixOS for the last several years and have recently resolved to learn how it works well enough to get all the benefits. Thanks for putting this together; looking forward to diving into it!
- siraben 6y agoNeat! In your post you mention that it takes a few seconds until you get to the prompt (and the waiting time is similar for the nix-shell shebang scripts), but recently someone made nix-script[0] to cache these scripts, resulting in a runtime on the order of dozens of milliseconds. Also, thanks for being dedicated enough to read the entire manual and give your impressions! If you spot typos, things that could be clearer, etc. please let us know on Freenode #nixos-dev or the Nixpkgs issue tracker! Do you also plan on covering flakes? Flakes are currently gaining adopting in the Nix ecosystem and are pretty exciting, since they resolve issues like ad-hoc project structuring, and since evaluation will now be pure, results can be cached aggressively and nix-shell will be much much faster. [0] https://bytes.zone/posts/nix-script/ https://bytes.zone/posts/nix-script/
- ianthehenry 6y agonix-script looks awesome -- I saw that post on HN right after I started learning Nix, and it's sitting in my notes as something to go back to later in the series (I'm trying not to jump around too much until I'm done reading the official docs, to try not to taint myself with too much knowledge). I used to work somewhere that had built something like this specifically for OCaml scripts, so it's very cool to see a generic solution. I have encountered flakes in passing -- in the as yet unpublished part 20 I built Nix from source to try to fix a bug I found, and encountered the concept briefly. This series is on my reading list once I've read the Nixpkgs manual and Nix Pills: https://www.tweag.io/blog/2020-05-25-flakes/ https://www.tweag.io/blog/2020-05-25-flakes/ (Not sure if there's a better place to start.) I plan on opening some doc PRs once I'm done reading through everything -- I found a few little things that I feel like I can fix myself. For higher level issues I'll try to open a discussion (e.g. I had a lot of trouble going through section 14.3 and 15.4 of the Nix manual). Anyway. Kind of off-topic for this thread, but here we are.