opencode: support ordered settings entries

Use the shared DAG-aware JSON generator for programs.opencode.settings so order-sensitive permission rules can be expressed with lib.hm.dag entries.

Add a focused NMT test for last-match-wins permission output and news for existing opencode users.
This commit is contained in:
Austin Horstman
2026-06-18 07:59:18 -05:00
parent 6716d811e5
commit b9a29e51c5
5 changed files with 49 additions and 1 deletions
@@ -0,0 +1,12 @@
{ config, ... }:
{
time = "2026-06-12T12:00:00+00:00";
condition = config.programs.opencode.enable;
message = ''
The 'programs.opencode.settings' option now supports ordered Home Manager
DAG entries in nested attribute sets.
This allows order-sensitive OpenCode permission rules to be expressed with
'lib.hm.dag.entryBefore' and 'lib.hm.dag.entryAfter'.
'';
}
+9 -1
View File
@@ -17,6 +17,9 @@ let
webCfg = cfg.web;
jsonFormat = pkgs.formats.json { };
orderedJsonFormat = lib.hm.generators.mkDAGOrderedJsonFormat {
inherit pkgs jsonFormat;
};
toOpencodeShape =
s:
@@ -116,6 +119,11 @@ in
Configuration written to {file}`$XDG_CONFIG_HOME/opencode/opencode.json`.
See <https://opencode.ai/docs/config/> for the documentation.
Attribute sets containing ordered `lib.hm.dag.entryBefore` or
`lib.hm.dag.entryAfter` values are rendered in topological order, with
raw sibling values treated as unordered entries. This is useful for
OpenCode permission rules, where the last matching rule wins.
Note, `"$schema": "https://opencode.ai/config.json"` is automatically added to the configuration.
'';
};
@@ -485,7 +493,7 @@ in
mergedSettings =
cfg.settings // (lib.optionalAttrs (mergedMcpServers != { }) { mcp = mergedMcpServers; });
in
jsonFormat.generate "opencode.json" (
orderedJsonFormat.generate "opencode.json" (
{
"$schema" = "https://opencode.ai/config.json";
}
@@ -1,5 +1,6 @@
{
opencode-settings = ./settings.nix;
opencode-settings-ordered-permissions = ./settings-ordered-permissions.nix;
opencode-empty-settings = ./empty-settings.nix;
opencode-context-inline = ./rules-inline.nix;
opencode-context-path = ./rules-path.nix;
@@ -0,0 +1,9 @@
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"bash": {
"zz *": "ask",
"aa *": "allow"
}
}
}
@@ -0,0 +1,18 @@
{ lib, ... }:
{
programs.opencode = {
enable = true;
settings = {
permission.bash = {
"aa *" = lib.hm.dag.entryAfter [ "zz *" ] "allow";
"zz *" = "ask";
};
};
};
nmt.script = ''
assertFileExists home-files/.config/opencode/opencode.json
assertFileContent home-files/.config/opencode/opencode.json \
${./settings-ordered-permissions.json}
'';
}