summaryrefslogtreecommitdiff
path: root/pkgs/bookwyrm.nix
blob: 7a5daecd815f7fac36fb9d3ad6a55b20a104d4cf (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{ lib
, fetchFromGitHub
, python
, writeShellScriptBin
, writeText
, settings ? { }
}:

let
  # set some dummy values to make the package build
  settingsWithDefaults = {
    DOMAIN = "localhost";
    DEBUG = false;
    USE_HTTPS = false;
    EMAIL = "your@email.here";
    PGPORT = 5432;
    POSTGRES_USER = "bookwyrm";
    POSTGRES_DB = "bookwyrm";
    POSTGRES_HOST = "localhost";
    REDIS_ACTIVITY_HOST = "localhost";
    REDIS_ACTIVITY_PORT = 6379;
    REDIS_BROKER_HOST = "localhost";
    REDIS_BROKER_PORT = 6379;
    EMAIL_HOST = "smtp.example.com";
    EMAIL_PORT = 587;
    EMAIL_HOST_USER = "mail@example.org";
    EMAIL_HOST_PASSWORD = "blub";
    MEDIA_ROOT = "/var/lib/bookwyrm/images";
  } // settings;

  # toShellVar produces "" for false, which bookwyrm rejects
  toDjangoVar = name: value: lib.toShellVar name
    (if value == false then "false" else
      (if value == true then "true" else value));

  envfile = writeText "bookwyrm.env"
    (lib.strings.concatLines
      (lib.mapAttrsToList toDjangoVar settingsWithDefaults));

  bookwyrm = python.pkgs.buildPythonApplication rec {
    pname = "bookwyrm";
    version = "0.7.2";

    format = "other";

    src = fetchFromGitHub {
      owner = "bookwyrm-social";
      repo = "bookwyrm";
      rev = "refs/tags/v${version}";
      hash = "sha256-5QhIHpNUn65qTh7ARlnGfUESoxw8hqFaoS2D2z+OSlM=";
    };

    propagatedBuildInputs = with python.pkgs; [
      aiohttp
      bleach
      celery
      colorthief
      django
      django-celery-beat
      bw-file-resubmit
      django-compressor
      django-imagekit
      django-model-utils
      django-sass-processor
      django-csp
      environs
      flower
      gunicorn
      libsass
      markdown
      packaging
      pillow
      psycopg2
      pycryptodome
      dateutil
      redis
      requests
      responses
      pytz
      boto3
      django-storages
      django-redis
      opentelemetry-api
      opentelemetry-exporter-otlp-proto-grpc
      # opentelemetry-instrumentation-celery
      opentelemetry-instrumentation-django
      # opentelemetry-instrumentation-pyscopg2
      opentelemetry-sdk
      protobuf
      pyotp
      qrcode
      grpcio
    ];

    postBuild = ''
      ln -s ${envfile} .env
      # needed for the python settings.py file to not fail, but not
      # used during the commands executed below, so this is safe
      export SECRET_KEY=fnord

      substituteInPlace contrib/systemd/* \
        --replace /opt/bookwyrm/venv/bin/gunicorn ${lib.getExe python.pkgs.gunicorn} \
        --replace /opt/bookwyrm/venv/bin/celery ${lib.getExe' python.pkgs.celery "celery"} \
        --replace /opt/bookwyrm $out

      sed -i /BindPath/d contrib/systemd/*

      python manage.py compile_themes
      python manage.py collectstatic --no-input
    '';

    postInstall = ''
      mkdir -p $out/lib/systemd/system
      cp -r * .env $out
      cp -r contrib/systemd/* $out/lib/systemd/system
    '';

    passthru = {
      pythonPath = python.pkgs.makePythonPath propagatedBuildInputs;
      gunicorn = python.pkgs.gunicorn;
      celery = python.pkgs.celery;
      manage = environmentFile: writeShellScriptBin "bookwyrm-manage.py" ''
        set -a
        ${if environmentFile != null
          then "source ${environmentFile}"
          else ""}
        export PYTHONPATH=${passthru.pythonPath}
        cd ${bookwyrm.outPath}
        exec ${bookwyrm.outPath}/manage.py "$@"
      '';
    };

    # hacky hacky hack
    shellHook = ''
      export PYTHONPATH=${passthru.pythonPath}
    '';
  };
in bookwyrm