Skip to content
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rec {
lib.make-root-apps
lib.make-id
lib.make-mcpx-overlay
lib.make-mcpx-modules
lib.make-laptop-backups-checks
];
inputs = {
Expand Down
22 changes: 22 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,27 @@ let
};
};

# The mcpx daemon modules (code#4801): the canonical module files
# live in pkgs/mcpx/modules; these outputs wrap them with this
# flake's own mcpx build as the package default. The opencode fork
# re-exports the same files defaulted to its build, and
# openchamber passes them through — the option set never forks.
make-mcpx-modules =
let
wrap =
module:
{ lib, pkgs, ... }:
{
imports = [ module ];
services.mcpx.package = lib.mkDefault (pkgs.callPackage ../pkgs/mcpx/package.nix { });
};
in
{
nixosModules.mcpx = wrap ../pkgs/mcpx/modules/nixos.nix;
homeManagerModules.mcpx = wrap ../pkgs/mcpx/modules/home-manager.nix;
darwinModules.mcpx = wrap ../pkgs/mcpx/modules/darwin.nix;
};

_self = merge [
lib
{
Expand Down Expand Up @@ -1207,6 +1228,7 @@ let
make-root-apps
make-id
make-mcpx-overlay
make-mcpx-modules
make-laptop-backups-checks
;
}
Expand Down
131 changes: 131 additions & 0 deletions pkgs/mcpx/modules/common.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# SPDX-License-Identifier: MIT OR Apache-2.0
#
# mcpx module — shared options and config rendering.
#
# Canonical home: developing-today/code, pkgs/mcpx/modules (moved
# from dezren39/nix's modules/mcpx on 2026-11-03, code#4801 — the
# daemon mode originates with mcpx; the opencode and openchamber
# flakes re-export these modules, they do not re-author them).
# Imported by every platform module in this directory (nixos.nix,
# home-manager.nix, darwin.nix): the option set is the contract —
# `services.mcpx.enable`,
# `package`, `settings` (rendered to config.json) and `servers` — and
# it is identical on every platform; only the service definition
# differs. No secret management: credentials come from
# a runtime file the service reads at start
# (`services.mcpx.environmentFile`, never copied into the store) or
# from files the user points at — mcpx's own `${VAR}` expansion in a
# server's `auth` reads those at runtime, nothing is rendered into
# the config beyond what the user wrote in `settings`/`servers`.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.mcpx;
in
{
options.services.mcpx = {
enable = lib.mkEnableOption "the mcpx daemon";

package = lib.mkOption {
type = lib.types.package;
description = ''
The mcpx package to run. The flake's module outputs default
this to the flake's own `packages.<system>.mcpx` build (from
the pinned mcpx-src input); override it to run another build.
'';
};

settings = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
example = lib.literalExpression "{ daemon.autostart = false; }";
description = ''
mcpx settings, rendered verbatim into the daemon's
config.json (see `services.mcpx.configFile`). A `mcpServers`
key written here is kept: it is merged per-server with
`servers`, and `servers` wins for any server name defined
in both places.
'';
};

servers = lib.mkOption {
type = lib.types.attrsOf (lib.types.attrsOf lib.types.anything);
default = { };
example = lib.literalExpression ''
{
codedb = {
command = "/run/current-system/sw/bin/codedb";
args = [ "mcp" ];
env.CODEDB_NO_TELEMETRY = "1";
};
}
'';
description = ''
MCP servers the daemon pools, rendered as `mcpServers` in
config.json, merged per-server over any `mcpServers` key in
`settings` (a name defined here wins). Each server's
attributes pass through untouched
(`command`, `args`, `env`, `auth`, pooling keys, ...). Use
absolute paths for `command`: a daemon inherits the PATH of
whatever started it, so a bare name can fail to resolve.
'';
};

environment = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = { };
example = lib.literalExpression ''{ MCPX_STATE_DIR = "/var/lib/mcpx"; }'';
description = ''
Extra environment variables for the daemon process. NOT for
secrets: values are rendered into the generated unit/plist
in the world-readable Nix store, exactly like any other
literal in a Nix configuration. For credentials use
`environmentFile` — a runtime file the module never copies
into the store.
'';
};

environmentFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = lib.literalExpression ''"/run/user/1000/mcpx/env"'';
description = ''
Path to a runtime environment file (`KEY=VALUE` lines) the
service reads at start (systemd `EnvironmentFile`). This is
the credential path: the file lives outside the Nix store,
owned and permissioned by the user, and only its path — never
its contents — enters the configuration. Linux only:
launchd has no equivalent, so the darwin branch of the
home-manager module asserts this stays unset there.
'';
};

configFile = lib.mkOption {
type = lib.types.path;
readOnly = true;
description = ''
The rendered config.json (settings + servers) the daemon is
started with. Read-only: derived from `settings` and
`servers`.
'';
};
};

config = lib.mkIf cfg.enable {
services.mcpx.configFile = pkgs.writeText "mcpx-config.json" (
builtins.toJSON (
cfg.settings
// {
# Merge, don't replace (#1244): a mcpServers key in
# settings survives when servers is unset/empty, and
# servers wins per-server for names defined in both.
mcpServers = (cfg.settings.mcpServers or { }) // cfg.servers;
}
)
);
};
}
121 changes: 121 additions & 0 deletions pkgs/mcpx/modules/darwin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# SPDX-License-Identifier: MIT OR Apache-2.0
#
# mcpx module — nix-darwin (code#4801).
#
# Canonical home: developing-today/code, pkgs/mcpx/modules. The
# launchd rendering of the daemon, ported from the hand-rolled agent
# dezren39/nix's darwin configuration ran (configuration.nix,
# launchd.user.agents.mcpx). Same service semantics as the NixOS and
# home-manager modules in this directory: start at login
# (RunAtLoad), keep running (KeepAlive), a 20 s stop window
# (ExitTimeOut — mcpx's stop drains HTTP and stops its server
# processes, all bounded by limits.execDrainTimeout, so launchd's
# 5 s default cut every restart off mid-shutdown), $HOME as the
# working directory (mcpx keys a daemon by the config files it finds
# searching up from where it starts, and a CLI run from $HOME finds
# the same set), and the rendered config named with `--config`.
#
# The program is a fixed path, /usr/local/libexec/mcpx/mcpx, NOT a
# store path — deliberately. macOS lists a background item under its
# executable's file name and tracks it by path: a store path changes
# on every mcpx build, giving the agent a new name and executable
# each time (dezren39/nix counted 9 path changes in three weeks).
# The fixed file is a copy of the wrapper below, refreshed by
# extraActivation before launchd is set up; since the plist no
# longer changes when mcpx does, postActivation kickstarts the agent
# whenever the copy changed.
#
# The wrapper stops whatever already serves this config's socket
# before starting: launchd never runs two copies of one job, so a
# running daemon was started by something else (the CLI's autostart,
# the OpenCode fork's mcpx plugin — developing-today/opencode#134).
# Left running, it holds the socket, this daemon exits "a daemon is
# already listening", and KeepAlive respawns it forever.
#
# Two things the dezren39/nix original had that this canonical
# module deliberately does not: its service-events start hook (a
# local observability embellishment of that repo, which can layer it
# back by overriding the agent) and its hand-written user config —
# the module starts the daemon on the rendered `configFile` and
# points clients at it with MCPX_CONFIG, exactly like the NixOS
# module, so every platform computes the same daemon socket.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.mcpx;
home = config.users.users.${config.system.primaryUser}.home;
agentPath = "/usr/local/libexec/mcpx/mcpx";
agent = pkgs.writeShellScript "mcpx" ''
${cfg.package}/bin/mcpx --config ${cfg.configFile} stop || true
exec ${cfg.package}/bin/mcpx daemon --config ${cfg.configFile}
'';
in
{
imports = [ ./common.nix ];

config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.environmentFile == null;
message = "services.mcpx.environmentFile has no launchd equivalent; on darwin, hand the daemon credentials through files its servers point at instead.";
}
];

environment.systemPackages = [ cfg.package ];

# Point every other mcpx client at the same config the daemon
# runs: MCPX_CONFIG names exactly one file and bypasses
# discovery, so a CLI or MCP client in a user session computes
# the same daemon socket.
# String interpolation, not the bare value: nix-darwin's
# launchd.user.envVariables is typed string with no derivation
# coercion, unlike the sessionVariables options on the other
# platforms, which accept cfg.configFile directly.
launchd.user.envVariables.MCPX_CONFIG = "${cfg.configFile}";

launchd.user.agents.mcpx = {
serviceConfig = {
ProgramArguments = [ agentPath ];
EnvironmentVariables = {
HOME = home;
}
// cfg.environment;
KeepAlive = true;
RunAtLoad = true;
ExitTimeOut = 20;
WorkingDirectory = home;
# Not /tmp: that is emptied at boot, taking the evidence of
# what went wrong before it.
StandardErrorPath = "${home}/Library/Logs/mcpx.log";
StandardOutPath = "${home}/Library/Logs/mcpx.log";
};
};

# Refresh the fixed-path copy before launchd is set up, and
# remember whether it changed so postActivation can kickstart
# the agent (its plist no longer changes when mcpx does).
# Root-owned and not user-writable, like the store copy it
# mirrors.
system.activationScripts.extraActivation.text = lib.mkAfter ''
mcpxAgentChanged=0
if ! /usr/bin/cmp -s ${agent} ${agentPath}; then
echo "installing ${agentPath}" >&2
/bin/mkdir -p "$(/usr/bin/dirname ${agentPath})"
/usr/bin/install -o root -g wheel -m 0755 ${agent} ${agentPath}.tmp
/bin/mv -f ${agentPath}.tmp ${agentPath}
mcpxAgentChanged=1
fi
'';

system.activationScripts.postActivation.text = lib.mkAfter ''
: "''${mcpxAgentChanged:=0}"
if [ "$mcpxAgentChanged" = "1" ]; then
/bin/launchctl kickstart -k "gui/$(/usr/bin/id -u ${config.system.primaryUser})/org.nixos.mcpx" || true
fi
'';
};
}
92 changes: 92 additions & 0 deletions pkgs/mcpx/modules/home-manager.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# SPDX-License-Identifier: MIT OR Apache-2.0
#
# mcpx module — home-manager.
#
# Canonical home: developing-today/code, pkgs/mcpx/modules
# (code#4801). Same service semantics as the NixOS module
# (./nixos.nix, whose header carries the reasoning): start at login,
# keep running, a 20 s stop window, $HOME as the working directory,
# and the rendered config named with `--config`. On Linux the unit
# is a systemd user service; on darwin it is a launchd user agent
# with the equivalent fields (KeepAlive / RunAtLoad / ExitTimeOut).
# The standalone nix-darwin rendering is ./darwin.nix; note the two
# darwin renderings differ in one respect — this one points launchd
# straight at the store path, while darwin.nix keeps the fixed-path
# program copy a nix-darwin host needs for a stable plist (see its
# header).
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.mcpx;
in
{
imports = [ ./common.nix ];

config = lib.mkIf cfg.enable (
lib.mkMerge [
{
home.packages = [ cfg.package ];

# Point every other mcpx client at the same config the
# daemon runs, two ways: MCPX_CONFIG in the session
# environment (mcpx reads exactly that file instead of
# searching), and the discoverable
# ~/.config/mcpx/config.json, a link whose resolved target
# is the very store path the daemon is started with — so a
# client that discovers it computes the same daemon socket.
home.sessionVariables.MCPX_CONFIG = cfg.configFile;
xdg.configFile."mcpx/config.json".source = cfg.configFile;
}

(lib.mkIf pkgs.stdenv.hostPlatform.isLinux {
systemd.user.services.mcpx = {
Unit.Description = "mcpx daemon";
Install.WantedBy = [ "default.target" ];
# Via the service-level `environment` attribute, not
# Service.Environment: home-manager injects its own
# Service.Environment (session variables) and the two
# definitions do not merge.
environment = cfg.environment;
Service = {
ExecStart = "${cfg.package}/bin/mcpx daemon --config ${cfg.configFile}";
Restart = "always";
TimeoutStopSec = 20;
WorkingDirectory = "%h";
}
// lib.optionalAttrs (cfg.environmentFile != null) {
EnvironmentFile = cfg.environmentFile;
};
};
})

(lib.mkIf pkgs.stdenv.hostPlatform.isDarwin {
assertions = [
{
assertion = cfg.environmentFile == null;
message = "services.mcpx.environmentFile has no launchd equivalent; on darwin, hand the daemon credentials through files its servers point at instead.";
}
];
launchd.agents.mcpx = {
enable = true;
config = {
ProgramArguments = [
"${cfg.package}/bin/mcpx"
"daemon"
"--config"
"${cfg.configFile}"
];
EnvironmentVariables = cfg.environment;
KeepAlive = true;
RunAtLoad = true;
ExitTimeOut = 20;
WorkingDirectory = config.home.homeDirectory;
};
};
})
]
);
}
Loading
Loading