rabbitmq.nix 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9
{ lib, pkgs, config, testing, ... }:
let

in {

  ## module options for queue deployment

  options.org.techcultivation.deploy.queue = {
    enable = lib.mkEnableOption "queue container";
10
    dockerImage.enable = lib.mkEnableOption "queue container docker image";
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
  };

  ## option implementation

  config =
    let
      nixosConfig = {
        # TODO
      };

      dockerConfig =
        let

          dockerImage =
            let
              # The erlang closure is giant by default;
              # rabbitmq doesn’t need most of it.
              # see https://github.com/rabbitmq/erlang-rpm
              # It still compiles some OTP libraries like wx,
              # that could be removed (~30–40M).
              rabbitmq_server-minimal = pkgs.rabbitmq_server.override {
                erlang = pkgs.erlang.override {
                  javacSupport = false;
                  odbcSupport = false;
                  wxSupport = false;
                  # prevent doc generation (>100M)
                  installTargets = ["install"];
                };
              };
40 41 42 43
              sh-minimal = pkgs.runCommand "sh-minimal" {} ''
                mkdir -p $out/bin
                cp ${pkgs.dash}/bin/dash $out/bin/sh
              '';
44

45 46 47 48
              userName = "rabbitmq";

              image = pkgs.helpers.createStandaloneDockerImage {
                name = "sangha-rabbitmq";
49
                tag = "unstable";
50
                description = "TODO";
51 52 53 54 55 56 57

                contents = [
                  rabbitmq_server-minimal
                  # only needed by the /bin/rabbitmq-env script,
                  # 11M that can probably be avoided
                  pkgs.coreutils
                  pkgs.gnused
58
                  sh-minimal
59 60 61
                ];

                config = {
62
                  User = userName;
63 64 65 66 67 68 69
                  Cmd = [ "rabbitmq-server" ];
                  Env = [
                    # logs to stdout
                    "RABBITMQ_LOGS=-"
                    "RABBITMQ_SASL_LOGS=-"
                    "HOME=/var/lib/rabbitmq"
                  ];
70
                  ExposedPorts."${toString 5672}/tcp" = {};
71 72
                };

73
                rootSetupScript = ''
74 75
                  # rabbit setup
                  mkdir -p /var/lib/rabbitmq/mnesia
76
                  chown --recursive ${userName}:${userName} /var/lib/rabbitmq
77

78 79 80 81
                  # add a symlink to the .erlang.cookie in /root
                  # so we can "docker exec rabbitmqctl ..." without gosu
                  # mkdir /root
                  # ln -sf /var/lib/rabbitmq/.erlang.cookie /root/
82 83 84 85 86 87 88 89 90 91 92 93 94 95
                '';
              };
            in image;

        in {
          generated.dockerImages.sanghaRabbitmq = dockerImage;
        };

    in
      let
        cfg = config.org.techcultivation.deploy.queue;
      in
        lib.mkMerge [
          (lib.mkIf cfg.enable nixosConfig)
96
          (lib.mkIf cfg.dockerImage.enable dockerConfig)
97 98 99 100
        ];


}