More airgap questions

I am trying to create the ability to build and use nixos environments on an airgapped network.

I originally created Building with all-tarballs - Help - NixOS Discourse and then realized I was asking the wrong questions.
Private Mirrors in Airgapped Environments - Help - NixOS Discourse explains how to get source for all of the free nix packages and also individual other packages.
I made a modification of the copy-tarballs.pl to remove the AWS upload portion and now I have 94G of nix compressed nix source in a nix store.

It seems that the topics I have seen so far skip over the next part, which makes me feel like I should already realize how to do this, but how do I redirect nixpkgs to this source rather than all of the individual urls within each package?

I have also come to realize that I don’t absolutely need source (though I prefer it). I keep seeing marsnix posted, but I can’t find any information on how it is supposed to be used. This is probably trivial for those who have been nixing for awhile, but I am still figuring out things that I am sure most of the community takes for granted, like that NIX_PATH points to a tar.xz archive which contains a particular revision of github:NixOS/nixpkgs which is why it works different than the flake which points directly to github.

That makes me hopeful that I can just point to a local version of the nixexprs.tar.xz, but then it points to source all over the place.

I would really appreciate a breakdown or a link to somewhere which will do the breakdown. I would also really appreciate a how-to marsnix. At what point is it run on the internet and at what point can it leave the internet? A step by step example would be really nice.

Thanks in advance for all of the help!

1 Like

I had planned to also build a cache server on this other network. I see that substitors can be used in a nix flake to reference a cache server. Will that also work if the /nix/store entries for the source is also present on that server or does nix serve require .nar’s?

If I bring over everything, will it matter that only my substitutor server exists and the original servers do not exist?

Well, in principle there is e.g. nix-serve so you can serve a binary cache directly from the store, if somewhat less efficiently.

Does it have to be a binary? If all of the source’s were already in the store, wouldn’t it grab them like any other derivation?

We call it binary cache because the modal use case is serving precompiled binaries and we are often bad with properly taking into account non-modal use cases. But technically it serves any store path resulting from a build, be they fixed-output or not (so store paths that are downloaded source tarballs are served just fine)

1 Like

Unless I am doing something wrong, it seems that fetch won’t check store paths to see if the source is already downloaded whether the source is within the /nix/store of the current system or being served up via a nix serve.

I changed the nix-store perl command in copy-tarballs.pl to --add-root packages/$name I am hoping that if I serve the packages directory up with nginx, perhaps I can use it as source for source.

That sounds like different content hashes… (But it is not clear what is fetch in this context)

Sorry, by fetch I was referring (perhaps improperly) to the method that is used to retrieve the source code by the various packages. I assume (assume being the operative word) that the ability to redirect toward tarballs.nixos.org is based within the fetch utility. In either case, despite having the source for the packages downloaded into the store (using the copy-tarballs.pl), trying to build without the internet still fails to find the source.

Sorry for my ignorance here. I feel like I am missing something very basic.

As far as I understand, tarballs.nixos.org support is fetchurl-only, and there it looks at hashedMirrors in pkgs/build-support/fetchurl/mirrors.nix, literally downloading from tarballs.nixos.org specifically.

copy-tarballs.pl tries to upload specifically there, and in the local store it creates files named by the hash — but normal building only checks locally for the files with the name specified in the call to fetchurl.

I guess you could resolve tarballs.nixos.org locally to a server serving the hash-named tarballs…But then there is fetchgit which would not be covered anyway.

You can also use nix-instantiate and nix-store -qR to figure out all the dependency derivations for a package, check for the presence of fixed-output output hash in those derivations files, and nix-store -r the fixed-output derivations in advance, getting the properly named fixed-output store paths in advance.

You can also read the documentation about substituters and write a substituter script that tries to use a local hash-named path for fixed-output derivations (thus using the copy-tarballs.pl tarballs), but I am not sure if there is a ready-made one already, probably setting up a local hashedMirror is easier.

1 Like

I did not realize that there was a nix.conf option to set hashed-mirrors, nor did I understand the pathing that needed to be used. This sounds promising! Thanks!

Thanks for the patience and help on this subject. Serving the tarballs as a hashed-mirror using ngnix did work for building small packages. It seems that every time busybox became involved in the build (which is more than you would think), the source was unable to be resolved.

I switched gears and decided to try for a full binary archive.

I think I finally understand this subject enough to understand previous posts on the topic. Perhaps I have complete cognitive dissidence in regards to this subject because it is so difficult to believe this problem isn’t solved.

I have been a software engineer for more than 30 years and somehow working Nix always makes me feel completely dumb. That isn’t to be taken as a slight against Nix, but perhaps as a slight against my own inability to adjust my way of thinking. Nix has so much potential. I am absolutely aghast that it isn’t the preferred solution world wide for so many things.

All of that said, the ability to be used completely offline/isolated/air-gapped is a critical piece that does not seem to have great support at this time.

The state of Nix Isolation and Airgapping

(Please correct the next paragraph if it is wrong)
For anyone researching this in the future, it should be understood that as of this time there is no foolproof solution to binary cache and even less so a source cache. There are some promising works that have been provided on the topic, but I have yet to find a complete working solution.

1. Marsnix

I first tried marsnix, since that has some record that it has been used successfully at least once. My thought was that if I could build a marsnix server, I could nix serve its packages. I was unable to get marsnix to work. It tried nix run with the github url; check out the repository and using nix-build, nix build, nix run; and several attempts at editing it to make it work.

2. Nix in an Isolated Environment

“Nix in an Isolated Environment” is a post by a few nixers trying to achieve the same thing as me. Reading it now makes me feel irresponsible to be posting all of this again. The same questions were brought up by people who undoubtedly have more experience with Nix and they got further than I have been able to so far. I found out the hard way that it is important to read the entire thread before trying anything in the thread as you will find the problems that came out of each attempt and the proposed (if somewhat hacky) solutions. I am going to try the final solution listed there, but I was trying to use existing Ubuntu infrastructure to prepare a cache to build up NixOS and Nix packages and this solution will require me to start from a nix system first. This is not so much a problem as it requires me to rework my infrastructure a bit, meaning I will have to get a bit more lenience from my IT group to let me try this.

3. Specific Mirroring Tool

The Specific Mirroring Tool was mentioned in the “Nix In an Isolated Environment” post. Since the author was kind enough to let us know the documentation is outdated, it is difficult to know the level of completeness offered by this tool. I will be continuing to cheer on AleXoundOS, but it is unclear whether this work continues.

4. Offline Install Iso’s

pete3n generated a repo for offline nix installer isos and discusses it in this thread. It is unclear whether this dependency list can be made large enough to support an offline for an extended period, but this warrants a look.

Additional paths from reponses below:

1. A function rebuildDependencyClosure that produces an efficient closure for offline rebuilds · Issue #180529 · NixOS/nixpkgs · GitHub .

More Information Requested

If I have missed any ongoing efforts to provide isolated/offline/airgapped support for a nix mirror or nix systems, please let me know and I will try to include them in this list. If anyone has specific success with any of these, also please let me know.

1 Like

Thank you for collecting these resources.

I think you are expecting an “on-the-shelf” “push button” solution because otherwise, as you can see, most people have been implementing their own thing for their own needs. The core of it remains and is a form of local binary cache.

Usually, the ecosystem is not great at providing everyone with “push button” solution because it’s rarely needed except for commercial interests / entities.

I will add extra one: A function `rebuildDependencyClosure` that produces an efficient closure for offline rebuilds · Issue #180529 · NixOS/nixpkgs · GitHub.
And I can confirm that I used many of the solutions you mentioned, including just manual mirroring, successfully.

In the end, this is mostly a documentation problem and understanding from first principles how things work because it’s so related to the core of Nix: the Nix store.

1 Like