From 08edcbe9dfd4a3098f5d097b13d7ce693ba2ad16 Mon Sep 17 00:00:00 2001 From: Thierry Delafontaine Date: Sat, 19 Jul 2025 11:16:38 +0200 Subject: [PATCH] opencode: add support for global custom instructions via `rules` option - Introduce `rules` option to provide global custom instructions for opencode - Write `rules` content to `~/.config/opencode/AGENTS.md` if non-empty - Update tests to cover presence and absence of `AGENTS.md` file with rules content --- modules/programs/opencode.nix | 40 ++++++++++++++++++- tests/modules/programs/opencode/AGENTS.md | 22 ++++++++++ tests/modules/programs/opencode/default.nix | 2 + .../modules/programs/opencode/empty-rules.nix | 9 +++++ tests/modules/programs/opencode/rules.nix | 34 ++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tests/modules/programs/opencode/AGENTS.md create mode 100644 tests/modules/programs/opencode/empty-rules.nix create mode 100644 tests/modules/programs/opencode/rules.nix diff --git a/modules/programs/opencode.nix b/modules/programs/opencode.nix index e3c27cf8..62a72865 100644 --- a/modules/programs/opencode.nix +++ b/modules/programs/opencode.nix @@ -41,13 +41,49 @@ in See for the documentation. ''; }; + rules = lib.mkOption { + type = lib.types.lines; + description = "You can provide global custom instructions to opencode; this value is written to {file}~/.config/opencode/AGENTS.md"; + default = ""; + example = lib.literalExpression '' + ''' + # TypeScript Project Rules + + ## External File Loading + + CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand. + + Instructions: + + - Do NOT preemptively load all references - use lazy loading based on actual need + - When loaded, treat content as mandatory instructions that override defaults + - Follow references recursively when needed + + ## Development Guidelines + + For TypeScript code style and best practices: @docs/typescript-guidelines.md + For React component architecture and hooks patterns: @docs/react-patterns.md + For REST API design and error handling: @docs/api-standards.md + For testing strategies and coverage requirements: @test/testing-guidelines.md + + ## General Guidelines + + Read the following file immediately as it's relevant to all workflows: @rules/general-guidelines.md. + ''' + ''; + }; }; config = mkIf cfg.enable { home.packages = mkIf (cfg.package != null) [ cfg.package ]; - xdg.configFile."opencode/config.json" = mkIf (cfg.settings != { }) { - source = jsonFormat.generate "config.json" cfg.settings; + xdg.configFile = { + "opencode/config.json" = mkIf (cfg.settings != { }) { + source = jsonFormat.generate "config.json" cfg.settings; + }; + "opencode/AGENTS.md" = mkIf (cfg.rules != "") { + text = cfg.rules; + }; }; }; } diff --git a/tests/modules/programs/opencode/AGENTS.md b/tests/modules/programs/opencode/AGENTS.md new file mode 100644 index 00000000..6e70ab27 --- /dev/null +++ b/tests/modules/programs/opencode/AGENTS.md @@ -0,0 +1,22 @@ +# TypeScript Project Rules + +## External File Loading + +CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand. + +Instructions: + +- Do NOT preemptively load all references - use lazy loading based on actual need +- When loaded, treat content as mandatory instructions that override defaults +- Follow references recursively when needed + +## Development Guidelines + +For TypeScript code style and best practices: @docs/typescript-guidelines.md +For React component architecture and hooks patterns: @docs/react-patterns.md +For REST API design and error handling: @docs/api-standards.md +For testing strategies and coverage requirements: @test/testing-guidelines.md + +## General Guidelines + +Read the following file immediately as it's relevant to all workflows: @rules/general-guidelines.md. diff --git a/tests/modules/programs/opencode/default.nix b/tests/modules/programs/opencode/default.nix index 6dd7a268..9a75ce55 100644 --- a/tests/modules/programs/opencode/default.nix +++ b/tests/modules/programs/opencode/default.nix @@ -1,3 +1,5 @@ { opencode-settings = ./settings.nix; + opencode-rules = ./rules.nix; + opencode-empty-rules = ./empty-rules.nix; } diff --git a/tests/modules/programs/opencode/empty-rules.nix b/tests/modules/programs/opencode/empty-rules.nix new file mode 100644 index 00000000..243d6865 --- /dev/null +++ b/tests/modules/programs/opencode/empty-rules.nix @@ -0,0 +1,9 @@ +{ + programs.opencode = { + enable = true; + rules = ""; + }; + nmt.script = '' + assertPathNotExists home-files/.config/opencode/AGENTS.md + ''; +} diff --git a/tests/modules/programs/opencode/rules.nix b/tests/modules/programs/opencode/rules.nix new file mode 100644 index 00000000..b0839c5a --- /dev/null +++ b/tests/modules/programs/opencode/rules.nix @@ -0,0 +1,34 @@ +{ + programs.opencode = { + enable = true; + rules = '' + # TypeScript Project Rules + + ## External File Loading + + CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand. + + Instructions: + + - Do NOT preemptively load all references - use lazy loading based on actual need + - When loaded, treat content as mandatory instructions that override defaults + - Follow references recursively when needed + + ## Development Guidelines + + For TypeScript code style and best practices: @docs/typescript-guidelines.md + For React component architecture and hooks patterns: @docs/react-patterns.md + For REST API design and error handling: @docs/api-standards.md + For testing strategies and coverage requirements: @test/testing-guidelines.md + + ## General Guidelines + + Read the following file immediately as it's relevant to all workflows: @rules/general-guidelines.md. + ''; + }; + nmt.script = '' + assertFileExists home-files/.config/opencode/AGENTS.md + assertFileContent home-files/.config/opencode/AGENTS.md \ + ${./AGENTS.md} + ''; +}