Owning books (not reading them) is growing as a hobby in the digital age. I, as many others, still keep a collection of books that I occasionally read, mostly physical, but also digital.

Unfortunately, some books are not easily accessible physically where I am, or I might want to read parts of a book to see if it is worth investing in buying an actual physical copy.

For these reasons, among others, it is nice to have a nice, accessible web library to store your own books, and have access to them over the internet. For this reason, this guide shows you how to set up calibre-web as a self-hosted ebook library.

specs #

When idling, calibre-web takes 132M of RAM on my system.

installation #

Create a calibre folder and a default.nix file inside it, at the place where you have the rest of your nixos configuration

$ mkdir calibre && touch calibre/default.nix

(or use the file explorer of your choice)

nix declaration #

Open calibre/default.nix in any text editor, and copy the following


{ config, pkgs, ... }:
{
  services = {
    calibre-web = {
      enable = true;

      listen = {
        ip = "127.0.0.1";
        port = 3020;
      };

      options = {
        calibreLibrary = "/var/lib/calibre-web/library";
        enableBookUploading = true;
      };
    };

    nginx = {
      virtualHosts."library.<YOUR-DOMAIN>" = {
        enableACME = true;
        forceSSL = true;

        locations."/" = {
          proxyPass = "http://127.0.0.1:3020";
          proxyWebsockets = true;
        };
      };
    };
  };

  systemd.services.calibre-web = {
    preStart = ''
      if [ ! -f "/var/lib/calibre-web/library/metadata.db" ]; then
        mkdir -p "/var/lib/calibre-web/library"
        ${pkgs.calibre}/bin/calibredb add --empty \
          --with-library "/var/lib/calibre-web/library"
      fi
    '';
  };
}

Then to enable it, just include it in the server config file:

  imports = [
    # ... other services
    ./calibre
    # ... other services
  ];

and you’re done.

Let’s break the config file down.

explanation #

  1. We declare the calibre-web service as enabled
  2. We configure the port to 3020, and make it so it won’t reach out to the web outside of the reverse proxy
  3. We set the library directory (where calibre-web will store your books), and enable uploading books via the web interface
  4. We set up the reverse proxy to allow you access through the web
  5. We set up a systemd service that runs when the calibre-web service runs, to verify that the location you set as a library is indeed a calibre database folder. If not, it initialised a .db file.

further setup #

Once the service is up and running, go to localhost:3020, or library.<YOUR-DOMAIN>, or whatever domain you set it to.

You should see a login screen. The default username is admin and the default password is admin123.

If you serve this over the open internet CHANGE THE DEFAULT USERNAME AND PASSWORD IMMEDIATELY AFTER LOGGING IN. Otherwise everyone will have access to your library and data.