5 ms·
If you want portable shell-scripts that come with their dependencies bundled, Nix also has a solution: writeShellApplication[0] (and more simpler ones like writ
by skulk 2y ago
If you want portable shell-scripts that come with their dependencies bundled, Nix also has a solution: writeShellApplication[0] (and more simpler ones like writeShellScript).
writeShellApplication {
name = "show-nixos-org";
runtimeInputs = [ curl w3m ];
text = ''
curl -s 'https://nixos.org' | w3m -dump -T text/html
'';
}
writeShellApplication will call shellcheck[1] on your script and fail to build if there are any issues reported, which I think is the only sane default.
[0]: https://nixos.org/manual/nixpkgs/stable/#trivial-builder-writeShellApplication https://nixos.org/manual/nixpkgs/stable/#trivial-builder-wri...
[1]: https://www.shellcheck.net/ https://www.shellcheck.net/
- randall 2y agoomg i love nix so much.
- azeirah 2y agoNix is the best. If you're reading this and wondering how you can use this for yourself? You don't need nixos at all. You can install nix on any linux-like system, including on MacOS
- gchamonlive 2y agoI still haven't come around to using nix in my daily workflow. My concern is high entry bar, obscure errors and breaking changes, but also excessive use of storage either because that's how it works or because I won't know how to manage the store well. How's nix these days? How long would you expect someone with years of Linux management experience (bash, ansible, terraform, you name it, either onprem or on cloud) to get comfortable with nix? And what's would be the best roadmap to start introducing nix slowly in my workflow?
- epic9x 2y agoStart by using home-manager in your current environment. Once you can modularize your own config, start building other systems with it. It's a very deep rabbit hole, and starting off as a replacement for managing your own dotfile scripts and the like is a great way to try it out without having to replace whole systems.
- rounce 2y agoI'd say start even smaller by making a simple `flake.nix` with a devShell output within a project and use it to manage the project's dependencies, that way you're experiencing it within a fairly constrained opt-in environment. Nix is simple when you 'get it' but it can be quite overwhelming for someone new to it, Home-Manager is pretty big and has regions of complexity and while it might be a good candidate for daily driving Nix without running NixOS, IMO it's best to start really small.
- burgerrito 2y agoI like, Nix/NixOS, but the very lack of documentation makes me really angry sometimes, I'll admit
- johnvaluk 2y agoIs it possible to override shellcheck? It's a valuable tool that I use all the time, but it reports many false positives. It's not unusual for junior developers to introduce bugs in scripts because they blindly follow the output of shellcheck.
- nerflad 2y agoA comment before the problematic line can specify options to shellcheck, (e.g) # shellcheck disable=SC2086 which remain valid within that block. Of course, disabling the linter should be done with deliberation...
- samtheprogram 2y agoSo it compiles to a single executable that I can send to someone who isn’t on Nix? Because if I wanted a portable shell script, I’d just write shell and check if something is executable in my path. This just looks like Nix-only stuff that exists in an effort to be ultra declarative, and in order to use it you’d need to be on Nix.
- skulk 2y agoThere is nix-bundle (which I admittedly have never had a reason to use) https://github.com/nix-community/nix-bundle https://github.com/nix-community/nix-bundle
- rounce 2y agoWell you're still leaning on Nix to provide the dependencies. All `writeShellApplication` will do is prepend the `PATH` variable with the `bin` directories of the provided `runtimeInputs`, it still just spits out a bash script, not a binary that includes bash, the script, and the other dependencies. I reckon it's quite possible for someone to lean on Nix to implement producing an all-in-one binary though.
- skulk 2y agomentioned in another comment, there are ways to bundle nix derivations into standalone run-on-any-linux binaries: https://github.com/nix-community/nix-bundle https://github.com/nix-community/nix-bundle
- rounce 2y agoThanks! I suspected something already existed like this but I didn't find anything from 30s of web search.
- deleted 2y ago[deleted]
- abathur 2y agoIf your shell scripts/libraries are a little more complex, resholve can also help package them a little more reliably. (I'd say it's overkill for your example here, but it blocks on missing dependencies and can support tricky cases such as modular shell libraries that expect different implementations of the same command.)