There's very little in the way of "here's a common task that new users want to do and here's the blessed way to do it." Even something that should be Nix's MAJOR selling point, like "create a single repo that configures a bunch of machines, including some NixOS and some other OS" doesn't have a recommended way to do it.
Another challenge with the docs is the fact that Nix is
* An OS * A package manager * A way to manage dev environments
A lot of times I find someone who solves my problem but they solve it in the context of building a dev environment and I want to do it in the context of setting up an OS package. It means that even with all the good tutorials and blog posts out there, you might still have a bunch of work to do to get things to work how you want. This translates into tons & tons of opportunities for users to give up.
Also, for some reason, so many examples are done in the imperative manner, and it's not always super clear how to translate them to a declarative setup. Again, just defeating the strengths of Nix and clouding the benefits. If a new user sees a bunch of examples that involve starting nix-shell and installing some dependencies it's both hard to understand the benefits and hard to translate that to a declarative setup.
Yes you can. Here's a Nix derivation which makes a Debian chroot and runs 'apt-get update; apt-get install -y chromium' https://github.com/Warbo/warbo-utilities/blob/7087c3b46a97f3...
Here a Nix derivation for a helper script that runs the above chromium: https://github.com/Warbo/warbo-utilities/blob/7087c3b46a97f3...
On a similar note, here's a Nix derivation for Mozilla's Firefox binary (in an FHS env), along with a warning if it's not the latest version: http://chriswarbo.net/git/warbo-packages/git/branches/master...
I wrote these once cache.nixos.org stopped providing "non-core" i686 binaries a few years ago, and I can't be bothered to build those large applications from source.
The `runCommand` function lets us define a "package" by just executing bash code. This avoids all the "phase" machinery that most nixpkgs definitions use (those phases are great for fine-grained overriding; but we don't usually needed to override our own definitions). It takes 3 arguments: the output name, a set of env vars (including `buildInputs` which will populate $PATH) and a string of bash code (which can be multi-line using ''two quotes''):
$ nix-build -E '(with import <nixpkgs> {}; runCommand "hello.txt" { buildInputs = [ foo bar baz ]; } "echo hello > $out")'
these derivations will be built:
/nix/store/z6ffwc1jb70zwz2ly42gaalxnvm7q0gk-hello.txt.drv
building '/nix/store/z6ffwc1jb70zwz2ly42gaalxnvm7q0gk-hello.txt.drv'...
/nix/store/l6sd75hj4854pdzv4044ma42axc2v5v4-hello.txt
The sandbox can be selectively disabled (via whitelists), or turned off altogether, which allows builders to access arbitrary filesystem locations and the network. For example, if we have a Python pip project, and we want to take a baby step towards Nix, we could do this (assuming the sandbox is disabled): with import <nixpkgs> {};
runCommand "my-app"
{
buildInputs = [ (python3.withPackages (p: [ p.pip ])) ];
}
''
echo "Making mutable copy of app" 1>&2
cp -r ${./.} "$out"
chmod +x -R "$out"
echo "Putting dependencies in place" 1>&2
pip3 install -r ${./requirements.txt} --target "$out"/
''
There are all sorts of other tricks. For example, Nix will make immutable copies of files if we reference them directly like ./foo (these copies are stored in /nix/store). If we don't want that, we can reference a symlink instead, since the resulting "immutable snapshot" is just a link to our mutable, non-/nix/store path.We can avoid having to hard-code hashes for "fixed output derivations" (like downloaded URLs) by overriding their `outputHash`, `outputHashAlgo`, `outputHashMode` and `sha256` to `null`.
I wrote up a bunch of these sorts of hacks at http://chriswarbo.net/projects/nixos/useful_hacks.html (although its a few years old, so might not be up to date)
Of course, these things aren't advisable; but they're very useful to get up and running quickly, and things can be made "more Nixy" later.
Here's an overview of the escape hatches that are currently around in NixOS for package management, meaning you can use them to avoid packaging something natively for Nix, or to run pre-built software for other distros or targeting multiple distros:
• flatpak — use flatpak to install stuff just like you would on any other distro: https://nixos.org/manual/nixos/stable/index.html#module-serv...
• FHS user environments — create a usermode chroot that puts Nix packages where naive `./configure; make; make install` programs will expect to see their dependencies
• steam-run — run whatever in the pre-built FHSUserEnv that Nixpkgs uses for Steam: https://nixos.org/manual/nixpkgs/stable/#sec-steam-run
• AppImage — launch an AppImage using `appimage-run` or create a quick and dirty Nix package that wrapps an existing AppImage: https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-appimageTo...
• Docker — you can run stuff with Docker if ya want: https://search.nixos.org/options?channel=unstable&show=virtu...
NixOS also includes little escape hatches on the configuration side for every service. Basically every module also has an ‘extraConfig’ type of option, which lets you append to the config file that NixOS generates for the service in its original/raw format.
Otherwise, you're generally looking at setting up a chroot environment. I can think of some ways that NixOS could be a little more generous with escape hatches, but I don't think there's much developer interest in the community.
This is actually changing so that the additional config can be merged/validated even if it can't be typed
https://github.com/NixOS/rfcs/blob/master/rfcs/0042-config-o...
I suppose this could still be annoying for someone who really just wants to copy/paste, though.
NixOps[0]? But yeah, it isn't featured as prominently anymore as it used to be...
It certainly doesn't give you any kind of opinion on how to, say, structure a repo that has your hardware configs, Home Manager on a non-NixOS machine, Home Manager on a NixOS machine, some systemd services, etc. And there's no reason to need NixOps to maintain a repo of three or four physical machines that you have around your house.
You don't. You can use `nixos-rebuild test --target-host=red --flake .#red`. If you want to build locally, add --build-host=localhost.
NixOps gives you the advantage of not having to repeat yourself, but you could easily whip up a shell script for `rebuild-switch red`.
With other popular distributions, you can rely on being able to find a StackOverflow answer with whatever your error message is. Most other OSs and package managers are malleable enough that you can easily beat them into shape should anything go wrong. It's also usually easy enough to adapt how things are done in one Linux distribution to another, but Nix is weird and takes a long time to figure out. (Nix stuff itself being expressive enough that there's often more than one way to do things).
Thank you for highlighting this. I love elegance, safety, reproducibility, declarative specifications, etc. But the minute I get the sense of "this philosophy is getting in the way. I am stuck, even though I know exactly what to do below this layer of abstraction", I get very annoyed.
That's why I asked about "escape hatches" in a sibling comment.
It can be a superb experience after you go through that trouble. Or it might not be. It depends on what you're trying to do, exactly, but you have to first spend a lot of effort learning before you'll have your answer.
Personally, I found it worth my while.
There was a similar issue if you didn't use Python virtualenvs properly, which was an early source of frustration for me personally.
If that happens, users will easily double and maybe more hands might fix the docs or perhaps it's a chicken egg problem but if it's the doc, it's easily solvable.
I’m a huge fan of the project and will give it a try again at one point. But I decided for now to go fully manual with arch to get a better understanding of most components involved in Linux before using the automatic that is nix.
I think one can do this separately, e.g. run NixOS for the host OS and then do LFS or Gentoo or Arch in a VM where you can easily blow things up and revert to a prior snapshot.
Also, flakes takes care of the channels shenanigans by pinning input package versions.
The hardest thing about Nix is that there is very little in the way of "beginner to intermediate" documentation. There is a huge gap between tutorials about just tweaking configuration.nix and a bit a people who have insanely abstract / modularized configs.
The key insight for me was that although they sell the "it's a lazily-evaluated language" thing really hard, at the end of the day there are a few things forcing side effects:
- There are certain key attributes that nix will call forceAttrs on (in the interpreter). You can think of these as the "roots" that will act as a starting point for evaluating all other attributes as needed. This is the stuff you usually see like nixosConfigurations, shell, etc. The sad part is that you really have to read the C++ source to understand this it seems. - At the "bottom", there are a bunch of things built in to write to files in the sandboxed result/ directory, but there are also these monstrous bash scripts to do a lot of the heavy lifting, e.g. the mkDerivation one. It's important to read over these scripts to understand what's being written.
I've found this youtube series [0] to be a good way to figure out how to go from beginner to intermediate.
[0] https://www.youtube.com/playlist?list=PL-saUBvIJzOkjAw_vOac7...
I will give it a go again when I have time as I like the declaritive nature of nixOS.
edit spelling