How to init mongodb service from dev.nix file?

I can install the MongoDB service, but I cannot run it. Does anyone know how to do it?"

There’s no services.mongodb for the dev.idx file yet… But you still start the mongodb services by your own.

The mongodb package brings a program called mongod which means to be the mongo daemon, if you try to run mongod it will fail because we need to do some tweaks before.

  1. Create a directory in your workspace, for instance mongodb
  2. Create two files inside that directory, for instance start.sh and mongo.conf with the following content:

mongo.conf file (referenced by mongo page for more info):

processManagement:
   fork: true
net:
   bindIp: localhost
   port: 27017
storage:
   dbPath: "./mongodb/var/lib/mongo"
systemLog:
   destination: file
   path: "./mongodb/var/log/mongodb/mongod.log"
   logAppend: true

start.sh file:

#!/usr/bin/bash

# create inside our directory that we created as `mongodb`
# the following directories which saves the data and log of the mongo service
mkdir -p ./mongodb/var/lib/mongo/
mkdir -p ./mongodb/var/log/mongodb/

# run the mongo daemon with the configuration that we have created above
mongod --config ./mongodb/mongo.conf
  1. Start the service everytime that we access to our workspace (in the dev.idx file):
{pkgs, ...}: {
  idx.workspace.onStart = {
    mongo-db = "./mongodb/start.sh";
  };
}

And this should be the final directory tree if we applied the above and run the service:

./my-blank-app/
β”œβ”€β”€ .idx
β”‚   └── dev.nix
β”œβ”€β”€ mongodb
β”‚   β”œβ”€β”€ mongo.conf
β”‚   β”œβ”€β”€ start.sh
β”‚   └── var
β”‚       β”œβ”€β”€ lib
β”‚       β”‚   └── mongo
β”‚       β”‚       β”œβ”€β”€ collection-0--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ collection-2--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ collection-4--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ diagnostic.data
β”‚       β”‚       β”‚   β”œβ”€β”€ metrics.2024-08-15T12-11-51Z-00000
β”‚       β”‚       β”‚   └── metrics.interim
β”‚       β”‚       β”œβ”€β”€ index-1--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ index-3--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ index-5--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ index-6--5619253475827229124.wt
β”‚       β”‚       β”œβ”€β”€ journal
β”‚       β”‚       β”‚   β”œβ”€β”€ WiredTigerLog.0000000001
β”‚       β”‚       β”‚   β”œβ”€β”€ WiredTigerPreplog.0000000001
β”‚       β”‚       β”‚   └── WiredTigerPreplog.0000000002
β”‚       β”‚       β”œβ”€β”€ _mdb_catalog.wt
β”‚       β”‚       β”œβ”€β”€ mongod.lock
β”‚       β”‚       β”œβ”€β”€ sizeStorer.wt
β”‚       β”‚       β”œβ”€β”€ storage.bson
β”‚       β”‚       β”œβ”€β”€ WiredTiger
β”‚       β”‚       β”œβ”€β”€ WiredTigerHS.wt
β”‚       β”‚       β”œβ”€β”€ WiredTiger.lock
β”‚       β”‚       β”œβ”€β”€ WiredTiger.turtle
β”‚       β”‚       └── WiredTiger.wt
β”‚       └── log
β”‚           └── mongodb
β”‚               └── mongod.log
└── README.md
2 Likes

Thank you for your suggestion. I tried the solution you provided, but unfortunately, it didn’t solve my problem. Instead, I encountered a new error:

Also, I created a new project and set it up as you explained it, but I get the same error.

Oops, sorry I had forgotten to say to add executable permission for the start.sh

chmod +x ./mongodb/start.sh

And it should work normally.

2 Likes

Thank you very much! It worked. :blush:

1 Like