Featured image of post Patching a Critical Ruby CVE on Production GitLab, the NixOS Way

Patching a Critical Ruby CVE on Production GitLab, the NixOS Way

When a critical Ruby CVE dropped and nixpkgs hadn't landed the fix yet, a NixOS overlay patched our self-hosted GitLab in minutes — no forking, no snowflake servers.

A critical Ruby CVE dropped last night: ruby2shell (GHSA-34m4-m9fh-gqp5). A one-byte heap overflow in Ruby’s string case conversion, reachable without authentication through Rails. My self-hosted GitLab was running Ruby 3.4.12, squarely in the affected range.

The upstream fix was already public: ruby/ruby@d582f8f. The problem was that nixpkgs hadn’t landed it yet. On a traditional system, you’d be waiting on your distro’s security team, manually applying patches, or making a judgment call about exposure.

On NixOS, I didn’t wait.

The Overlay

A nixpkgs overlay lets you override any package derivation without forking the entire package set. You inject the override directly into your flake, and the rest of your config stays untouched. For a security patch, it’s the right tool: minimal blast radius, temporary by nature, and committed to Git with a clear reason.

The addition to our flake:

overlays.default = final: prev: {
  ruby_3_4 = prev.ruby_3_4.overrideAttrs (old: {
    patches = (old.patches or []) ++ [
      (prev.fetchpatch {
        name = "CVE-ruby2shell.patch";
        url = "https://github.com/ruby/ruby/commit/d582f8f.patch";
        hash = "sha256-bX0XRsGSnoLgfccBtWM5t6SBXjVqir3+0fHDBk6Iej0=";
      })
    ];
  });
};

overrideAttrs lets you modify an existing derivation’s attributes. Here we’re appending to the existing patches list rather than replacing it, so any patches nixpkgs already applies stay intact. The fetchpatch call pulls the upstream commit diff directly from GitHub and verifies it against a fixed sha256 hash. If GitHub ever serves a different file at that URL, the build fails. No silent substitution.

Getting the Hash

The hash isn’t something you guess. You run this once to find it:

nix store prefetch-file --hash-type sha256 \
  'https://github.com/ruby/ruby/commit/d582f8f.patch'

Nix fetches the file, prints the hash, and stores it locally so the build doesn’t have to re-download it. Paste the hash into the overlay and you’re done.

Deploying with Colmena

I manage my servers with Colmena, a NixOS deployment tool built on top of flakes. With the overlay in place:

colmena --impure apply --on gitlab switch

Colmena builds the new Ruby derivation with the patch applied, pushes the closure to the target machine, and activates it. No SSH-ing in. No apt install with fingers crossed. No configuration drift between what’s declared and what’s running.

The whole process from “CVE dropped” to “patched and running” was about an hour. Most of that was waiting on the build.

Why This Matters

The overlay is explicitly temporary. The commit message says so. The moment nixpkgs ships Ruby 3.4.13 with the fix included, the overlay comes out and nothing else changes. There’s no residue, no forgotten manual patch, no config that “someone did once.”

The Git history tells the full story: a commit adding the overlay with a reference to the CVE, and later a commit removing it once upstream catches up. Any engineer looking at the repo six months from now can see exactly what happened and why.

This is what declarative infrastructure looks like under pressure. Not “I think I patched it.” A reproducible build, an auditable diff, and a rollback that actually works.

If you’re running critical self-hosted services and you’re not on NixOS, the question is worth asking: when the next CVE drops, how long does your response take?

Linux, Scripting, and Self-Hosting Projects
Built with Hugo
Theme Stack designed by Jimmy