summaryrefslogtreecommitdiff
path: root/content/theses/self-sufficiency/books.md
blob: 2bf583c3c255d88dc40c24cab40bb72002c4dec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: books
weight: 30
---
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
```sh
$ 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
```nix

{ 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:
```nix
  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`.
{{% hint danger %}}
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.
{{% /hint %}}
Directive (EU) 2019/790, Article 4(3); all rights regarding Text and Data Mining (TDM) are reserved.