I use Nix and Devenv to manage dependencies and build environments for all of my hobby projects. While I got the tutorial server up and running in a terminal relatively quickly, I had some trouble figuring out how to invoke it as a CLI from Claude Desktop’s MCP configuration. During development, I typically run nix develop --no-pure-eval to enter a devShell and then have access to all the packages I’ve declared a dependency on. In order to run node build/index.js from Claude Desktop, I needed the right config to get a nix run invocation to work. With some help from Claude itself, here’s the flake I ended up with:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    systems.url = "github:nix-systems/default";
    devenv.url = "github:cachix/devenv";
    devenv.inputs.nixpkgs.follows = "nixpkgs";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs = {
    self,
    nixpkgs,
    devenv,
    systems,
    ...
  } @ inputs: let
    forEachSystem = nixpkgs.lib.genAttrs (import systems);
  in {
    packages = forEachSystem (system: let
      pkgs = nixpkgs.legacyPackages.${system};
    in {
      devenv-up = self.devShells.${system}.default.config.procfileScript;
      weather-server = pkgs.stdenv.mkDerivation {
        name = "weather-server";
        src = ./.;

        installPhase = ''
          mkdir -p $out/bin $out/lib
          cp -r build $out/lib/
          cp -r node_modules $out/lib/

          # Create a simple wrapper script
          cat > $out/bin/weather-server <<EOF
          #!${pkgs.runtimeShell}
          exec ${pkgs.nodejs_22}/bin/node $out/lib/build/index.js "\$@"
          EOF

          chmod +x $out/bin/weather-server
        '';
      };
      default = self.packages.${system}.weather-server;
    });

    devShells =
      forEachSystem
      (system: let
        pkgs = nixpkgs.legacyPackages.${system};
      in {
        default = devenv.lib.mkShell {
          inherit inputs pkgs;
          modules = [
            {
              # https://devenv.sh/reference/options/
              packages = with pkgs; [
                pkgs.nodejs_22
              ];

              languages.javascript = {
                enable = true;
                package = pkgs.nodejs_22;
                pnpm.enable = true;
              };

              processes = {
                app.exec = "node build/index.js";
              };
            }
          ];
        };
      });
  };
}

The corresponding entry in Claude Desktop’s config is:

{
    "mcpServers": {
        "weather": {
            "command": "/nix/var/nix/profiles/default/bin/nix",
            "args": [
                "run",
                "/Users/amey/Developer/mcp-server-tutorial#weather-server"
            ]
        }
    }
}
Tagged: