From af32a8dd4b33ede30811c1a6d58b2076d87646c8 Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Mon, 23 Nov 2026 10:03:02 +0000 Subject: [PATCH] lib/json-c: Work around the 0.19 memory leak In json-c 0.19, json_object_put calls the function that frees detached JSON objects inside assert(). Release builds with NDEBUG skip that call. Sway's frequent IPC replies then leak memory, eventually making workspace switches lag. Patching json-c in the main package set changes the library's store path and forces dependent packages, including Firefox and Chromium, to rebuild. Keep the pin with SPDK 26.09 and build the ABI-compatible fix separately. Rewrite runtime references in NixOS systems and standalone Home Manager generations to reuse their existing binaries. Drop the replacement when the pinned nixpkgs includes the fix. https://github.com/NixOS/nixpkgs/pull/586077 --- home-manager/modules/common.nix | 18 +++++++++++++++++- lib/json-c-runtime.nix | 13 +++++++++++++ nixos/modules/common.nix | 6 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/json-c-runtime.nix diff --git a/home-manager/modules/common.nix b/home-manager/modules/common.nix index adc275f..e2d1298 100644 --- a/home-manager/modules/common.nix +++ b/home-manager/modules/common.nix @@ -2,10 +2,12 @@ let inherit (builtins) listToAttrs mapAttrs readFile; inherit (lib) - optionalString nameValuePair concatMapStrings concatStringsSep optionalAttrs + optionalString nameValuePair concatMapStrings concatStringsSep mapAttrsToList mkMerge mkIf mkDefault mkOption; inherit (lib.hm) dag; inherit (lib.my) mkOpt' dummyOption; + + jsonCRuntimeReplacements = import ../../lib/json-c-runtime.nix { inherit lib pkgs pkgsFlake; }; in { # Disabled: breaks `nix flake check` in CI (see flake.nix pi-agent note). @@ -29,6 +31,20 @@ in }; }; }; + + home.activationPackage = mkOption { + apply = drv: + if config.my.isStandalone && jsonCRuntimeReplacements != [ ] then + (pkgs.replaceDependencies.override { + replaceDirectDependencies = pkgs.replaceDirectDependencies.override { + nix = config.nix.package; + }; + }) { + inherit drv; + replacements = jsonCRuntimeReplacements; + } + else drv; + }; }; config = mkMerge [ diff --git a/lib/json-c-runtime.nix b/lib/json-c-runtime.nix new file mode 100644 index 0000000..492e340 --- /dev/null +++ b/lib/json-c-runtime.nix @@ -0,0 +1,13 @@ +{ lib, pkgs, pkgsFlake }: + +# TODO: Drop this when updating the fork pin to a revision with the json-c fix. +# Patch the runtime library separately so existing consumers can be reused. +lib.optional ((pkgsFlake.rev or null) == "320600d55ff67365e933b98b339acdeac238eb26") { + oldDependency = pkgs.json_c; + newDependency = pkgs.json_c.overrideAttrs (old: { + patches = (old.patches or [ ]) ++ [ (pkgs.fetchpatch { + url = "https://github.com/json-c/json-c/commit/f291fa81c6f21d925ea771a8cca597f5886309cc.patch"; + hash = "sha256-ZffSw1pfEfOH2ylRpJShdx7sTQR2nmPM/JIj/26UrDM="; + }) ]; + }); +} diff --git a/nixos/modules/common.nix b/nixos/modules/common.nix index 8f9f4ba..d397bb4 100644 --- a/nixos/modules/common.nix +++ b/nixos/modules/common.nix @@ -2,6 +2,8 @@ let inherit (lib) mkIf mkDefault mkMerge optionalAttrs versionOlder; inherit (lib.my) mkDefault'; + + jsonCRuntimeReplacements = import ../../lib/json-c-runtime.nix { inherit lib pkgs pkgsFlake; }; in { options = with lib.types; { @@ -18,6 +20,10 @@ in ]; config = mkMerge [ + (optionalAttrs (jsonCRuntimeReplacements != [ ]) { + system.replaceDependencies.replacements = jsonCRuntimeReplacements; + }) + { system = { stateVersion = "22.05";