Skip to content

fix(ci): scope the gem cache restore to the Nix environment - #54

Merged
jcardozo-eth merged 1 commit into
mainfrom
fix/ci-gem-cache-per-nix-env
Oct 29, 2026
Merged

jcardozo-eth merged 1 commit into
mainfrom
fix/ci-gem-cache-per-nix-env

Conversation

@jcardozo-eth

@jcardozo-eth jcardozo-eth commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Problem

How CI gets its Ruby gems

  • Each CI job enters the Nix dev shell, which provides Ruby. flake.lock pins the exact nixpkgs revision, and with it the exact Ruby build.
  • The Ruby tools (puppet, rspec-puppet, puppet-lint and their dependencies) are not part of the dev shell. Bundler installs them from Gemfile.lock into vendor/bundle.
  • A few of those gems (syslog, fiddle, bigdecimal) compile a C extension at install time. The compiled file links against the exact Nix store path of the libruby it was built with (and, for fiddle, of libffi).
  • So that each run does not download and compile the whole bundle again, the Puppet jobs cache vendor/bundle with actions/cache.

What goes wrong

  • The cache key hashes flake.lock and Gemfile.lock, so a nixpkgs bump does produce a new key. On a key miss, though, the fallback restore-keys: gems-<os>- matches any earlier bundle, including one built under a different Ruby.
  • bundle install sees every gem as installed and rebuilds nothing.
  • Ruby refuses to load an extension linked to a different libruby, so the job fails as soon as the code loads it.
  • The cache step's comment already said a Ruby bump must miss the cache; the fallback prefix let it hit anyway.

Evidence

  • build(deps): bump nixpkgs from 6fcc160 to 1f607aa in the nix group across 1 directory #52 (nixpkgs bump, Ruby 3.3.10 to 3.3.13) fails both Puppet jobs with Cannot determine basic system flavour.
  • Its run restored a gem cache built under 3.3.10 while the Nix store held only 3.3.13, so the cached syslog extension did not load. Puppet 8.10 needs syslog to detect a POSIX system.
  • The failure reproduces locally: Ruby rejects a stale syslog build as linked to incompatible …libruby-3.3.10.dylib.
  • Ruby 3.3.13 is not at fault: both Linux builds ship syslog.so, and their bundled_gems.rb are identical.

Fix

Both gem cache steps now key on gems-<os>-<hash of the Nix files and flake.lock>-<hash of Gemfile.lock> and restore only within the same Nix hash:

  • A flake change starts a fresh bundle.
  • A Gemfile.lock-only change still reuses the last bundle from the same environment.

Verification

Appendix

CI runs of #52

Commit Ruby in the Nix store Gem cache restored Result
0b09b2c (run, 2026-10-21) 3.3.10 gems-Linux-b0bcc816… (from main) pass; saved gems-Linux-2c06d127…
599114c (run, 2026-10-28) 3.3.13 only gems-Linux-2c06d127… fail

From the failing Unit tests job:

Cache restored from key: gems-Linux-2c06d12787b9a62fb5da6d1865620eb0e5435a31df99acde197ad809b9528669
Puppet::Error:
  Cannot determine basic system flavour
0 examples, 0 failures, 2 errors occurred outside of examples

Why syslog stops Puppet

lib/puppet/feature/base.rb in Puppet 8.10.0 defines posix as !Etc.getpwuid(0).nil? && Puppet.features.syslog? and raises Cannot determine basic system flavour when neither posix nor microsoft_windows holds. The Gemfile pins syslog 0.4.0, a C extension.

Local reproduction (aarch64-darwin)

$ otool -L vendor/bundle/ruby/3.3.0/gems/syslog-0.4.0/lib/syslog_ext.bundle | grep libruby
	/nix/store/xsi1kcglc8y3c3m0qvvpxg2j3jcb7nxa-ruby-3.3.10/lib/libruby-3.3.10.dylib (compatibility version 3.3.0, current version 3.3.10)
$ nix develop --command ruby -e 'print RbConfig::CONFIG["prefix"]'
/nix/store/85w8cldki5is7rrcj9ibkvj8ja9298mb-ruby-3.3.10

Loading the extension fails with linked to incompatible …/libruby-3.3.10.dylib. After bundle pristine rebuilds it, just check passes.

Native gem extensions (syslog, fiddle, bigdecimal) link against the
exact Nix store path of the libruby and libffi they were built with,
and Ruby refuses to load an extension linked to a different libruby.
The restore prefix gems-<os>- matched any earlier bundle, so a nixpkgs
bump that changed the Ruby build restored extensions compiled for the
old one. require "syslog" then failed, and Puppet aborted with "Cannot
determine basic system flavour".

Key the cache on the dev-shell hash (the Nix files and flake.lock) and
Gemfile.lock, and restore only within the same dev-shell hash. A flake
change now always starts a fresh bundle; a Gemfile.lock-only change
still reuses the last bundle built for the same environment.
@jcardozo-eth jcardozo-eth self-assigned this Oct 29, 2026
@jcardozo-eth
jcardozo-eth merged commit 2af79fd into main Oct 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant