8000 ekn _yamlToJson crashes with pygit2/OpenSSL error inside Nix build sandboxes · Issue #91 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

ekn _yamlToJson crashes with pygit2/OpenSSL error inside Nix build sandboxes #91

Closed

Description

@Lillecarl

ekn _yamlToJson crashes with a pygit2/OpenSSL error inside Nix build sandboxes

Symptom

Any Nix build that shells out to ekn _yamlToJson (or, by extension, any
other ekn CLI subcommand invoked as a cold subprocess rather than
through ekn's own long-lived worker process) fails immediately with:

Traceback (most recent call last):
  File ".../ekn-env/lib/python3.14/site-packages/ekn/cli.py", line 13, in <module>
    import pygit2
  File ".../ekn-env/lib/python3.14/site-packages/pygit2/__init__.py", line 570, in <module>
    settings = Settings()
  File ".../ekn-env/lib/python3.14/site-packages/pygit2/settings.py", line 60, in __init__
    self._initialize_tls_certificate_locations()
  File ".../ekn-env/lib/python3.14/site-packages/pygit2/settings.py", line 66, in _initialize_tls_certificate_locations
    self.set_ssl_cert_locations(
        self._default_tls_verify_paths.cafile,
        self._default_tls_verify_paths.capath,
    )
  File ".../ekn-env/lib/python3.14/site-packages/pygit2/settings.py", line 216, in set_ssl_cert_locations
    option(Option.SET_SSL_CERT_LOCATIONS, cert_file, cert_dir)
  File ".../ekn-env/lib/python3.14/site-packages/pygit2/options.py", line 437, in option
    check_error(err)
  File ".../ekn-env/lib/python3.14/site-packages/pygit2/errors.py", line 67, in check_error
    raise GitError(message)
_pygit2.GitError: OpenSSL error: failed to load certificates: error:00000000:lib(0)::reason(0)

This crashes the whole process before any subcommand-specific code runs --
_yamlToJson never even gets a chance to parse its stdin.

Where it actually bites (confirmed empirically, not guessed)

  • Does NOT happen running ekn normally, interactively, through
    direnv exec . pynix ekn <subcommand> (validate, deploy, eval,
    etc.) -- confirmed working repeatedly in a real solid-kubernetes session,
    including against modules that use importyaml with git-fetched CRDs
    (Cilium).
  • DOES happen whenever a plain nix build/nix eval invocation (no
    ekn worker attached -- e.g. an ad-hoc nix eval --file . <attr> from a
    terminal, or anything else that forces an easykubenix attribute without
    going through ekn's own CLI entrypoint) needs to render a module that
    uses importyaml. Reproduced directly by building
    nix/modules/gateway-api's CRD import and separately
    nix/modules/cilium's, both via nix build --impure --expr '...' in
    solid-kubernetes -- same crash, same yaml2json.drv build.

Root cause

easykubenix/easykubenix/lib/parseYamlStream.nix has two code paths for
turning a YAML document stream into JSON:

if builtins ? ${streamBuiltin} then
  builtins.${streamBuiltin} (builtins.readFile src)          # in-process primop
else
  lib.importJSON (
    pkgs.runCommand "yaml2json" { nativeBuildInputs = [ eknPackage ]; } ''
      ekn _yamlToJson --yaml-version ${yamlVersion} < ${src} >$out
    ''
  )

The first branch (builtins.fromYAML11Stream/fromYAMLStream) is a
nanopynix primop, only registered when Nix is evaluated through ekn's own
worker
(ekn.lib module arg, _module.args.eknPackage, etc.) -- that's
why interactive ekn usage never hits this bug: it always takes this
branch, never touches _yamlToJson at all.

The second branch is the fallback for any other evaluator (plain nix build/nix eval, or anything else that doesn't register those primops).
It shells out to ekn _yamlToJson inside a runCommand derivation --
i.e. a hermetic Nix build sandbox, not the calling shell's environment.

ekn/src/ekn/cli.py does import pygit2 unconditionally at module load
(line 13, alongside every other CLI dependency), regardless of which
subcommand is actually being invoked. pygit2's own Settings.__init__
(triggered at import time, not lazily) calls
_initialize_tls_certificate_locations(), which asks Python's ssl
module for the platform's default CA bundle paths and hands them to
libgit2 via set_ssl_cert_locations. Inside a Nix build sandbox there is
no /etc/ssl/certs (or wherever OpenSSL's compiled-in defaults point) --
the sandbox has no ambient trust store at all unless a derivation
explicitly provides one.

The yaml2json derivation's nativeBuildInputs = [ eknPackage ] does
not include cacert (nixpkgs' CA-bundle package), and nothing else in
that derivation sets SSL_CERT_FILE/SSL_CERT_DIR/NIX_SSL_CERT_FILE.
Every other Nix derivation that needs to hit the network and solves this
correctly (i.e. essentially every real network fetcher in nixpkgs) either
adds cacert to nativeBuildInputs (its setup hook exports
SSL_CERT_FILE) or sets those env vars directly. This derivation does
neither, so ssl.get_default_verify_paths() resolves to paths that don't
exist in the sandbox, and libgit2's OpenSSL binding fails outright with
the generic/empty error:00000000:lib(0)::reason(0) (characteristic of
"the cafile I was told to open doesn't exist").

Two compounding factors make this worse than it needs to be:

  1. The crash has nothing to do with git. _yamlToJson is pure YAML
    parsing -- no git object is ever touched. The only reason it's affected
    at all is that cli.py imports pygit2 unconditionally at the top of
    the file for other subcommands (git-based GitOps commit/push), so
    every subcommand pays the cost of that import succeeding, including
    ones that will never call into git.
  2. It fails at import time, not at first use. Even a subcommand that
    would gracefully handle "no git repo here" never gets the chance --
    the process dies during import ekn.cli, before main() even starts
    dispatching to a subcommand handler.

Suggested fixes (pick one or combine; not prescribing)

  1. Make pygit2 a lazy import in cli.py. Move import pygit2 out
    of the module-level imports and into whichever command handler(s)
    actually need it (the git-based GitOps commit/push path). This is the
    most surgical fix: _yamlToJson/_jsonToYAML and any other
    non-git subcommand stop depending on pygit2's import-time behavior
    entirely, and the fix has zero effect on interactive ekn usage (which
    never had this problem anyway).
  2. Give the yaml2json derivation a real trust store. Add cacert to
    parseYamlStream.nix's runCommand's nativeBuildInputs (or set
    SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; directly in
    its environment), matching the standard nixpkgs pattern for any
    derivation that might touch OpenSSL/libgit2/curl. Fixes the symptom
    without touching cli.py, but every other place ekn gets shelled out
    to from inside a build sandbox (if any exist beyond this one call site)
    would need the same treatment individually.
  3. Defensive fallback in pygit2 settings init (upstream-adjacent, more
    invasive): wrap _initialize_tls_certificate_locations()'s failure
    mode so a missing default cert path doesn't raise -- not something to
    fix in this codebase directly (it's pygit2's own behavior), but worth
    knowing this is a known rough edge of pygit2 in minimal/sandboxed
    environments generally, independent of anything ekn-specific.

(1) is probably the right primary fix -- it's a correctness issue on its
own (a YAML-parsing subcommand should not transitively require a working
git/SSL stack to run), and (2) is a reasonable belt-and-suspenders addition
regardless, since the same derivation shape could resurface anywhere else
ekn gets shelled out to from inside a sandboxed build in the future.

Repro recipe

From a solid-kubernetes checkout with direnv loaded:

nix build --impure -L --no-link --expr '
  let pkgs = (import ./default.nix {}).environments.nixlab.passthru.pkgs;
  in pkgs.fetchHelm {
    chart = "backstage";
    repo = "https://redhat-developer.github.io/rhdh-chart";
    version = "7.0.1";
    sha256 = pkgs.lib.fakeSha256;
  }
'

(This particular command fails on the hash, not this bug -- included
only to show the harness; swap in any attribute that forces
kubernetes.resources/kubernetes.objects for a module using
importyaml, e.g. nix/modules/gateway-api or nix/modules/cilium, via a
plain nix eval --file . environments.nixlab.config.kubernetes.resources
with no ekn worker attached, to hit the actual crash.)

Why this matters beyond one annoying error message

Right now, the only thing that makes importyaml-based modules "work" at
all is that real usage always goes through ekn's own worker (which never
takes the _yamlToJson fallback path). That's fragile: any tooling,
CI step, or ad-hoc debugging session that evaluates an easykubenix
attribute via plain nix build/nix eval -- a completely reasonable thing
to want to do, e.g. to inspect rendered output without a full ekn
invocation -- silently can't, for a reason that has nothing to do with
what it's actually trying to do (parse YAML). Fixing this makes the
fallback path actually usable, not just theoretically present.

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