From 39012ddac260255a60bbf99777e9cf1ba9bb219b Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 12:24:39 +0000 Subject: layers/nix.nix: fc-drop-caches, for 9P's stale caches 9P with cache=loose (the host's store; /nixpkgs with --nixpkgs-cache=loose, which evaluates three times as fast as without) never asks the host again: a file changed on the host keeps its old size and contents in the guest (nix then reads a truncated aliases.nix), and a name looked up before the host had it stays missing, until the guest drops its cached dentries and inodes, which only root can. fc-drop-caches lets anyone: a setuid wrapper for a program that takes no input and only ever writes 2 to drop_caches. --- layers/nix.nix | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/layers/nix.nix b/layers/nix.nix index b679539..42d4729 100644 --- a/layers/nix.nix +++ b/layers/nix.nix @@ -119,4 +119,30 @@ in noCheck = true; options = [ "ro" "x-systemd.automount" "nofail" ]; }; + + # 9P with cache=loose (the host's store, and /nixpkgs with + # --nixpkgs-cache=loose) never asks the host again: a file changed on + # the host keeps its old size and contents in the guest, and a name + # looked up before the host had it stays missing, until the guest drops + # its cached dentries and inodes, which fc-drop-caches lets anyone do + # (setuid; no input, it only ever writes 2 to drop_caches). + security.wrappers.fc-drop-caches = { + setuid = true; + owner = "root"; + group = "root"; + source = lib.getExe (pkgs.writeCBin "fc-drop-caches" '' + #include + #include + #include + int main(void) { + sync(); + int fd = open("/proc/sys/vm/drop_caches", O_WRONLY); + if (fd < 0 || write(fd, "2\n", 2) != 2) { + perror("fc-drop-caches: /proc/sys/vm/drop_caches"); + return 1; + } + return 0; + } + ''); + }; } -- cgit v1.3.1