How to install php extension like mongodb?

# To learn more about how to use Nix to configure your environment
# see: https://developers.google.com/idx/guides/customize-idx-env
{pkgs}: {
  # Which nixpkgs channel to use.
  channel = "stable-23.11"; # or "unstable"
  services.docker.enable = true;
  # Use https://search.nixos.org/packages to find packages
  packages = [
    pkgs.php83
    pkgs.php83Packages.composer
    pkgs.php83Extensions.mongodb
    pkgs.rPackages.specL
    pkgs.nodejs_20
    pkgs.sudo
    pkgs.neovim
    pkgs.apt
  ];
  # Sets environment variables in the workspace
  env = {};
  idx = {
    # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
    extensions = [
      "formulahendry.auto-close-tag"
      "formulahendry.auto-rename-tag"
      "mikestead.dotenv"
      "onecentlin.laravel5-snippets"
      "DEVSENSE.composer-php-vscode"
      "donjayamanne.githistory"
      "GitHub.copilot"
      "codezombiech.gitignore"
      "eamodio.gitlens"
      "ecmel.vscode-html-css"
      "kisstkondoros.vscode-gutter-preview"
      "oderwat.indent-rainbow"
      "VisualStudioExptTeam.vscodeintellicode"
      "VisualStudioExptTeam.intellicode-api-usage-examples"
      "DEVSENSE.intelli-php-vscode"
      "ryannaddy.laravel-artisan"
      "shufo.vscode-blade-formatter"
      "onecentlin.laravel-blade"
      "austenc.laravel-blade-spacer"
      "IHunte.laravel-blade-wrapper"
      "glitchbl.laravel-create-view"
      "onecentlin.laravel-extension-pack"
      "MrChetan.goto-laravel-components"
      "naoray.laravel-goto-components"
      "mongodb.mongodb-vscode"
      "DEVSENSE.phptools-vscode"
      "xdebug.php-pack"
      "esbenp.prettier-vscode"
      "MrChetan.goto-laravel-components"
      "amiralizadeh9480.laravel-extra-intellisense"
      "onecentlin.laravel-extension-pack"
      "codingyu.laravel-goto-view"
      "pgl.laravel-jump-controller"
    ];
    # Enable previews and customize configuration
    previews = {
      enable = true;
      previews = {
        web = {
          command = ["php" "artisan" "serve" "--port" "$PORT" "--host" "0.0.0.0"];
          manager = "web";
        };
      };
    };
  };
}

My dev.nix configuration is like this, but Iā€™m having trouble installing the Mongodb PHP extension.
Please help me in the difficulty I am experiencing

Try modify your packages like bellow:

  packages = [
    (pkgs.php83.buildEnv {
      extensions = ({enabled, all}: enabled ++ (with all; [
        mongodb
      ]));
    })
    pkgs.php83Packages.composer
    # pkgs.php83Extensions.mongodb
    pkgs.rPackages.specL
    pkgs.nodejs_20
    pkgs.sudo
    pkgs.neovim
    pkgs.apt
  ];
1 Like

can I run the command

sudo pecl install mongodb

instead of using

(pkgs.php83.buildEnv {
 extensions = ({enabled, all}: enabled ++ (with all; [
 mongodb
 ]));
 })

contents of my composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The skeleton application for the Laravel framework.",
    "keywords": [
        "laravel",
        "framework"
    ],
    "license": "MIT",
    "require": {
        "php": "^8.1",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^10.10",
        "laravel/sanctum": "^3.3",
        "laravel/tinker": "^2.8",
        "mongodb/laravel-mongodb": "4.1.2",
        "mongodb/mongodb": "^1.17"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.18",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^7.0",
        "pestphp/pest": "^2.34",
        "pestphp/pest-plugin-laravel": "^2.3",
        "spatie/laravel-ignition": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

Try running the command below:

composer install --ignore-platform-reqs

I have tried simulating using the packages below and connecting it to MongoDB Atlas.

My dev.nix

  packages = [
    (pkgs.php83.buildEnv {
       extensions = ({enabled, all}: enabled ++ (with all; [
         mongodb
       ]));
     })
    pkgs.php83Packages.composer
    pkgs.nodejs_20
  ];

My composer.json

    "require": {
        "php": "^8.2",
        "laravel/framework": "^11.9",
        "laravel/tinker": "^2.9",
        "mongodb/laravel-mongodb": "^4.4",
        "mongodb/mongodb": "^1.19"
    },

1 Like

this works, thanks bro

2 Likes