nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

couchdb.nix (1955B)


      1 {
      2   config,
      3   lib,
      4   haravara,
      5   ...
      6 }: let
      7   cfg = config.services.haravara.couchdb;
      8 in {
      9   options.services.haravara.couchdb = {
     10     enable = lib.mkEnableOption "CouchDB database server";
     11     adminUser = lib.mkOption {
     12       type = lib.types.str;
     13       description = "CouchDB admin username";
     14     };
     15     adminPassFile = lib.mkOption {
     16       type = lib.types.nullOr lib.types.path;
     17       default = null;
     18       description = "Path to a file containing the CouchDB admin password";
     19     };
     20     publicDomain = lib.mkOption {
     21       type = lib.types.str;
     22       description = "Public domain for the CouchDB Fauxton UI (set to null to disable)";
     23     };
     24     port = lib.mkOption {
     25       type = lib.types.port;
     26       default = 5984;
     27       description = "Port for CouchDB to listen on";
     28     };
     29     bindAddress = lib.mkOption {
     30       type = lib.types.str;
     31       default = "127.0.0.1";
     32       description = "Address for CouchDB to bind to";
     33     };
     34     authLabel = haravara.authLabelOption;
     35   };
     36 
     37   config = lib.mkIf cfg.enable {
     38     services = {
     39       couchdb = {
     40         enable = true;
     41         inherit (cfg) bindAddress adminUser;
     42         adminPass = null;
     43         extraConfig = {
     44           chttpd.bind_address = cfg.bindAddress;
     45           httpd.bind_address = cfg.bindAddress;
     46           httpd.enable_cors = true;
     47           cors = {
     48             origins = "*";
     49             credentials = true;
     50             methods = "GET, POST, PUT, DELETE, HEAD, OPTIONS";
     51             headers = "accept, authorization, content-type, origin, referer";
     52           };
     53         };
     54       };
     55 
     56       caddy.virtualHosts = lib.mkIf (cfg.publicDomain != null) {
     57         "${cfg.publicDomain}".extraConfig = ''
     58           handle /_utils* {
     59             ${haravara.mkAuthImport cfg.authLabel}
     60             reverse_proxy ${cfg.bindAddress}:${toString cfg.port}
     61           }
     62           handle {
     63             reverse_proxy ${cfg.bindAddress}:${toString cfg.port}
     64           }
     65         '';
     66       };
     67     };
     68   };
     69 }