Repository navigation
Description
Issue description
Background: I professionally develop and operate some large-scale Erlang services and I am investigating NixOS as a potential platform, and here's my thoughts. (Not saying this to brag, but rather to share an outsider's impression)
- NixOS ships a patched version of rebar3 with "hermetic" patches and a vanilla rebar3 called rebar3-open. This behavior is confusing for anyone with Erlang background who wants to use rebar3 for local development. I would propose renaming rebar3 to rebar3-hermetic and making rebar3-open the default package. This change is backward-compatible, as (most) nix packages use rebar3 via a function wrapper rather than directly. Moreover, as nix-build runs with network isolation, I don't see much reason in keeping this patch at all: rebar3 won't be able to fetch any packages anyway.
tl;dr: Shipping a package with incompatible patches and not renaming it is wrong. Proposal: remove rebar3 hermetic patch
- Nix build system tries to reinvent Erlang dependency handling.
rebar3 get-depsfunction reproducibly downloads a fixed set packages with certain hashes validated against the release lock file. There is no reason in making nix wrappers for the packages for two reasons:
-
Maintaining a mirror of hex repository index in semi-manual mode requires too much community effort, and therefore this approach is not sustainable, as witnessed by the last patch to hex-packages.nix from 2017 and lots of broken packages (e.g. try installing something as simple as
cowboy). Integratingrebar3 get-depsinto Nix will fix many problems. -
Last but not least: this goes against OTP design principles [1]. Killer feature of Erlang is hot bytecode patching. Most applications achieve it by packaging the bytecode in OTP releases [2], so they can benefit from Erlang's standard release management system. Note that building OTP releases can be made reproducible, so it doesn't go against Nix packaging principles. But the way NixOS packages Erlang bytecode today is not compatible with the OTP design principles and this is a deal-breaker. Going away from Nix-packaged Erlang applications to Nix-packaged Erlang releases + some nix-specific wrappers for transitioning a running system from one state to another has tremendous potential. But it requires rather fundamental changes in the way packaging happens.
tl;dr: Hot code loading is a unique, yet extremely tricky feature and everyone should stick to the procedures recommended by the OTP team. It means treating Erlang releases as opaque blobs rather than trying to manage their moving parts separately.
Conclusion: I can put time and effort into maintaining the Erlang ecosystem in Nix, or at least try to investigate the live upgrade and reproducible dependency handling, but I want to have some confirmation from the current maintainers that moving closer to the OTP design principles is not going to be flat-out rejected on ideology grounds.
Steps to reproduce
N/A
Technical details
N/A
[1] http://erlang.org/doc/design_principles/users_guide.html
[2] http://erlang.org/doc/design_principles/release_structure.html#release-concept
Activity
hedning commentedon Jan 12, 2019
ping erlang maintainers: @gleber @couchemar @sjmackenzie @the-kenny
nixpkgs/pkgs/development/interpreters/erlang/generic-builder.nix
Line 126 in 497e6f1
sjmackenzie commentedon Jan 13, 2019
If you can do it, go for it. I'm currently working on something similar whereby the dynamic OTP like system calls nix to change its state. To me this is the correct way to do it.
k32 commentedon Jan 16, 2019
First step: #54115
It eliminates a major pain point of having an up-to-date hex registry snapshot that can't be used anyway.
Next step: investigate if it's possible to create an escript parsing rebar lock files and automagically turning them into derivations
ankhers commentedon Feb 6, 2019
I have been meaning to write a thing to parse rebar and mix lock files for a while to get rid of the hex snapshot. I can get working through that.
ankhers commentedon Feb 6, 2019
Just to make sure I understand your proposal here. #54115 is temporary for the new
fetch-rebar-deps, and you want to eventually have a foo2nix tool that will read the rebar.lock file and generate a nix file for the build, correct?k32 commentedon Feb 6, 2019
Yes
ankhers commentedon Feb 6, 2019
Awesome. I can get behind this.
ankhers commentedon Feb 10, 2019
I have started ankhers/rebar32nix which will be the escript to parse rebar3's lock file and generate a nix file for your project. I have also split out the actual building of the nix expression to ankhers/beam2nix so that there is a common library that can be taken advantage of when I build the equivalent for Elixir.
k32 commentedon Feb 10, 2019
Awesome. I will drop my own tool then. Here #54115 (comment) I gathered some useful information about hex hashes, it may help.
There is an extremely cursed way of using hex hashes to fetch hex packages:
Adownloading tar from hexcat VERSION metadata.config contents.tar.gz > $outHash of this derivation will match with the one from lock file, but the contents are unusable
Aup to gzip magic bytes (1f 8b) yielding a valid tarballankhers commentedon Feb 10, 2019
Since we have the name of the lib and the version, I think we can just shell out to
nix-prefetch-urland parse the output to recieve the required sha. I haven't quite gotten to the point of pulling in deps yet, but that is the path I plan on taking. I saw the same approach taken in the carnix tool (rust's cargo2nix tool).If that doesn't work, I will dig through what you have started there.
k32 commentedon Feb 11, 2019
So with your approach some tool will return hashes of the release dependencies that can be copy-pasted to the final derivation? I guess it will work.
ankhers commentedon Feb 12, 2019
@k32 I have a simple working version of my rebar32nix tool. I just wanted some clarification from you. In rebar3-release.nix, the
checkoutsare meant to be the dependencies of the application, right? As in the packages from hex? If so, I had to make a modification to the handling of it and wanted your thoughts. Below is the changeThis places each of the dependencies into the
_checkoutsdirectory so that rebar3 can pick them up when attempting to compile. The version you had failed when passing in a list, which I assume_checkoutsis supposed to be.And for what it's worth, this is the
default.nixfile my tool spits out for rebar3's repository.Let me know if you have any questions.
k32 commentedon Feb 12, 2019
Looks good. In the original
rebar3-release_checkoutsis a derivation already containing all packages in unpacked form, therefore it's not a list. But this change makes it better.ankhers commentedon Feb 16, 2019
In addition to the change I showed above for the
rebar3-release.nixfile, I have renamedcheckoutstobeamDepsto make it more understandable what it actually is. I'm also skipping the_checkoutsdirectory and directly linking into the_builddirectory.I am also trying to come up with a different name than
rebar3Relx. My hesitation with that name is thatrelxis a specific tool to build releases. But that derivation also can build escripts. It is not immediately apparent based on the name that it does so. Unfortunatelyrebar3Releasesuffers from a similar issue because a release is a specific distribution type and an escript is different.Does anyone have any thoughts on what it could be named that is not ambiguous in what it does? I just want to finalize the naming before releasing an initial version of my tool.
k32 commentedon Feb 16, 2019
I see some problems with linking to _build directory. Unlike using
_checkoutswhich is a documented and supported rebar3 feature, manually managing stuff in_buildis a hack.ankhers commentedon Feb 16, 2019
After re-testing, I am fine with using the
_checkoutsdirectory. I swear I tested previously and the link/directory name needed to be the exact same as the application name. That apparently is not the case. So I will revert to using the_checkoutsdirectory.I think the issue with the derivation name is still a problem though. We could have two derivations.
rebar3Releaseandrebar3Escriptizethat inherit from the same builder. Similar to what we are doing with building Erlang.k32 commentedon Feb 16, 2019
Great. I have no problem with changing the name, in fact I agree that bringing relx into name was not correct, but the proper name was already taken by the old derivation.
ankhers commentedon Feb 19, 2019
After a conversation in IRC, I'm thinking we should still use the
_builddirectory. When you use the_checkoutsdirectory, rebar will check on every compile whether or not it needs to recompile that application. This may not seem like a big deal when building for a deployment, but if younix-shellfor development, it will take additional time unnecessarily.rebar3will also remove the entry for those applications from the dependency list in therebar.lockfile.k32 commentedon Feb 19, 2019
I don't think we need to consider development in nix-shell as a common use case. I think of rebar wrapper mostly as a mean to package an existing release and I don't expect that a significant number of developers will use it "ab initio". Main problem with unpacking a package to
_buildis that rebar3 may consider build directory dirty, so it will attempt to sync hex registry, which will fail. It was a big problem with the previous version of rebar wrapper. So we'll get back to bootstrap scripts manipulating contents~/.cacheand what not, and it's something I wouldn't touch with a 10 foot pole.While checkouts directory is a documented way of telling rebar3 that dependencies are local. So this solution is simpler and therefore more reliable.
alexongotos commentedon Mar 5, 2019
@k32 I just picked up on this because I am interested in running erlang / elixir applications on nixos. At the moment I am particularly interested in running zotonic cms on Nixos.
http://docs.zotonic.com/en/latest/index.html
alexongotos commentedon Mar 5, 2019
Sorry I might have contravened use of issue tracker by asking a general question but I thought it might be relevant regarding the issue with rebar3 and otp standards.
ankhers commentedon Mar 16, 2019
Would anyone mind giving my rebar32nix tool a go? Just clone the repo and
rebar3 escriptizeshould take care of the rest. It technically works for hex.pm and git dependencies, but I am having some trouble on the building side with git deps. Please post any issues you find on the issue tracker.k32 commentedon Mar 19, 2019
Sorry, I am having an intensive period at work right now. But I guess I can rewrite at least rebar3 derivation to use your tool in the nearest future.
ankhers commentedon Mar 19, 2019
Right. #57909 actually has my changes. If you use my tool to generate a nix file, you can build using
alunduil commentedon Jun 22, 2019
Have the changes outlined in this issue been documented in the nixpkgs manual? Does that make sense to do? Also, with the mentioned pull request still open do we want to leave this issue open as well?
ankhers commentedon Jun 22, 2019
To my knowledge, nothing really came out of this. I'm wondering if @k32 just became too busy and is unable to continue working on this?
Whatever the reason behind closing the issue, I am more than happy to continue the work I have already started on this.
k32 commentedon Sep 30, 2019
I encountered some other problems with Nix and NixOS, unrelated to Erlang. Mainly they are related to security and secret management. Unfortunately these are not as easy to fix, and they were deal-breakers for my usecase, so I halted my investigation.
Sorry for the radio silence, I should've make it clear earlier.