by vbrandl · 4y ago · 0 comments
For me, it has come so far that I don't have any interpreters or compilers in my main OS environment and even for one off REPL sessions, I'll use `nix shell nixpkgs#python3 -c python3`.
[0]: https://nixos.org/
Be prepared to sink dozens and dozens of hours learning the language, it’s features, and switching everything over. Be prepared to get stuck when you are required to use an old version of a language and things don’t install correctly — and sometimes blocked by nix itself from installing it. Be prepared to get stuck if you are not in the happy path. Be prepared to get lost in the sometimes scant documentation.
I love the idea of nix, but it did not work for me except for the simplest cases.
Like you, I find Nix hard to use and the docs confusing (beyond trivial examples). The amount of time the Nix mafia spends proselyting could be better spent making better docs
But it's no wonder that people keep commenting that Nix is an awesome idea that solves a lot of problems. That's because it's an awesome idea that solves a lot of problems (yeah, like Rust).
It is also completely undiscoverable (not completely unlike Rust), and that's why almost nobody uses it. But that's a matter of improving the documentation or maybe fixing one or two superficial problems on the language.
Based on a quick check of their Wikipedia page, Nix has been around for 15 years, since 2007.
So those for sure look like structural problems. I don't know which ones exactly, but paraphrasing Tolstoi, popular software products are popular in much the same way, while unpopular ones are unpopular in their own unique ways.
In my experience almost no open source project that hasn't entered the mainstream in its first 10 years manages to turn the ship around, unless it lucks into a major change of environment. Programming languages are sometimes exempt if they have a "killer" library or framework pop up.
The reality is that the docs are fine. The reference material is comprehensive, and there are some very good tutorials and introductory essays to get people started. The problem is that it's difficult. Nix is very, very different from what people already know, and it takes quite a bit of effort to learn how it works. Most people just aren't prepared to do that much work just to install some software.
then show me where `nix-env`'s `--arg` and `--argstr` is documented.
Oh I think you're using HN wrong. To learn about the subject you can read the actual article that's linked to. No guarantees about the comments section.
I've never heard of asdf and popped in here to read about it, people's experiences with it, etc. If I want to read about Nix/OS I'll find a thread on that, or go to /r/nixos, etc.
Nix provides a level of flexibility that other solutions simply do not offer. If a package in Nix doesn't fit your requirements for whatever reasons, you can create a modified version of a package with ease. For example, say that you need a version of Nginx built against a custom version of OpenSSL. You can do just that with a few lines of code:
let
mypatch = pkgs.fetchpatch {
url = "https://example.com/bugfix-for-openssl.patch";
sha256 = "...";
};
openssl = pkgs.openssl.overrideAttrs (old: {
# add build flags
configureFlags = old.configureFlags ++ [ "--enable-foo" ];
# add dependencies
buildInputs = old.buildInputs ++ [ pkgs.foo ];
# add patches
patches = old.patches ++ [ mypatch ];
});
in
nginx.override { inherit openssl; }
Nix will even know which packages it'd need to build locally instead of downloading a prebuilt binary.You can't do that with other common package managers. You're stuck with whatever the package manager provides you. So if you're not in the happy path, you're out of luck. You'd either have to give up or build from scratch. If you need a bugfix for a particular package, you'd have wait until the fix reaches the package repository. When I was using Ubuntu, that was often until the next major release. None of this stuff is a problem with Nix, which allows for customization with very little effort.
It's easy to learn about Nix if you know where to look for. Nix Pills [1] would be a good start. The core language is the easiest part. It's JSON, but with functions and variables for proper abstraction. The documentation, while imperfect, is quite extensive compared to a majority of other high profile open source projects. If the documentation fails you, the Nixpkgs repository [2] is an even more rich source of information. The code is well organized, and I was able to get familiar with writing Nix packages fairly quickly by grepping the codebase. And finally, Nix has a sizable community so you could always ask if you're stuck.
I understand you are saying it with the best intentions but it might sound like not the most correct way of defending a tool being criticized for its lack of documentation or easy of use.
Here's the simplest example of an utter failure when I tried nix: installing ruby 2.2. It's very possible I did something totally wrong, but when I have to use ruby 2.2 and nix cannot do it, it sort of kills your awesome example of being able to override openssl.
Another extremely frustrating experience was the bifurcation between nix and nixos. Clearly one experience is preferred over the other. So many blogs and docs talk about things like HomeManager and other configurations that are nixos specific.
On top of nixos/nix, there's also shell.nix/flakes. There's just too much development in nix right now to make sense of what's going on.
Considering that ruby 2.2 was released in 2014, I can't say I'm surprised. It's too much effort to continue maintaining something so old. But if you really want to use it, you could try using the package definition from an older commit of Nixpkgs:
let
nixpkgs1709 = pkgs.fetchFromGitHub {
owner = "NixOS";
repo = "nixpkgs";
rev = "e09c0adc63d10249dac8f90313f91e1050861d3c";
sha256 = "sha256-Di9D0gvaESV3JmX/kW2uEJ68QDlAke23t19bImTXsJ8=";
};
pinnedPkgs = import nixpkgs1709 { };
in
pinnedPkgs.ruby_2_2
> Another extremely frustrating experience was the bifurcation between nix and nixos.I'm not sure what you mean. Nix is the package manager while NixOS is an entire distro based on Nix the package manager and its configuration language. They have clear separation of concerns. Keeping it that way has actual benefits too, namely portability. It allows you to use Nix on a wide range of platforms including non-NixOS Linux distros, macOS, and even BSDs to some extent.
> On top of nixos/nix, there's also shell.nix/flakes.
Flakes are currently an experimental feature and not meant for wide use yet. So if you're just getting started with Nix, I'd recommend looking into it later until you're sufficiently comfortable with Nix.
They're not fundamentally different, though. Flakes formalize the conventions for writing Nix expressions to make them more reusable.
I've been using nix as my daily OS for six months and I'm able to be productive. At the same time, I'm often confused as to whether I should use nix-shell, nix-env or modify my configuration files.
Thanks for the great comment.
Also rollbacks in nixos are amazing.
On the other hand newly packaging something can be really daunting.
That's less of a guarantee with a collection of shell scripts.
Nix provides better abstractions and composability to make that experience far less painful. Note that in my previous example, I didn't need to specify how to build OpenSSL or Nginx. I was able to reuse the definition from Nixpkgs and apply my own customizations to it.
I also feel like nix is immediately tangible and beneficial in a way that makes us want to get to the power user level, but I don't feel like wanting to learn, say, kubernetes means one automatically wants to learn to be a cluster operator. The scope and complexity are great, and I have experienced all of the things you have described as well. Now that I am fairly confident in my abilities, I think it's important to make sure we find ways to help everyone get the most out of it, even if it doesn't involve scaling the wall that is the learning curve.
Can you clarity what you mean?
What do you mean by "trap"? Which (if any) of the following concepts factor into your understanding? Broken expectations? Premature optimization? Over-engineering? Lock-in?
Are you suggesting that many users of the Nix package manager "fall into a trap" and begin using NixOS?
What do surveys and/or data show about usage of just the Nix package manager? Combined with NixOS?
The Nix ecosystem is attractive to me at a high level because it looks like it has the possibility to revolutionize a lot of the problems that come up with an increased focus on devops culture. The problem with the Nix ecosystem itself is that it is larger and more varied than it might appear.
There are really three large entities in the ecosystem: nix the language, nix the package manager (nixpkgs), and nix the OS (NixOS). Despite the fact that there is a lot of overlap in some of the details of these three things, they are easily understood as separate entities. There are a bunch of smaller secondary entities such as nix-darwin, home-manager, devshell, and digga (formerly devos). The aim of these entities is usually to supplement, enhance, replace, or reproduce features of nixpkgs and/or NixOS.
Each of these entities has its own learning curve. It takes surprisingly little nix knowledge to use it as a dev environment generator, but nixpkgs knowledge will help a bit more. Even within nixpkgs itself, each major language has a large ecosystem built up around facilitating package building and deployment, and these are extremely heterogeneous. Some of them require that you regenerate the entire matrix of package dependencies for the package manager (javascript, haskell). Some of them have tooling that allow you to get started with almost no effort (mach-nix for python). These all have their own peculiarities. NixOS has a ton of modules, all rather heterogeneous, on top of requiring more careful knowledge of how to put together a system than most other distros. And nix the language without nixpkgs is very small, and some of what it does have built in is necessary so it can bootstrap an actual stdlib from nixpkgs.
The trap is sprung once you've been sold on the idea, and even seen it shine through some things, but feel like you have an entire wall to climb to get to a point of general competency. All of these things I've described, and even some of their subgroups, require new knowledge for a more complete understanding. I do not understand the details of many of the things I have described, and I would consider myself a power user at this point. What looks like one gigantic, insurmountable cliff is really dozens of much smaller cliffs, many of which are not necessary, at least not to most people.
At one point I wanted to test my skills in a really generic way, so I helped fix broken packages on macOS during one of the two yearly regression periods where the community tries to get the CI builds passing. I picked the packages completely at random. Most of what I ended up fixing was C/C++ based, and I don't normally work in it, or I haven't for many years, but I feel like I learned a good bit about cmake. I don't really write Haskell, but I was able to fix a bug in a solving library having to do with a missing portability shim. I didn't have help for any of this, and in some cases I was either too stuck to fix the build or unable to fix it due to problems beyond my control.
All of this is to say that I think getting to this point is a lot less painful if one takes a use case, preferably a narrow one, and focuses on learning whatever is necessary in the service of that use case. Some good topics are dev environments, using nix as an app/project builder, using nix to build containers, using nix to manage remote machines (the difference between these and personal machines is that the scope of these machines tends to be much smaller). I think people try to take on too many of these things simultaneously and end up overwhelmed. That's not to say that NixOS isn't worth learning, but I think many people could realize most or all of the real-world benefit if they use nix on an OS they're comfortable with. Coming back to my comparison of being dropped in the Romanian countryside, if you want to learn Romanian, start there. If you want to experience the countryside, maybe a guided tour is the way to go. If you want to learn about the life of Ligeti, you might not even need to learn Romanian! But total immersion is difficult and draining, and a failed immersion experience doesn't mean Romania is a bad place. So it is with Nix.
package manager (whether on Darwin or any linux distro), to have some tools globally available:
nix-env -iA nixpkgs.${some-package}
per project, most shell.nix can just look like this: {
pkgs ? import <nixpkgs> {},
}:
let
some_var = some_value;
in pkgs.mkShell {
buildInputs = [
pkgs.some_package
...
];
shellHook = ''
# this is bash, so, whatever floats your boat
'''
e.g asdf, only much more generic with full non-leaking package management: {
pkgs ? import <nixpkgs> {},
}:
let
some_package = pkgs.some_package_1_2;
in pkgs.mkShell {
buildInputs = [
some_package
pkgs.some_other_package
...
];
shellHook = ''
export SOME_VAR="some_value"
...
'';
}
ruby/rvm/rbenv/bundle exec (example for rails >= 6): {
pkgs ? import <nixpkgs> {},
}:
let
ruby = pkgs.ruby_2_7;
python = pkgs.python27;
node = pkgs.nodejs-14_x;
in pkgs.mkShell {
buildInputs = [
ruby
pkgs.sqlite
python
node
pkgs.nodePackages.yarn
];
shellHook = ''
export RUBY_VERSION="$(ruby -e 'puts RUBY_VERSION.gsub(/\d+$/, "0")')"
export GEM_HOME="$(pwd)/vendor/bundle/ruby/$RUBY_VERSION"
export BUNDLE_PATH="$(pwd)/vendor/bundle"
export PATH="$GEM_HOME/bin:$PATH"
'';
}
python/pyenv/venv: {
pkgs ? import <nixpkgs> {},
}:
let
python_packages = python-packages: [
python-packages.pip
];
python = pkgs.python38.withPackages python_packages;
in pkgs.mkShell {
buildInputs = [
python
];
shellHook = ''
export PYTHON_VERSION="$(python -c 'import platform; import re; print(re.sub(r"\.\d+$", "", platform.python_version()))')"
export PIP_PREFIX="$PWD/vendor/python/$PYTHON_VERSION/packages"
export PYTHONPATH="$PIP_PREFIX/lib/python$PYTHON_VERSION/site-packages:$PYTHONPATH"
unset SOURCE_DATE_EPOCH
export PATH="$PIP_PREFIX/bin:$PATH"
'';
}
mixing arm64 and x86_64 on an Apple Silicon machine: {
x86_64 ? import <nixpkgs> { localSystem = "aarch64-darwin"; },
aarch64 ? import <nixpkgs> { localSystem = "x86_64-darwin"; }
}:
let
foo = aarch64.foo;
in aarch64.mkShell { # this makes nix-shell drop to an arm64 shell, change it to x86_64 to be intel/Rosetta2
buildInputs = [
foo
x86_64.bar
aarch64.baz
];
}
using an unstable/pinned/git package: {
stable ? import <nixpkgs> {},
unstable ? import (fetchTarball http://nixos.org/channels/nixos-unstable/nixexprs.tar.xz) {},
pinned ? import (fetchTarball https://github.com/nixos/nixpkgs/archive/ca2ba44cab47767c8127d1c8633e2b581644eb8f.tar.gz) {},
git ? import (fetchGit { url = "https://github.com/nixos/nixpkgs/"; ref = "refs/heads/nixos-unstable"; rev = "ca2ba44cab47767c8127d1c8633e2b581644eb8f"; }) {},
}:
let
foo = stable.foo;
in stable.mkShell {
buildInputs = [
foo
unstable.bar
pinned.baz
git.qux
];
}
selecting a particular C/C++ compiler&stdlibc++ version: {
pkgs ? import <nixpkgs> {},
}:
let
clang = pkgs.clang_12
in pkgs.llvmPackages_12.stdenv.mkDerivation {
buildInputs = [
clang
];
}
The trap is: You could handle all of that by using or writing nix features. You could even use NixOS instead of whatever distro you're used to. But then by going cold-turkey you have to learn whatever nix feature on top of all the basic (as in fundamental) things nix has to offer. Purists would say "no no no this is not the sanctioned way", which is kind of true but also setting yourself up for failure; it'd be like looking at a mountain and trying to jump right to the top, which of course you will fail to, when you could just be climbing it and be successful. Whatever practical gets you on board is fine. You can get to the "pure nix" stuff later, if you ever need to.Thanks to a chatty Romanian taxi driver I had earlier today, I understand this.
I recently gave nix a try because of a hacker news comment and agree with most of the points in this comment. The fact that new users are encouraged to read not 1, but 3 separate manuals to grok the tool is extremely discouraging for new users. Also the fact that nix is currently transitioning to a new feature called flakes and deprecating channels signals to me that the ecosystem is not stable enough for me to recommend that my team at work use it right now.
That being said, I intend to read at least 2 of the manuals and use nix for dependency management in my side projects and to manage my system dependencies. I think nix takes all the great things that asdf can do, to a whole new level. The dream of having a nix section in my projects 'Getting Started' that only has 2 steps (1. Install nix 2. Run `nix-shell`) is something I desperately want.
I think nix is much like erlang in that it's incredibly powerful and solves a lot of common problems, but it's used and maintained by an old guard that went through the trouble of learning how to use it and maintains the "I did it, why can't you" mindset. The erlang ecosystem has been given new life and been made easily available to new users thanks to the awesome work done by Jose Valim and the rest of the elixir team and community. However, I really hope that it doesn't take 30 years and a new language built on top of nix for it to become accessible to everyone.
For those working on nix and the new flake functionality please look to things like elixir and asdf for inspiration on documentation, ease of use and pragmatism. Help make the tool you love ubiquitous. It will ultimately make your life easier when working with new developers because they will already have nix installed.
I intended to do everything I can to help push nix in this direction (once I understand how the hell it works) if anyone reading this, has some time on their hands and wants to improve the state of software development for everyone please go down the nix rabbit hole and try to find ways to contribute to making it more accessible.
I've found it to be quite flexible though. For example, here's a commit in which I apply a patch to a tool to solve a problem that the derivation hadn't taken into account (and absent a home-manager solution): https://github.com/samhh/dotfiles/commit/867dd3b4d4b3942a0aa...
I understand everything in that sentence except the word "so".
I really like Nix but in this particular way it feels like taking a big step backwards from the other languages I frequently use, particularly for a language in which you'll necessarily be constantly interacting with bespoke interfaces.
Really, I like the idea and I actually like the fact that they use Guile (scheme) instead of a home-built DSL, but guix is a horrible, poorly documented, doesn't work even on the happy path mess.
But maybe I'm just a "GNU zealot".
I didn't mean to imply everyone who uses it is a GNU zealot, but there's definitely a subset who are.
Look at Python with Nix: https://nixos.wiki/wiki/Python And then consider the simplicity of `pyenv install x.x.x` and a requirements.txt.
"possible future efficiency gains".
With this in mind, the question shifts from rather vague talk about possible futures to various kinds of scenarios.
What happens when software developers have a deterministic build system? On the whole, the benefits are significant. Sure, there are costs to get there. In my view, the benefits often outweigh the costs.
- you want to use lang-x.y.z, so you'll just reference an old commit
- turns out that version was never packaged, so you copy x.y.z-1 to your flake/overlay
- you end up patching the build because it doesn't work by default with new environments
- on the other hand a module you're using needs the new environment so you do your best to make everything happy - more overlaying
- because you changed something crucial in a few steps, you're compiling llvm 20 times through this process and now it's 1am
Compare that to putting "rust 1.59" in your .tool-version and running "asdf install". Guess who tried to use 3 specific versions of rust at the same time recently and got very familiar with the process...
(That's also ignoring the issue like Ruby being packaged without the platform identifier in nix which makes it unusable for some development purposes)
asdf does not allow you to keep three different versions of the same language, so I'm not sure how that compares? It's not super-trivial to do in Nix, but at least you can do it.
asdf is also no different than Nix when it comes to minor/major versions. You're at the mercy of what the plugin does, other than that you have to create your own plugin from scratch or make a fork. Nix has the option to patch things up more easily at least.
It should never be Nix's business (or any other OS) to provide elements of a project's toolchain. Reciprocally, projects should go out of their way to not depend on OS installed toolchain components.
As others pointed, the idea behind nix is cool, but it sometimes fall short, hope it get ironed all the issues in macOS so I could give a try in the future.
nix-shell -p bundlerhttps://lists.gnu.org/archive/html/guix-devel/2022-03/msg001...
Mostly I think Nix would be just as hard if packages were expressed in any language.
Stuff like "this program compiled by nix is using a different glibc than this library" is going to be confusing regardless of whether Nix expressions can be written with JavaScript; or stuff like some program assumes it can write to $HOME in its build script not playing nicely with Nix.
A lot of why Nix is hard is because it's weird, and that even as an end user when things go wrong it may require understanding more about what's going on than what other solutions require.
(Tongue-in-cheek, but I'd actually like to see this happen: Nix is a fantastic concept obscured by space-alien syntax.)
I don't understand the concept/motivation behind it, everything that nix does should be able to be done in userland, like podman does, which I love.
The daemon is not powerful at all, almost all the logic happens in the client.
The client does the parsing, job ordering, and tells the daemon exactly what to do.
The daemon, only required in multiuser environments, does only two things really.
- ensure that the /nix/store is protected as the daemon should be the only process able to _write_ to it. Everything can read from the nix store; no nix required at all.
- execute build commands in a sandbox. Providing isolation between multiple builds.
I'm not sure if there are other reasons.
The biggest drawback is installing Nix on OS X is a huge pain. Docker has lots of magical UX tooling to make it rather seamless in comparison.
Did it on an aarch64 mac the other day. One command (two?), 15 minutes, done.
I spent time understanding it, and getting it working, but it really doesn't work just universally. Once the install process becomes more like, say, miniconda -- where an unprivileged user can install it locally with ease -- nix will get a massive boost in my books. Until then, I can only yearn.
But, I would encourage people to try out NixOS. Its much better than the package manager, yet it follows the same basic principles.
- Uses a simple DSL language
- 1 config file describes your entire environment, you basically use nix the language to tell your computer if you want wifi enabled or not, or if you want sound, or if you want i3 etc.
- Its easy to roll back, since the config file is versioned (like git)
I personally use a mac machine, running nixos in vmware fusion and I can't believe how long I had been using a separate machine for development and another for personal use.
I get the best of both worlds with this setup; my messages and notes are here, I get world class hardware (come at me bro), and I get to control every single aspect of my development machine.
I still use docker inside nixos, and yes I know its almost supposed to replace docker and all but I will get there one day, right now is not it.
Areas for improvement: - They should have come up with a better name: nix, Nix and NixOS are confusing (language, package manager and operating system - in that order. Yes, they went with an upper case letter for the pkg manager) - Docs NEED to be better, this is not an easy task. The language is shared between every component. - Give playgrounds to beginners, I shouldn't have to download the minimal iso and boot it on my machine to get a sense of how powerful it is. There has to be an easier way.
End of my rant. I started using it a month ago, I have run into issues with dependency pinning but I remain optimistic.
It's astounding because the local version is doing a naive, case-sensitive regex search, which is pretty bad when you're looking for, say, the Perl package MIME::Lite which can have various junctions and capitalizations. In fact, that package has a listed name of perl5.34.0-MIME-Lite but is installable as nixpkgs.perl534Packages.MIMELite!? I can't even
I found the code a few months back (but can't be bothered to look it up just for a comment). I didn't fix it then because I had other priorities than learning how to modify my local copy of nix to test it.
For $REASONS I want python 3.7.10, and the full scientific stack (numpy, scipy, pandas, numba, matplotlib, ...) at the latest compatible versions along with pytorch. I don't mind waiting for things to compile. How, if at all, can Nix help me get into an environment that meets those constraints? Can it help without requiring me to find and copy hashes of things?
Put an incorrect hash in. Run the build. The error will tell you what hash it actually got. Copy that. Run build again.
Both for parent's quandary, and for when you're working with a group and don't want to force everyone else to use Nix.
This makes me wonder: does Nix use libsolv, or a similar idea?
Roughly, Nix's approach is to describe packages as a function of it dependencies as inputs. (And all the inputs have all their dependencies described in Nix, too). Nix doesn't do things like "install latest version of X". (One of Nix's principles is build reproducibility; the package should have the same behaviour if built with the same input).
Tools like mach-nix or poetry2nix wrap around nix.. as I understand, they adapt the requirements.txt or whatever and pass it into Nix in a way that Nix can understand.
As for Windows support: It seems to be possible to use nix in WSL [0] but I've never tried that. I have used nix alongside apt and pacman on Ubuntu/Arch before I decided to go all the way and install the NixOS distribution. Using it alongside other package managers worked really well.
I've used Nix on Linux (i686, x86_64 and ARM), macOS and Windows (WSL2). I believe it also works (although not officially supported) on BSDs, etc. too, although I've not tried that yet.
> Lorri sounds amazing though! ...which handles the "this directory needs these specific packages" installed locally, not globally problem that Debian gives you).
You're describing Nix, not Lorri. Specifically:
- nix-shell provides a directory-local environments (and more, e.g. #!nix-shell to fetch script dependencies automatically)
- direnv can enter/exit this nix-shell environment when cd-ing in/out of a directory
- lorri makes direnv+nix-shell faster, by building the environment asynchronously in the background (rather than causing our terminal to freeze)
like "nixpkgs" is almost always the default, from what I can tell, so it could be omitted and simply be assumed to be the default, and not sure what the -c param does but perhaps it could also assume that the specified package(s) are also the ones to install/choose, in which case the whole thing could be like `nix shell -c python3`?
Could it solve the issues we're seeing lately with NPM?
There's a reason why, despite being almost 20 years old, Nix has at best a small cult following. It just doesn't solve the problems that most people are looking to solve, most other solutions are simply easier and in many ways better
Are there any other advantages to using nix?
The advantages are more pronounced for DevOps than for developers.
e.g. for DevOps, Nix allows copying a package of software (and all its dependencies) from one computer to another, with confidence that the programs will work on both machines.
For developers, Nix is more appealing if you ever work on more than one computer. e.g. with Nix, it's easy to ensure you've got the same version of packages installed regardless of whether you're on macOS or Linux.
It's pretty neat for side projects, in order to get "I was able to build it now, I'll be able to build it in 6 months". Whereas, if you're otherwise relying on Ubuntu's system packages, maybe the package versions got updated, and so you'll need to spend time to get your program to work with the latest compiler/libraries.