Repository navigation
Description
rpc.Session gives a caller no way to set environment variables for the Nix worker, and the forkserver's own design means the obvious workaround does not work either.
_pool.py:269-272 states the constraint directly:
A partial rather than a closure: grpclib_transports pickles the factory through the forkserver, so it has to be a module-level function plus picklable arguments. An environment variable would not arrive at all — the forkserver copies the environment once, when it starts, and reuses that copy for every child.
So os.environ[...] = ... before constructing a Session is not reliable: if the multiprocessing forkserver singleton has already started (for any earlier session in the process), the new value never reaches the worker.
Why it matters
Nix reads NIX_SSHOPTS from the environment of the process that spawns ssh. For an ssh-ng:// store that process is the worker. Without per-session environment, an rpc caller cannot:
- reach a host through a bastion —
-o ProxyCommand=...and-o ProxyJump=...have no store-URI equivalent, unlikessh-key,base64-ssh-public-host-keyandcompress; - vary any SSH option per target within one process.
The concrete case is ansinix, which copies closures to Ansible-managed hosts. Ansible expresses exactly this through ansible_ssh_common_args / ansible_ssh_extra_args, and bastions are common in the fleets it targets. One controller process talks to many hosts with different SSH options, so a process-global environment cannot express it.
More generally this affects anything the worker reads from its environment — SSL_CERT_FILE, NIX_PATH, proxy variables, TMPDIR.
Suggested remediation
There is already a precedent for the mechanism. rpc/worker/_worker.py:168 sets a variable in the worker from the init message:
os.environ[NIX_USER_CONF_FILES_ENV] = message.nix_confGeneralising that would be a small change:
Session(store_uri="ssh-ng://web01", env={"NIX_SSHOPTS": "-o ProxyCommand=..."})carried on the init message and applied in the worker before Nix initialises. Points worth deciding:
- merge or replace the inherited environment (merge seems right, with the explicit mapping winning);
- whether to reject names that would contradict settings nanopynix already owns,
NIX_USER_CONF_FILESin particular; - whether it is settable only at construction, or live per-store — Nix reads
NIX_SSHOPTSwhen it opens the connection, so construction-time may be sufficient.
Note
inproc.Session does not have this problem: Nix runs in the calling process, so os.environ is simply ours. ansinix works around the issue today by using inproc for the SSH-facing half — but that is only available because that half happens to run in a short-lived forked process where mutating os.environ is harmless. It isn't a general answer, and it forces an engine choice for a reason that has nothing to do with the engines.
Related: the fork-safety issue filed alongside this one.
Happy to send a PR if the shape looks right.
Activity
Lillecarl commentedon Aug 6, 2026
The init message cannot carry every variable, and the precedent above is the proof
The remediation in this issue puts
envon the init message and applies it inthe worker before Nix initialises. That is right for
NIX_SSHOPTS, which Nixreads when it opens the SSH connection, long after
initLibStore.It cannot work for a variable that Nix reads while the library loads, and
the line this issue quotes as the precedent is exactly such a case:
That line is a no-op on Nix 2.31. Three measurements, made while fixing #125:
1. 2.31 reads the variable at static initialisation.
libstore/globals.cc:settingsis a global, so the constructor runs whenlibnixstoreloads.loadConfFilethen reads the storedsettings.nixUserConfFilesat:125andnever looks at the environment again. 2.34 moved the read behind a function
with a local static that
loadConfFilecalls (:143,:163-167), so on 2.34the same line works.
Measured, one key, the same file, only the linked Nix different:
2. The library loads in the forkserver, not in the child. The default
preload is
nanopynix.rpc.worker._worker, and importing it importsnanopynix_bindingsand every_extmodule. Solibnixstoreis loaded, andits static initialisation is done, before any worker child exists.
3. The environment of the forkserver is frozen at its start, silently.
So there are two tiers, and they need different primitives
Tier 1 — Nix reads it after
initLibStore.NIX_SSHOPTS, the proxyvariables,
TMPDIR. The init message serves these, and this issue as writtencovers them.
Tier 2 — Nix reads it while
libnixstoreloads.NIX_USER_CONF_FILESon2.31 today, and any future one. No message can serve these, because the
library is already loaded when the message arrives. Only two shapes work:
them itself and can apply its environment first. This costs whatever the
preload buys, which
DEFAULT_WORKER_PRELOADsays is measured.multiprocessingsingleton, so theenvironment is set before the interpreter of the child starts.
A third option is to accept that tier 2 is process-global and say so in the
API, rather than let a caller pass a variable that quietly does nothing. That
is the smallest change, and it is honest.
Whatever the choice, a name that cannot take effect must be refused rather
than ignored. This issue already asks whether to reject the names nanopynix
owns; the measurement above turns that from a preference into a correctness
rule, because the current failure is silent in every case.
tests/support/nix_markers.py::NIX_CONF_FILE_IGNOREDcarries the same sourcecitations, and it skips the three #125 tests that a 2.31 worker cannot satisfy.
feat(nanopynix): give a session an environment, and refuse what it ca…