about summary refs log tree commit diff
path: root/pkgs/tools/networking/maubot/wrapper.nix
blob: 6f4c577110be37eb48ff1bc910da406c701420e4 (plain) (blame)
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
{ lib
, symlinkJoin
, runCommand
, unwrapped
, python3
, formats
}:

let wrapper = { pythonPackages ? (_: [ ]), plugins ? (_: [ ]), baseConfig ? null }:
  let
    plugins' = plugins unwrapped.plugins;
    extraPythonPackages = builtins.concatLists (map (p: p.propagatedBuildInputs or [ ]) plugins');
  in
  symlinkJoin {
    name = "${unwrapped.pname}-with-plugins-${unwrapped.version}";

    inherit unwrapped;
    paths = lib.optional (baseConfig != null) unwrapped ++ plugins';
    pythonPath = lib.optional (baseConfig == null) unwrapped ++ pythonPackages python3.pkgs ++ extraPythonPackages;

    nativeBuildInputs = [ python3.pkgs.wrapPython ];

    postBuild = ''
      rm -f $out/nix-support/propagated-build-inputs
      rmdir $out/nix-support || true
      ${lib.optionalString (baseConfig != null) ''
        rm $out/${python3.sitePackages}/maubot/example-config.yaml
        substituteAll ${(formats.yaml { }).generate "example-config.yaml" (lib.recursiveUpdate baseConfig {
          plugin_directories = lib.optionalAttrs (plugins' != []) {
            load = [ "@out@/lib/maubot-plugins" ] ++ (baseConfig.plugin_directories.load or []);
          };
          # Normally it should be set to false by default to take it from package
          # root, but aiohttp doesn't follow symlinks when serving static files
          # unless follow_symlinks=True is passed. Instead of patching maubot, use
          # this non-invasive approach
          # XXX: would patching maubot be better? See:
          # https://github.com/maubot/maubot/blob/75879cfb9370aade6fa0e84e1dde47222625139a/maubot/server.py#L106
          server.override_resource_path =
            if builtins.isNull (baseConfig.server.override_resource_path or null)
            then "${unwrapped}/${python3.sitePackages}/maubot/management/frontend/build"
            else baseConfig.server.override_resource_path;
        })} $out/${python3.sitePackages}/maubot/example-config.yaml
        rm -rf $out/bin
      ''}
      mkdir -p $out/bin
      cp $unwrapped/bin/.mbc-wrapped $out/bin/mbc
      cp $unwrapped/bin/.maubot-wrapped $out/bin/maubot
      wrapPythonProgramsIn "$out/bin" "${lib.optionalString (baseConfig != null) "$out "}$pythonPath"
    '';

    passthru = {
      inherit unwrapped;
      python = python3;
      withPythonPackages = filter: wrapper {
        pythonPackages = pkgs: pythonPackages pkgs ++ filter pkgs;
        inherit plugins baseConfig;
      };
      withPlugins = filter: wrapper {
        plugins = pkgs: plugins pkgs ++ filter pkgs;
        inherit pythonPackages baseConfig;
      };
      withBaseConfig = baseConfig: wrapper {
        inherit baseConfig pythonPackages plugins;
      };
    };

    meta.priority = (unwrapped.meta.priority or 0) - 1;
  };
in
wrapper