Repository navigation
Recursive nix - #213
Recursive nix#213shlevy wants to merge 20 commits into
Conversation
|
Ah, forgot to mention, as a quick check I ran |
|
Ah, just realized that this doesn't make the new paths available in chroot. I'll work on a fix to that (which will probably revert the temproots change in favor of a new way of reporting paths), but what's here is still valid. |
|
I've rebased and added commits to make it so new paths are available in the chroot. The recursive paths reporting protocol has changed slightly: The client can only write in chunks of at most |
|
cc @civodul for the slight protocol change. |
|
Ack, still broken, chroot builds are done in a private mount namespace so bind mounts done in the parent aren't seen. Will fix later. |
If NIX_REMOTE=recursive, the client checks NIX_REMOTE_RECURSIVE_PROTOCOL_VERSION for an integer version. If it is set and its major version matches the client major version (current protocol version 0x101), it then reads a connected unix domain socket file descriptor from NIX_REMOTE_RECURSIVE_FD. It sends a message containing the client protocol version in the message data and a connected unix domain socket in the ancillary data, and the other end of that socket is then used for the normal daemon protocol.
With recursive nix, it is possible for a build to legally gain access to paths that weren't specified as its inputs (indeed, that is the whole point). In order for the nix process that started the build to be able to scan for dependencies properly, it must be told when these new paths are (potentially) accessed. This amends version 0x101 of the recursive protocol to put the socket fd into NIX_REMOTE_RECURSIVE_SOCKET_FD and to put another fd into NIX_REMOTE_RECURSIVE_PATHS_FD. This new fd is assumed to point to a new kind of temporary gc root, where the root is kept from being considered invalid by the parent nix process staying alive and thus only a write lock is needed to avoid a race between the gc reading the root file and a process adding to it. Upcoming commits should clarify this if it's confusing.
nix-daemon now supports listening on an already-connected socket. In the normal case, nix-daemon will listen on its traditional socket (passed through systemd or not) and makes a new socketpair for listening for the new kind of connection. If, however, nix-daemon was started with NIX_RECURSIVE_FDS set, it uses the two fds listed in that env var as the two ends of the connected socket and doesn't listen on the traditional socket at all. In a future commit, LocalStores that were *not* launched from a nix-daemon child process will launch nix-daemon with that env var properly set if they need to perform a build. In either case, nix-daemon now has a connected socket to read from and possibly an unconnected socket it's listening to to connect to. When the connected socket is readable, it reads the client recursive version from the message data and the client domain socket from the ancillary data to form a new connection. If the listening socket is connectable, a connection is formed as usual. Finally, when a child process is launched, its LocalStore gets passed the other side of the connected socket so it can use the existing nix-daemon process for recursive nix instead of having to spawn a new nix-daemon process every time (to be implemented in a future commit)
This will allow one process to keep several temproots files alive, as is
required for recursive nix.
The semantics are as follows:
* When creating its first temproot file, a process takes the gc lock,
creates /nix/var/nix/temproots/{pid} directory (deleting it if it
exits), takes a read lock on /nix/var/nix/temproots/{pid}/lock, and
releases the gc lock
* Before adding a new temproot file (including the first one), the
process upgrades its read lock on /nix/var/nix/temproots/{pid}/lock to
a write lock. It can then make as many temproot files in
/nix/var/nix/temproots/{pid}/lock as it likes, so long as they are not
named "lock", and then it downgrades the write lock to a read lock
* Before adding a new root to a temproot file, the process takes a write
lock on the file. Afterwards, it releases the lock. The locking
process *never* holds a read lock on individual temproot files
* For each directory in /nix/var/nix/temproots, the gc first tries to
get a write lock on /nix/var/nix/temproots/{dirname}/lock. If it
succeeds, or if the lock file does not exist, the directory must be
stale so it deletes it and moves on. If it fails, it waits until it
can get a read lock on /nix/var/nix/temproots/{dirname}/lock (after
which the owning process can't create more temproot files until
released) and then iterates through every file not named "lock" in
/nix/var/nix/tmproots/{dirname}, taking a read lock on the file then
reading roots out of it as before. The gc does not release any of the
read locks it takes here until collection is done.
For now, local-store uses /nix/var/nix/temproots/{pid}/main for its
temproots file, but future commits will also put recursive nix temproots
there.
This involves: * Setting up the NIX_STORE_PATH etc. env vars to match the current process * Packing settings into _NIX_OPTIONS * Setting NIX_REMOTE=recursive and NIX_REMOTE_RECURSIVE_PROTOCOL_VERSION to the protocol version * If this process is not itself a daemon worker and it has not started a recursive nix daemon instance yet, making a new unidirectional socketpair and a pipe, sending both ends of the socketpair and the read end of the pipe to a new nix-daemon process in NIX_RECURSIVE_FDS, keeping the write end of the pipe open (so that nix-daemon can know if/when we die), and using the write end of the new socketpair as the store's recursive nix daemon socket * Setting NIX_REMOTE_RECURSIVE_SOCKET_FD to the store's recursive nix daemon socket (and keeping it alive past closeMostFDs) * Creating a new temproots file with the same name as this drv's path * Setting NIX_REMOTE_RECURSIVE_PATHS to the new temproots file (and keeping it alive past closeMostFDs) * After the build (if not using the build hook, which for now must use nix-store --import if it wants to report references not in the inputs closure), read through the new temproots file, add the closure of each temproot (include outputs of derivations) to allPaths, and add each path as a temproot so we can delete the new temproots file This required adding functions to gc.cc to create and delete arbitrary temproots files (which addTempRoot uses to creat the main temproots file). nix-daemon was updated to add the third fd in NIX_RECURSIVE_FDS (the read side of the close notification pipe) to its select set so that it can exit once its parent dies (closes the write side of the pipe). Now recursive nix should work! Next up: Test case.
…ons don't step on each other
This allows the parent nix process to add any needed new paths to the chroot. The reporting protocol is changed to have a cap of 4096 bytes per write and to require reading a single confirmation byte after each write. Temp roots handling is reverted back to the pre-recursive-nix way, since we no longer create a new temp roots file for each build.
Also fix a dumb bug
Recursive path reporting is expensive, especially in the chroot case. So now we only report valid paths and are less defensive about derivations doing stupid things with paths they aren't sure are valid.
Builder processes can purposefully cause an exception to be thrown in any number of ways, so don't let a rogue builder kill an entire store process.
|
Rebased and pushed some fixes. Now this works with and without chroot enabled. Note that only valid recursive paths should be reported now, and that generally you only need to report a recursive path if you might reasonably later assume the path exists (so in fewer cases than before). |
|
BTW @edolstra I think this is ready for merge now, though of course you'll probably want to wait until after 1.7 |
|
Probably best to do it after 1.7. |
|
Any comment on my proposal at #13 ? Maybe a builder written in nix itself might make this obsolete on the long term. |
|
As discussed on IRC, I get the following on MacOSX whenever I run Possibly unrelated, but I get the following with error: disabling reads from fdRecursiveTo: Socket is not connected |
|
@charleso What version of OSX? I haven't tested yet but this looks like a bug in |
|
OSX 10.9.2. |
|
@edolstra I have to bring this up to date and fix some bugs, but before I do that can you comment on whether this is something you want/the general approach looks good? |
|
@edolstra ping? |
|
Do you have any examples of how this will be used? |
|
Most of my experimenting also uses my Without the recursive derivations stuff, if you replace the nix-instantiate with a nix-build the above two envs will be functionally equivalent. Besides nix-make, I'd also like to use this in conjunction with a to-be-written qemu backend to test nixops networks in hydra. If running nix commands just worked inside of a build, then you could just run nixops create and nixops deploy and have a network created. |
|
How would the inner build.nix find its dependencies (e.g. Nixpkgs)? |
|
You'd have to make it available as an input, forgot that bit. So you could add |
|
The output path of the outer derivation will still change, right? And if that's used as a dependency by lots of other packages, you might still be looking at a lot of rebuilds. |
|
Right, that's why the next step is the An illustration might help: Suppose
This way, even though the inner evaluation is done at realisation time (step 1), the dependency graph is rewritten as if it had been done as part of the initial evaluation, and so changes in nix expression dependencies that don't affect the path of the On top of this machinery, |
|
@edolstra ping? |
|
@edolstra I would really like to bring this up-to-date and get |
|
Guessing this is not wanted. |
|
:( |
|
I'm bummed to see this PR end this way, given the effort that went into it, in terms of both code/commits and communication. I can't speak for @shlevy, but I know that I'd much rather be told that my ideas are poorly conceived (and the reasons why) than feel left hanging. From a marketing standpoint, such silence suggests that involvement in this project is risky business - you might invest your time in some new feature, only to either abandon it or decide to fork nix/nixpkgs (if you do the latter, you're now encumbered with maintaining the fork, and it just might turn out that it was a bad idea anyway - but you could hardly be blamed, as no one told you why). @edolstra: I really, really want to see nix grow, because in purely subjective non-technical terms, it's absolutely fucking awesome. What can we - the community - do to improve communication? |
|
I agree with @cstrahan. Probably better to discuss it in mailing list? |
|
Is it worth leaving this open until the discussion is resolved? Seems like a pity for this to get lost. |
|
This is something I really want. Can we reopen the PR? I don't expect @shlevy will want to take care of bringing it up to date, but I (or somebody else) might. |
|
I suppose I can take the same branch and open a new PR for it from my own fork. Would be nice to retain the comment history though. |
|
@copumpkin I've added you as a collab on my fork so you can just reuse this PR. Please also check out my |
|
This is sad and unhealthy situation. Really demotivates to contribute. |
|
@nmikhailov I definitely agree about the organizational aspects of what you're saying, but on the specifics of this PR, not all is lost! Perhaps @shlevy can elaborate on what aspects of recursive nix still need to exist after the new primop, and someone (maybe me, except I never have free time) can do the work to adapt this to reflect it. |
|
@copumpkin Well that's nice to hear regardless of general organizational issues. |
|
I've tried quite hard over the last few days to resurrect this branch and make it mergeable with 2017's HEAD. Unfortunately there seems to just be too much churn in The best path forward might be to use this PR as an inspiration for a re-implementation. |
Convert the remaining toplevel files
…recursion-v2 Fix infinite recursion detection
Folds the findings from E1-E8, F1-F3, B1-B3, C1, and V1-V5 into the candidate sections as in-place body edits. Each affected candidate now carries a See also: line pointing at the verifying report(s); counts are re-derived where the V-pass produced a verified number; mechanism corrections from source-level investigations replace inherited framing. Substantive corrections to existing candidates: - NixOS#134 / NixOS#135 / NixOS#138 (15-globals-settings.md): - B1 found that the catalog's "NixOS#136 keystone for NixOS#134/NixOS#135/NixOS#137/ NixOS#144/NixOS#149" chain is wrong about NixOS#135. The body's keystone block now lists only NixOS#134/NixOS#137/NixOS#144/NixOS#149 as NixOS#136-blocked; NixOS#135 is independent. The reasoning is preserved in NixOS#134's and NixOS#135's bodies (asymmetry between virtual-base diamond and inline-member flattening). - V1 found that 5 of 6 C-API libraries install_headers with their *_internal.h(h) files, exposing layout to C++ consumers. The NixOS#138 body now spells out the C++ ABI caveat for deleting the dead nix::ref<bool> readOnlyMode field, with a release-note requirement. - F1+V3 confirmed (via end-to-end source trace) a latent UAF in the _new-without-_load path: the heap bool is destroyed by nix_eval_state_builder_free while the resulting EvalState still holds a bool * to it. NixOS#138's body now folds the UAF repair into the readOnlyMode -> Store migration, so deleting the dangling field sweeps the bug away in the same change. - NixOS#65 / NixOS#211 (08-duplicated-parsers.md, 21-eval-core-evalstate-value.md): - B2 verified by source reading that NixOS#65 has 79 canonical-template sites plus ~135 bespoke. NixOS#211 has 7 structurally-shared bodies, not 10 and not 16: forceAttrs/forceList use withTrace (a different shape) and forceString(NixStringContext &, ...) / forceStringNoCtx delegate to the single-overload forceString. The catalog's proposed merge of NixOS#65+NixOS#211 was rejected on different-risk-profile and different-consumer-scope grounds. NixOS#211 first (~1 week, internal); NixOS#65 second (~4-6 weeks, user-visible trace migration). - NixOS#213 (21-eval-core-evalstate-value.md): - E8+V4 recommend Direction A on four non-perf grounds. V4 downgraded the unsupported "negligible perf benefit" framing and surfaced a critical observation: ExprOpEq::eval evaluates into stack-local Value v1/v2 and ExprVar::eval ends with v = *v2; — the highest- frequency eqValues caller cannot benefit from canonical pointer- equality regardless of direction. - NixOS#18 (03-repeated-boilerplate.md): - B3 verified the hand-written CloneableError derivation count is 17 (catalog said 14). 18a (the MakeError macro itself) is not a real refactor target because MakeError already is CRTP sugar. 18b is per-class because most carry intrinsic payload. 18c is too small for top-level status. Body now documents the SQLiteBusy/SQLiteError cross-link and the BadNixStringContextElem/GitError err.msg- mutation fragility. - NixOS#166 (17-cross-cutting.md): - E7 verified per-file unique-header counts: 8 Source, 5 Sink, 20 Store, 12 EvalState, 7 Value, 6 StorePath, 10 StoreDirConfig. Recommendation: ship serialise-fwd.hh AND store-fwd.hh AND eval-fwd.hh together. Self-forward-decl caveat: don't replace the inline self-decls in store-api.hh and eval.hh. Zero C-API impact. - NixOS#125 (14-vestigial-stdlib.md): - E4+V2+F2 verified the wire-crossing scope. Daemon does NOT link libexpr; libexpr's 107 throws are local-only. Wire-crossing surface is libstore (199 throws, 71 positional %N%, 51 reachable) plus libutil (281 all-format, 50 positional, 45 reachable). V2 found 5 categories of sites E4's grep missed including the load-bearing store-api.hh::Store::requireStoreObjectAccessor. F2 found a second wire-crossing text-grep contract: "is not valid" in BasicClientConnection::queryPathInfo. Phasing constraint: text-grep contracts span both %s and %1% sites; Phase 2 cannot batch positional and non-positional separately for any contract-bearing site. - NixOS#128 (14-vestigial-stdlib.md): - Note added per CLAUDE.md: the landed cleanup/libmain-shared branch bundled this with the unrelated blockInt deletion (NixOS#49). Soft style issue; recorded for future cleanup-branch authors. - NixOS#140 (15-globals-settings.md): - E1's verification cleared the deletion path. nm -m on the built dylib confirms internal linkage; lix already deleted it; commit 50912d0 (2022-03-31) intended full removal but missed the namespace-scope definition. - N41 (24-test-infrastructure-mirrors.md): - V5 verified each fixture body. 28 declaring fixture classes (E6's "22" was an undercount). 19 fit cleanly with subdir-only ctor; 27 of 28 fit with (subdir, suffix) ctor pair. One structural hazard: CaDerivationAdvancedAttrsTest reassigns unitTestData in SetUp() and is linked to DerivationAdvancedAttrsTest via TYPED_TEST_SUITE — needs common abstract base. New candidate NixOS#218 (12-libutil-libstore-core-extras.md): - The Hash::dummy / StorePath::dummy sentinels are overloaded as both "uninitialised placeholder" and "wire-marker for missing". Independent grep identified 16 sites in three classes: placeholder-then-overwrite (6, including two FIXME-tagged sites in nar-info.cc), wire-marker (3 in worker-protocol.cc, serve-protocol.cc, legacy-ssh-store.cc), and test-fixture (3 + one legitimate constant). The right factoring is std::optional<Hash> / std::optional<StorePath> for the placeholder sites and a separately-named sentinel for the wire-marker sites. Resumption documentation: - INVENTORY.md was missing pointers to STATUS.md, AGENT-CHARTER.md, and review/00-INDEX.md. Added a paragraph after the "Consolidated duplication and refactoring candidates" section documenting all three plus the five-step read-first ordering for resumption. - INVENTORY.md updated: 217 per-shard candidates -> 218 (with NixOS#218 added during integration). - STATUS.md cleaned up: dropped the 13 stale "Integration" sub-bullets (those tasks are now in candidate bodies), collapsed the duplicate "E1 implementation" entries, fixed the vague "completed in commit ahead of HEAD~0" line, added "E3 implementation" and "E5 verifiable-counts manifest" to the queue. "Recently completed" annotated to note the post-integration count is 218 + 44. - review/00-INDEX.md updated with an integration-status note above the "How a new agent should pick a report to read" section. Self-check against AGENT-CHARTER.md eight rules: 1. Call graphs walked: V3's UAF chain through ~ProgressBar / ~InterruptCallbackImpl was already in V3's report; B1's addSetting(this) analysis already in B1's report. 2. Shipping artefacts traced: V1's install_headers finding cited in NixOS#134 and NixOS#138 bodies. 3. Counts cited their source: re-derived for the dummy pattern (16 sites) and CloneableError derivations (17); took V-counts where verified (V2's 71/281, V5's 28/19/27/1). 4. Predictions labelled: N41's V5-verified replaces E6's prediction. 5. Inherited claims re-derived: B3 said 22 hand-written CloneableError; my grep produced 17; used 17. 6. Same-shape walked individually: the dummy pattern split into 3 classes (placeholder vs wire-marker vs test) rather than C1's single shape. 7. No invented numbers: V4's downgrade applied to NixOS#213. 8. Comments verified vs code: NixOS#213's "not a singleton" comment treated as hypothesis the consumers contradict. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This PR makes it possible to run nix from within a nix build. This is useful for things like using nix as a low level build tool, and is usable in places where import-from-derivation isn't (see #13 for more details).
This is done by introducing a new remote mode for nix. If
NIX_REMOTEisrecursive, then the remote client (e.g.RemoteStoreorguix) should do the following:NIX_REMOTE_RECURSIVE_PROTOCOL_VERSIONas the ASCII representation of an unsigned int. This is the version of the remote protocol in use by the parent nix process (currently0x101)NIX_REMOTE_SOCKET_FDas the ASCII representation of a file descriptor. This is a writable unix domain socket (note that this can't be used as the daemon socket directly as multiple processes may use the same fd).socketpair) in the ancillary data to the fd found inNIX_REMOTE_RECURSIVE_SOCKET_FD. The socket that the sent socket is connected to can then be treated as a freshly-connected socket for the daemon protocol.NIX_REMOTE_RECURSIVE_PATHS_FDas the ASCII representation of a file descriptor. This file descriptor should be used to tell the parent nix process of new inputs that it should scan for when looking for references in this build's outputs (which is needed since recursive nix can make store paths that aren't inputs to the build accessible to it, which is really the whole point). Any paths so reported are also guaranteed not to be garbage collected until after the reference scan. Before appending (the fd is opened withO_APPEND), you must take anfcntlwrite lock on the file. The file format is just a sequential series of null-terminated store paths. TheRemoteStoreimplementation collects every newly-discovered path inPathSetthroughout its lifetime, then in the destructor reports them all at once to avoid holding the lock for a long time, but that isn't necessary.And then the daemon protocol is followed as usual. This required several changes on the part of the parent nix process:
nix-daemonalso creates a pair of unix sockets to receive recursive nix connections on. It passes the write end of the pair to all childrenLocalStoreinstances.LocalStoresetsNIX_STORE_DIRand friends to the values appropriate for this store instance,packs thesettingsinto_NIX_OPTIONS(seeSettings::packandSettings::unpack), and setsNIX_REMOTEtorecursiveand puts the protocol version inNIX_REMOTE_RECURSIVE_PROTOCOL_VERSION. It also creates a new temproot (named after the derivation file for guaranteed uniqueness) and puts that intoNIX_REMOTE_RECURSIVE_PATHS_FD. If this is the first build, it then checks if it was passed a socket upon construction. If it was, that means this is a child of anix-daemonprocess and it passes that socket inNIX_REMOTE_RECURSIVE_SOCKET_FD. If it wasn't, then it creates a socket pair and a pipe, puts both ends of the socket and the read end of the pipe intoNIX_RECURSIVE_FDS(space separated) and startsnix-daemon. WhenNIX_RECURSIVE_FDSis set,nix-daemondoes not listen on its traditional socket and instead uses the passed fds to listen for recursive nix connections. The read end of the pipe is used bynix-daemonto know when the parent process has died so it knows to exit. The parentLocalStorethen closes the read end of the pipe and socket pair, purposefully leaks the write end of the pipe (though it isO_CLOEXEC), and uses the write end of the socket pair asNIX_REMOTE_RECURSIVE_SOCKET_FDfor all builds.LocalStorereads the temproot for that build, adds the closure of each path written there (including outputs if any are derivations) to its temproots and to thePathSetused when scanning for references, and then deletes the temproot.cc @civodul for
guixFixes #13