From 635b992acd303163bc4d6625b13a337afec04f63 Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Sat, 5 Dec 2026 18:59:50 +0000 Subject: [PATCH] pi: merge declared settings into a persisted settings.json Pi saves fields of its own into ~/.pi/agent/settings.json, such as the deviceId it sends OpenAI when signing in with ChatGPT and defaults saved from the model picker. The file was a home-manager link into the Nix store, so every save failed silently and Pi made a new deviceId for every login. ogygia-nix-fixer copied its settings in only when the file was missing, so changes to them never reached an existing install. Added pi-merge-settings, which replaces each top-level field the Nix config declares, keeps every other field, and logs the path of each value the replacement drops without the value. The home module persists settings.json and runs it on every home-manager activation, so the log lands in the journal under home-manager-jake. The fixer runs it in preStart in place of its one-time copy. An AGENTS.md tells Pi where its configuration comes from, so it proposes changes to this repository rather than editing files a deploy overwrites. Test plan: - CI - Built maverick's and iceman's toplevels - Ran pi-merge-settings on a missing file, an unchanged file, a file with changed declared fields plus a deviceId, and a declared object replaced by a string: it logged only the changed paths and kept deviceId --- flake.nix | 1 + modules/home/pi/AGENTS.md | 14 +++++++++++ modules/home/pi/default.nix | 14 +++++++---- modules/services/ogygia-nix-fixer/default.nix | 6 ++--- pkgs/pi-merge-settings/default.nix | 7 ++++++ pkgs/pi-merge-settings/merge-settings.sh | 24 +++++++++++++++++++ 6 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 modules/home/pi/AGENTS.md create mode 100644 pkgs/pi-merge-settings/default.nix create mode 100644 pkgs/pi-merge-settings/merge-settings.sh diff --git a/flake.nix b/flake.nix index b9dfd11c..438cffbe 100644 --- a/flake.nix +++ b/flake.nix @@ -107,6 +107,7 @@ "piPackages"."pi-web-access" = final.callPackage ./pkgs/pi-web-access.nix { }; "piPackages"."background-subagents" = final.callPackage ./pkgs/pi-background-subagents { }; "piPackages"."loop" = final.callPackage ./pkgs/pi-loop { }; + "piPackages"."merge-settings" = final.callPackage ./pkgs/pi-merge-settings { }; }) ]; mkSystem = import ./lib/mkSystem.nix { inherit inputs; }; diff --git a/modules/home/pi/AGENTS.md b/modules/home/pi/AGENTS.md new file mode 100644 index 00000000..e5ae696c --- /dev/null +++ b/modules/home/pi/AGENTS.md @@ -0,0 +1,14 @@ +# Your configuration + +Your configuration under ~/.pi (settings.json, models.json, agents/, +extensions/, web-search.json and this file) comes from the +JakeHillion/nixos repository, in modules/home/pi/default.nix. Every deploy +replaces the settings.json fields that repository declares, and every other +file in that list, so changes made to them here don't last. + +When asked to change how Pi is configured, or when you notice something in +your own configuration worth changing, don't edit those files. Propose the +change to modules/home/pi/default.nix, and make it in the repository only +when asked, following that repository's own instructions. A choice that +should last only for this session needs no change: use /model, /thinking or +/settings without saving it as a default. diff --git a/modules/home/pi/default.nix b/modules/home/pi/default.nix index 070466f3..3b49dd7c 100644 --- a/modules/home/pi/default.nix +++ b/modules/home/pi/default.nix @@ -51,6 +51,7 @@ let video = { enabled = false; }; pdf = { enabled = false; }; }; + piSettingsFile = pkgs.writeText "pi-settings.json" (builtins.toJSON piSettings); piSettings = { lastChangelogVersion = piPackage.version; defaultProvider = "openai"; @@ -106,6 +107,7 @@ in custom.impermanence = lib.mkIf config.custom.impermanence.enable { userExtraFiles.${user} = [ ".pi/agent/auth.json" + ".pi/agent/settings.json" ".pi/agent/trust.json" ]; userExtraDirs.${user} = [ ".pi/agent/sessions" ]; @@ -120,7 +122,7 @@ in mode = "0400"; }; - home-manager.users.${user} = { + home-manager.users.${user} = { lib, ... }: { home = { packages = [ piPackage ]; sessionVariables = { @@ -130,10 +132,7 @@ in }; home.file = { - ".pi/agent/settings.json" = { - text = builtins.toJSON piSettings; - force = true; - }; + ".pi/agent/AGENTS.md".source = ./AGENTS.md; ".pi/agent/models.json" = { text = builtins.toJSON piModels; force = true; @@ -151,6 +150,11 @@ in force = true; }; }; + + # Pi saves its own fields, such as deviceId, into settings.json + home.activation.pi-settings = lib.hm.dag.entryAfter [ "linkGeneration" ] '' + run ${lib.getExe pkgs.piPackages.merge-settings} ${piSettingsFile} "$(${pkgs.coreutils}/bin/readlink -f "$HOME/.pi/agent/settings.json")" + ''; }; }; } diff --git a/modules/services/ogygia-nix-fixer/default.nix b/modules/services/ogygia-nix-fixer/default.nix index aafce04b..e3b79bae 100644 --- a/modules/services/ogygia-nix-fixer/default.nix +++ b/modules/services/ogygia-nix-fixer/default.nix @@ -181,10 +181,8 @@ in preStart = '' install -d -m 0700 ${home}/.pi/agent ${home}/.config/jj install -d ${home}/.pi/agent/{extensions,agents,skills} ${home}/state - # Seed mutable settings, rather than making Pi's runtime state a store link. - if [ ! -e ${home}/.pi/agent/settings.json ]; then - install -m 0600 ${settings} ${home}/.pi/agent/settings.json - fi + # Pi saves its own fields into settings.json + ${lib.getExe pkgs.piPackages.merge-settings} ${settings} ${home}/.pi/agent/settings.json ln -sfn ${jjConfig} ${home}/.config/jj/config.toml ln -sfn ${pkgs.piPackages.background-subagents} ${home}/.pi/agent/extensions/subagent ln -sfn ${pkgs.piPackages.loop} ${home}/.pi/agent/extensions/loop diff --git a/pkgs/pi-merge-settings/default.nix b/pkgs/pi-merge-settings/default.nix new file mode 100644 index 00000000..7efe4be3 --- /dev/null +++ b/pkgs/pi-merge-settings/default.nix @@ -0,0 +1,7 @@ +{ writeShellApplication, jq }: + +writeShellApplication { + name = "pi-merge-settings"; + runtimeInputs = [ jq ]; + text = builtins.readFile ./merge-settings.sh; +} diff --git a/pkgs/pi-merge-settings/merge-settings.sh b/pkgs/pi-merge-settings/merge-settings.sh new file mode 100644 index 00000000..8c847b9d --- /dev/null +++ b/pkgs/pi-merge-settings/merge-settings.sh @@ -0,0 +1,24 @@ +# Usage: pi-merge-settings DECLARED TARGET +declared=$1 +target=$2 + +current='{}' +if [ -e "$target" ]; then + current=$(<"$target") +fi + +jq -r --slurpfile declared "$declared" ' + def leaves: if type == "object" and length > 0 + then to_entries[] | [.key] + (.value | leaves) + else [] end; + + . as $cur | $declared[0] as $decl + | $decl | keys[] as $k + | select($cur | has($k)) + | $cur[$k] | [leaves][] as $p + | select([$decl[$k] | getpath($p)?] != [$cur[$k] | getpath($p)]) + | "pi settings: replaced \([$k] + $p | join(".")), which NixOS declares" +' <<<"$current" + +jq --slurpfile declared "$declared" '. + $declared[0]' <<<"$current" >"$target.tmp" +mv "$target.tmp" "$target" -- 2.56.1