8000 rpc.Session: no per-session environment passthrough (blocks NIX_SSHOPTS / bastions) · Issue #94 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

rpc.Session: no per-session environment passthrough (blocks NIX_SSHOPTS / bastions) #94

Closed

Description

@Lillecarl

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, unlike ssh-key, base64-ssh-public-host-key and compress;
  • 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_conf

Generalising 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_FILES in particular;
  • whether it is settable only at construction, or live per-store — Nix reads NIX_SSHOPTS when 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

  1. Lillecarl commented on Aug 6, 2026

    @Lillecarl
    ContributorAuthor

    The init message cannot carry every variable, and the precedent above is the proof

    The remediation in this issue puts env on the init message and applies it in
    the worker before Nix initialises. That is right for NIX_SSHOPTS, which Nix
    reads 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:

    os.environ[NIX_USER_CONF_FILES_ENV] = message.nix_conf   # _worker.py:167

    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:

    Settings settings;                                    // :49, namespace scope
        , nixUserConfFiles(getUserConfigFiles())          // :66, member init

    settings is a global, so the constructor runs when libnixstore loads.
    loadConfFile then reads the stored settings.nixUserConfFiles at :125 and
    never looks at the environment again. 2.34 moved the read behind a function
    with a local static that loadConfFile calls (:143, :163-167), so on 2.34
    the same line works.

    Measured, one key, the same file, only the linked Nix different:

    file says `max-jobs = 42`   ->   2.31 reports 4 (the host), 2.34 reports 42
    

    2. The library loads in the forkserver, not in the child. The default
    preload is nanopynix.rpc.worker._worker, and importing it imports
    nanopynix_bindings and every _ext module. So libnixstore is loaded, and
    its static initialisation is done, before any worker child exists.

    3. The environment of the forkserver is frozen at its start, silently.

    1. set before the forkserver started : set-before-first-spawn
    2. changed after it started          : set-before-first-spawn
       (the parent itself now reports      changed-after-first-spawn)
    

    So there are two tiers, and they need different primitives

    Tier 1 — Nix reads it after initLibStore. NIX_SSHOPTS, the proxy
    variables, TMPDIR. The init message serves these, and this issue as written
    covers them.

    Tier 2 — Nix reads it while libnixstore loads. NIX_USER_CONF_FILES on
    2.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:

    • Take the bindings out of the forkserver preload, so each child imports
      them itself and can apply its environment first. This costs whatever the
      preload buys, which DEFAULT_WORKER_PRELOAD says is measured.
    • Own the spawn, instead of the multiprocessing singleton, so the
      environment 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_IGNORED carries the same source
    citations, and it skips the three #125 tests that a 2.31 worker cannot satisfy.

  2. added
    source: nanopynix
    on Oct 19, 2026
  3. added theissue type on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source: nanopynix

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0