I wrote this so I can link to it in future comments :)
It attempts to describe Nix’s data model, including basic “hello world” examples. I’ve seen similar explanations of, e.g. git’s internals, but haven’t seen much for Nix (outside the original papers/dissertation). It specifically avoids:
Detailed description of CLI
Nixpkgs (or indeed any imports!)
Any deep dive on the Nix language
Any reference to “package management” (indeed, I think of Nix as “Make done right”; and now consider “package managers”, “configuration managers”, etc. to be unnecessary workarounds for all of the “Makes done wrong”)
Profiles, garbage-collection, flakes, channels, etc.
Installation instructions
I may write a followup at some point. If not, the Nix pills series taught me a lot when I was getting started (10 years ago now!)
I would absolutely adore (and recommend to friends) any successive articles written in this way that build up to the things you initially are trying to avoid (:
This is the first thing I’ve read about Nix where I felt like I actually understood what was happening and why. I might be mistaken but I’m still grateful that you wrote it up, thanks!
Props to this Nix tutorial for covering something other than the same basics that the other dozen Nix tutorials cover. If I wrote a Nix tutorials, I think I’d begin it in the same place: the basic operation of executing a build, including the distinct steps of instantiation and executing the build.
Like Git from the Bottom Up, this is exactly what I personally need to understand Nix. And, like Git from the Bottom up, I wish I had it five years earlier.
Question: when we run nix-instantiate, what determines the hash of the resulting file? My guess would be that it’s just content hash, so, if I do
To be honest I’m not entirely sure what Nix includes in its hashes. The ultimate source of truth would be the Nix source code, which I’ve looked through every now and then to answer such questions (it’s not too hard to follow; speaking as someone who avoids C++ as much as possible!)
Note that we can add .drv files directly to the Nix store, but it requires some trial-and-error to get the hashes right. For example (where the echoed text is a stripped-down copy of an existing .drv file):
$ echo 'Derive([("out","/nix/store/00000000000000000000000000000000-myName","","")],[],[],"aarch64-linux","/bin/sh",["-c","echo ABC > $out"],[("out","/nix/store/00000000000000000000000000000000-myName")])' > myName.drv
$ nix-store --add 'myName.drv'
error: derivation '/nix/store/97ip2v92rqjs83s2xxf23wq011ln9kjq-myName.drv' has incorrect output '/nix/store/00000000000000000000000000000000-myName', should be '/nix/store/r853x2wai95pp9c711j10srq10xryn8a-myName'
$ echo 'Derive([("out","/nix/store/r853x2wai95pp9c711j10srq10xryn8a-myName","","")],[],[],"aarch64-linux","/bin/sh",["-c","echo ABC > $out"],[("out","/nix/store/r853x2wai95pp9c711j10srq10xryn8a-myName")])' > myName.drv
$ nix-store --add 'myName.drv'
/nix/store/mcjp1bawqh0my5pma8lbpm4mdpldw9sd-myName.drv
$ nix-store --realise /nix/store/mcjp1bawqh0my5pma8lbpm4mdpldw9sd-myName.drv
this derivation will be built:
/nix/store/mcjp1bawqh0my5pma8lbpm4mdpldw9sd-myName.drv
building '/nix/store/mcjp1bawqh0my5pma8lbpm4mdpldw9sd-myName.drv'...
warning: you did not specify '--add-root'; the result might be removed by the garbage collector
/nix/store/r853x2wai95pp9c711j10srq10xryn8a-myName
$ cat /nix/store/r853x2wai95pp9c711j10srq10xryn8a-myName
ABC
As someone who uses nix actively and knows just enough about it to be dangerous, I must say this was really, really helpful. Thank you very much for writing it.
I wrote this so I can link to it in future comments :)
It attempts to describe Nix’s data model, including basic “hello world” examples. I’ve seen similar explanations of, e.g. git’s internals, but haven’t seen much for Nix (outside the original papers/dissertation). It specifically avoids:
I may write a followup at some point. If not, the Nix pills series taught me a lot when I was getting started (10 years ago now!)
Agreed with your approach. It was very affective and helpful. Than, you!
I would absolutely adore (and recommend to friends) any successive articles written in this way that build up to the things you initially are trying to avoid (:
This is the first thing I’ve read about Nix where I felt like I actually understood what was happening and why. I might be mistaken but I’m still grateful that you wrote it up, thanks!
Props to this Nix tutorial for covering something other than the same basics that the other dozen Nix tutorials cover. If I wrote a Nix tutorials, I think I’d begin it in the same place: the basic operation of executing a build, including the distinct steps of instantiation and executing the build.
Thank you!
Like Git from the Bottom Up, this is exactly what I personally need to understand Nix. And, like Git from the Bottom up, I wish I had it five years earlier.
Question: when we run
nix-instantiate, what determines the hash of the resulting file? My guess would be that it’s just content hash, so, if I doI’ll end up with the same hash, but apparently that’s not the case?
To be honest I’m not entirely sure what Nix includes in its hashes. The ultimate source of truth would be the Nix source code, which I’ve looked through every now and then to answer such questions (it’s not too hard to follow; speaking as someone who avoids C++ as much as possible!)
Note that we can add
.drvfiles directly to the Nix store, but it requires some trial-and-error to get the hashes right. For example (where the echoed text is a stripped-down copy of an existing.drvfile):As someone who uses nix actively and knows just enough about it to be dangerous, I must say this was really, really helpful. Thank you very much for writing it.