mcp-nixos

repository·main·Indexed 20 days ago

https://github.com/utensils/mcp-nixos

A Model Context Protocol (MCP) server version 2.4.3 that provides AI agents with real-time access to NixOS, Home Manager, and nix-darwin resources. It includes a unified query tool for searching packages, options, and documentation across sources like FlakeHub, Noogle, and the NixOS Wiki, as well as a tool for finding historical package versions and commit hashes.

Tokens
9.2K
Snippets
28
Records
44
Agent score
71%

What's inside mcp-nixos

  1. Overview of MCP-NixOS capabilities

    main

    MCP-NixOS is a Model Context Protocol (MCP) server designed to provide AI assistants with accurate, real-time information about the NixOS ecosystem. It prevents LLM hallucinations by providing direct access to live data instead of relying on training data.

    Key data sources provided to AI assistants:

    • NixOS Packages & Options: Metadata and system configuration options.
    • User Configuration: Home Manager settings and nix-darwin macOS options.
    • Neovim Config: Nixvim options (via NuschtOS) and NVF options.
    • Flakes: FlakeHub and community flake registries.
    • Nix Functions: Signatures and documentation via Noogle.
    • Community Knowledge: NixOS Wiki and nix.dev guides.
    • Package Details: Version history, binary cache status (availability, size, compression), and local flake inputs read from /nix/store.
  2. Data sources used by MCP-NixOS

    main

    MCP-NixOS aggregates data from several authoritative Nix ecosystem sources to provide up-to-the-second information:

    • Official Indices: search.nixos.org (packages & options), wiki.nixos.org (community wiki), and nix.dev (official tutorials).
    • Versions & Metadata: NixHub.io (versions, store paths, licenses).
    • Infrastructure: cache.nixos.org (binary cache status).
    • Flakes: FlakeHub (Determinate Systems registry).
    • Nix Functions & Options: noogle.dev (function signatures), NuschtOS (Nixvim options), and NVF docs (NVF option catalogue).
    • Local Context: Your local /nix/store (for pinned flake inputs).
  3. What tools are provided by MCP-NixOS?

    main

    MCP-NixOS exposes two primary tools to your AI assistant. All responses are returned as plain text to ensure compatibility and ease of parsing for LLMs.

    • nix: A unified query tool. Use this for searching packages, inspecting info/stats, browsing options, checking channels, looking up flake inputs, and checking binary cache status.
    • nix_versions: A specialized tool for retrieving package version history and specific nixpkgs commit hashes via NixHub.io.
  4. How MCP-NixOS architecture works

    main

    MCP-NixOS v2.x is a stateless, asynchronous FastMCP 3.x server built with Python 3.11+. It does not use a persistent database; instead, it relies on live APIs as the source of truth. To maintain performance, it uses in-process caching for specific index-style sources (like Nixvim, NVF, Noogle, and nix.dev) to avoid redundant fetches during a single server session.

    Core Components:

    • nix tool: A unified query router that handles search, info, stats, browse, channels, flake-inputs, cache, and store operations.
    • nix_versions tool: Provides package version history via NixHub.io.
    • Elasticsearch client: Interfaces with search.nixos.org for packages, options, and flakes.
    • HTML Parsers: On-demand parsing of Home Manager and nix-darwin documentation, and normalization of NVF option catalogues.
    • Plain-text Formatter: Ensures all responses are human-readable text, optimized for LLM consumption (avoiding raw XML or JSON in prompts).
    • Async Runtime: Uses asyncio.to_thread() for all blocking I/O to ensure the event loop remains responsive.
  5. Generate favicon files using ImageMagick

    main

    You can regenerate the project's favicon assets from the source PNG logo using ImageMagick's convert command. This process involves resizing the source logo to various dimensions for different platforms (standard favicons, iOS, Android, and Windows tiles) and then combining specific PNGs into a single .ico file.

    # Generate PNG files from the source PNG logo
    convert -background none -resize 16x16 ../images/mcp-nixos.png favicon-16x16.png
    convert -background none -resize 32x32 ../images/mcp-nixos.png favicon-32x32.png
    convert -background none -resize 180x180 ../images/mcp-nixos.png apple-touch-icon.png
    convert -background none -resize 192x192 ../images/mcp-nixos.png android-chrome-192x192.png
    convert -background none -resize 512x512 ../images/mcp-nixos.png android-chrome-512x512.png
    convert -background none -resize 150x150 ../images/mcp-nixos.png mstile-150x150.png
    
    # Generate ICO file (combines multiple sizes)
    convert favicon-16x16.png favicon-32x32.png favicon.ico
  6. Install MCP-NixOS via uvx (Recommended)

    main

    If you have Python installed, the recommended way to run MCP-NixOS is using uvx. This method does not install anything permanently and is ideal for most users. Add the following configuration to your MCP client settings:

    {
      "mcpServers": {
        "nixos": {
          "command": "uvx",
          "args": ["mcp-nixos"]
        }
      }
    }
  7. Install MCP-NixOS for Pi Coding Agent

    main

    Since Pi does not speak MCP natively, use one of these two paths:

    A. pi-mcp-adapter (Recommended): Install the adapter via npm:

    pi install npm:pi-mcp-adapter

    Then add this to your ~/.pi/agent/mcp.json:

    {
      "mcpServers": {
        "nixos": {
          "command": "uvx",
          "args": ["mcp-nixos"],
          "lifecycle": "lazy"
        }
      }
    }

    B. Project-local extension: Clone this repository. It ships .pi/extensions/mcp-nixos.ts, which is auto-loaded when running pi inside the cloned repo.

  8. Run MCP-NixOS via HTTP Transport (Remote MCP)

    main

    For shared setups or remote hosts, you can run the server over HTTP instead of STDIO. This allows one server instance to serve multiple clients.

    Default startup (http://127.0.0.1:8000/mcp):

    MCP_NIXOS_TRANSPORT=http mcp-nixos

    Custom host, port, and path:

    MCP_NIXOS_TRANSPORT=http \
      MCP_NIXOS_HOST=0.0.0.0 \
      MCP_NIXOS_PORT=9090 \
      MCP_NIXOS_PATH=/api/mcp \
      mcp-nixos

    Stateless mode (no per-client sessions):

    MCP_NIXOS_TRANSPORT=http MCP_NIXOS_STATELESS_HTTP=1 mcp-nixos
    # Start with defaults: http://127.0.0.1:8000/mcp
    MCP_NIXOS_TRANSPORT=http mcp-nixos
  9. Manage the MCP-NixOS website with docs commands

    main

    Once inside a Nix development shell, you can use the following categorized helper commands to manage the documentation site. These commands are available in both the full and docs-only shells:

    • docs-install: Performs the initial npm install setup.
    • docs-dev: Starts the VitePress development server with hot reload.
    • docs-build: Generates a static build of the site into website/out/.
    • docs-preview: Serves the built site locally for testing.
    • docs-check: Runs vue-tsc to type-check Vue components.
    • docs-clean: Wipes the website/out/ directory and the .vitepress/cache.
    docs-install
    docs-dev
    docs-build
    docs-preview
    docs-check
    docs-clean
  10. Declarative Installation (Nix/NixOS/Home Manager)

    main

    MCP-NixOS is available in nixpkgs.

    NixOS (configuration.nix):

    environment.systemPackages = [ pkgs.mcp-nixos ];

    Home Manager (home.nix):

    home.packages = [ pkgs.mcp-nixos ];

    nix-darwin (darwin-configuration.nix):

    environment.systemPackages = [ pkgs.mcp-nixos ];

    Using a Flake with Overlay: You can use the provided overlay in your flake.nix to integrate it into your NixOS or Home Manager configurations.

    # flake.nix
    {
      inputs = {
        nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
        mcp-nixos.url = "github:utensils/mcp-nixos";
      };
    
      outputs = { self, nixpkgs, mcp-nixos, ... }: {
        # Example: NixOS configuration
        nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [{
            nixpkgs.overlays = [ mcp-nixos.overlays.default ];
            environment.systemPackages = [ pkgs.mcp-nixos ];
          }];
        };
    
        # Example: Home Manager standalone
        homeConfigurations.myuser = home-manager.lib.homeManagerConfiguration {
          pkgs = import nixpkgs {
            system = "x86_64-linux";
            overlays = [ mcp-nixos.overlays.default ];
          };
          modules = [{
            home.packages = [ pkgs.mcp-nixos ];
          }];
        };
      };
    }