Files
eyduhandClaude Opus 4.8 7276286711 auth: gated-scope admin access via roles.admin + shared bearer keys
Corpus bearer keys become share=true and group-readable (clan-rag, 0440)
so an interactive admin on a trusted-root machine can present them. New
roles.admin (dedicated tag, decoupled from client/desktop) grants a named
user into the clan-rag group and publishes /etc/clan-rag/keys.json
(corpus->keyfile), read from the co-located server's instances.

CLIs attach the token as a bearer: resolve_instance_key picks it up from
CLAN_RAG_INSTANCE_KEY, _FILE, or the per-corpus keys map, mirroring the
API-URL corpus resolution so the right key reaches the right corpus.

Gate is anchored on the age/sops-admin set (who can decrypt the var) plus
root on each deploy target, NOT the auth export's admins group; group/mode
is only plumbing, since deployed clan vars are plaintext at rest.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-13 01:19:30 +02:00

245 lines
13 KiB
Nix

# Clan service: multi-corpus local RAG stack.
# _class = "clan.service" — consumed via clan.modules."@eyduh/clan-rag".
#
# Imports nixosModules/default.nix (the multi-instance module) and adds clan
# vars + state on top. One inventory instance hosts N corpora (each its own
# MeiliSearch index, scopes and derived API port) sharing one FastFlowLM +
# MeiliSearch.
#
# Roles:
# server — AMD NPU inference (FastFlowLM) + MeiliSearch + one clan-rag-api per corpus
# client — installs the clan-rag-{query,tui,mcp} binaries
{ nix-amd-ai, clan-core, crane, clanCoreRev ? "main" }:
let
baseModule = import ../nixosModules { inherit nix-amd-ai clan-core crane clanCoreRev; };
# Options shared by a corpus definition (server role) — mirrors the module's
# per-instance options (minus instanceKeysFile, which this service generates).
corpusModule = { lib, ... }: {
options = {
docs = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Optional docs tree (markdown + nix) to index.";
};
useClanCore = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Index the clan-core tree this service is pinned to (sets `docs` to it).";
};
sources = lib.mkOption {
type = lib.types.listOf (lib.types.submodule {
options = {
kind = lib.mkOption { type = lib.types.enum [ "docs" "code" "memory" ]; };
path = lib.mkOption { type = lib.types.str; };
scope = lib.mkOption { type = lib.types.str; default = "developer"; };
source = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };
url = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };
chunker = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };
};
});
default = [ ];
description = "Source trees to index (code/docs/memory).";
};
openScopes = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ "general" ]; };
gatedScopes = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; };
scopeLabels = lib.mkOption { type = lib.types.attrsOf lib.types.str; default = { }; };
clanName = lib.mkOption { type = lib.types.str; default = ""; };
clanDomain = lib.mkOption { type = lib.types.str; default = ""; };
port = lib.mkOption { type = lib.types.nullOr lib.types.port; default = null; };
reindexInterval = lib.mkOption { type = lib.types.str; default = "1h"; };
# Registry/MCP metadata. project/projectDesc also anchor the API persona
# for non-clan corpora; an empty projectDesc is auto-generated on first index.
project = lib.mkOption { type = lib.types.str; default = ""; description = "Corpus name; also the prompt persona subject."; };
projectDesc = lib.mkOption { type = lib.types.str; default = ""; description = "One-line corpus description for the persona. Empty → auto-generated on first index."; };
toolSlug = lib.mkOption { type = lib.types.str; default = ""; };
};
};
in
{
_class = "clan.service";
manifest = {
name = "@eyduh/clan-rag";
description = "Multi-corpus local RAG — NPU-accelerated via FastFlowLM + MeiliSearch";
categories = [ "Utility" "Development" ];
readme = ''
Local RAG over one or more corpora (clan docs, project source, reference
libraries). The **server** role runs FastFlowLM on an AMD Ryzen AI (XDNA2)
NPU plus MeiliSearch, and one clan-rag-api per corpus on a derived port.
Corpora are declared under `roles.server…settings.corpora`; each is indexed
in the background on first boot. The **client** role installs the
clan-rag-{query,tui,mcp} binaries. Secrets are clan vars.
Prompt persona: a `clanName` corpus uses clan framing ("my clan" → this
deployment); any other corpus uses `project`/`projectDesc` as its persona
so answers stay on-domain. The excerpt legend is derived from `openScopes`
(general tier) and `gatedScopes` (the caller's own setup); a gated scope
turns on "prefer it for your own setup" guidance. When `projectDesc` is
empty, the indexer generates one from the corpus content on first index
(cached in the state dir; a manual `projectDesc` always overrides it).
'';
};
# ──────────────────────────────────────────────────────────────────────────
# server role
# ──────────────────────────────────────────────────────────────────────────
roles.server = {
description = "NPU inference + MeiliSearch + one RAG API per corpus — requires AMD Ryzen AI (XDNA2)";
interface = { lib, ... }: {
options = {
user = lib.mkOption {
type = lib.types.str;
description = "Existing user to run FastFlowLM as. Added to video+render groups.";
};
model = lib.mkOption {
type = lib.types.str;
default = "gemma4-it:e4b";
description = "Shared FastFlowLM generation model. Pre-pull with `flm pull <tag>`.";
};
embedModel = lib.mkOption {
type = lib.types.str;
default = "embeddinggemma-300m";
description = "Shared embedding model.";
};
ragHost = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = "Bind address for every corpus's clan-rag-api (0.0.0.0 so clients can reach it).";
};
openToMesh = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Bind APIs to clan.core.networking.internalListenAddresses + open firewall ports.";
};
corpora = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule corpusModule);
default = { };
description = "RAG corpora to host on this machine. Each gets its own index, API port and units.";
};
};
};
perInstance = { settings, instanceName, ... }: {
nixosModule = { config, lib, pkgs, ... }:
let
sanitize = s:
let r = builtins.replaceStrings [ "@" "/" "." "+" ":" " " ] [ "-" "-" "-" "-" "-" "-" ] s;
in lib.removePrefix "-" (lib.removeSuffix "-" r);
safe = sanitize instanceName;
meiliVar = "${safe}-meili-key";
corpusKeyVar = cname: "${safe}-${sanitize cname}-key";
gatedCorpora = lib.filterAttrs (_: c: c.gatedScopes != [ ]) settings.corpora;
randKeyGenerator = {
files.key = { };
runtimeInputs = [ pkgs.coreutils ];
script = ''(tr -dc 'A-Za-z0-9' < /dev/urandom || true) | head -c 48 > "$out/key"'';
};
# Secrets this machine generates, in two flavours:
# meiliKeyGen — MeiliSearch master key. Per-machine: it's arcus's own
# DB key and never needs to leave the server.
# corpusKeyGens — one bearer key per gated corpus. share = true so the
# value is generated once and reused clan-wide, letting an
# admin client present the same token to unlock gated scopes.
# Group-readable (clan-rag, mode 0440) so the `admin` role
# can expose it to a named interactive user on this
# trusted-root machine; root reads it regardless of mode.
meiliKeyGen = { ${meiliVar} = randKeyGenerator; };
corpusKeyGens = lib.mapAttrs'
(cname: _: lib.nameValuePair (corpusKeyVar cname) (randKeyGenerator // {
share = true;
files.key = { group = "clan-rag"; mode = "0440"; };
}))
gatedCorpora;
in
{
imports = [ baseModule ];
services.clan-rag = {
enable = true;
inherit (settings) user model embedModel ragHost openToMesh;
meiliKeyFile = config.clan.core.vars.generators.${meiliVar}.files.key.path;
instances = lib.mapAttrs (cname: c: {
inherit (c) sources openScopes gatedScopes scopeLabels clanName clanDomain port reindexInterval
project projectDesc toolSlug;
docs = if c.useClanCore then "${clan-core}" else c.docs;
instanceKeysFile =
if c.gatedScopes != [ ]
then config.clan.core.vars.generators.${corpusKeyVar cname}.files.key.path
else null;
}) settings.corpora;
};
clan.core.vars.generators = lib.mkMerge [ meiliKeyGen corpusKeyGens ];
# Owns the gated bearer keys' group. Empty by default (only root reads);
# the admin role adds interactive users to it.
users.groups.clan-rag = lib.mkIf (gatedCorpora != { }) { };
clan.core.state."${safe}-rag".folders = [ "/var/lib/meilisearch" "/var/lib/clan-rag" ];
};
};
};
# ──────────────────────────────────────────────────────────────────────────
# client role — the RAG CLI/TUI/MCP binaries (connect via per-project .mcp.json)
# ──────────────────────────────────────────────────────────────────────────
roles.client = {
description = "Installs clan-rag-{query,tui,mcp}; connect them via a per-project .mcp.json.";
perInstance = { ... }: {
nixosModule = { pkgs, ... }: {
environment.systemPackages = [ (pkgs.callPackage ../nix/clan-rag.nix { inherit crane; }) ];
};
};
};
# ──────────────────────────────────────────────────────────────────────────
# admin role — interactive access to a corpus's GATED scopes on this machine.
#
# Dedicated tag, deliberately decoupled from `client`/`desktop`: apply it ONLY
# to trusted-root machines you want to read deployment-scoped answers, since a
# deployed clan var is plaintext at rest (root reads it regardless of mode).
# It grants `user` into the clan-rag group (server-readable keys) and publishes
# the corpus→key-file map the CLIs use to present the bearer. Must be
# co-located with the server role — it reads the locally-configured
# services.clan-rag.instances for the key paths.
# ──────────────────────────────────────────────────────────────────────────
roles.admin = {
description = "Grants a named user interactive access to gated scopes. Co-locate with the server role.";
interface = { lib, ... }: {
options.user = lib.mkOption {
type = lib.types.str;
description = "Existing interactive user granted read access to the gated bearer keys.";
};
};
perInstance = { settings, ... }: {
nixosModule = { config, lib, pkgs, ... }:
let
# Server role already resolved each gated corpus's key file into
# instances.<name>.instanceKeysFile; the CLIs read this map by corpus name.
# Guarded on the option existing: it's declared by the co-located server
# role (baseModule). An admin role without a server is a no-op here (and
# unsupported — no local registry/keys), so it degrades to no map.
instances = if config.services ? clan-rag then config.services.clan-rag.instances else { };
keyFiles = lib.mapAttrs (_: i: i.instanceKeysFile)
(lib.filterAttrs (_: i: i.instanceKeysFile != null) instances);
in
{
environment.systemPackages = [ (pkgs.callPackage ../nix/clan-rag.nix { inherit crane; }) ];
users.groups.clan-rag = { };
users.users.${settings.user}.extraGroups = [ "clan-rag" ];
environment.etc."clan-rag/keys.json" = lib.mkIf (keyFiles != { }) {
text = builtins.toJSON keyFiles;
};
};
};
};
}