nur-packages

My NUR packages
git clone git://git.sikmir.ru/nur-packages
Log | Files | Refs | README | LICENSE

mbtileserver.nix (2713B)


      1 {
      2   config,
      3   lib,
      4   pkgs,
      5   ...
      6 }:
      7 
      8 with lib;
      9 
     10 let
     11   cfg = config.services.mbtileserver;
     12   description = "MBTiles server";
     13 in
     14 {
     15   options.services.mbtileserver = {
     16     enable = mkEnableOption description;
     17     package = mkPackageOption pkgs "mbtileserver" { };
     18     address = mkOption {
     19       type = types.str;
     20       default = "127.0.0.1";
     21       description = "IP address to listen on.";
     22     };
     23     port = mkOption {
     24       type = types.port;
     25       default = 8000;
     26       description = "Server port.";
     27     };
     28     tileDir = mkOption {
     29       type = types.path;
     30       default = "/srv/tilesets";
     31       description = "The path where *.mbtiles files stored.";
     32     };
     33     tilesOnly = mkOption {
     34       type = types.bool;
     35       default = true;
     36       description = "Only enable tile endpoints.";
     37     };
     38     nginx = mkOption {
     39       default = { };
     40       description = ''
     41         Configuration for nginx reverse proxy.
     42       '';
     43       type = types.submodule {
     44         options = {
     45           enable = mkOption {
     46             type = types.bool;
     47             default = false;
     48             description = ''
     49               Configure the nginx reverse proxy settings.
     50             '';
     51           };
     52           hostName = mkOption {
     53             type = types.str;
     54             description = ''
     55               The hostname use to setup the virtualhost configuration
     56             '';
     57           };
     58         };
     59       };
     60     };
     61   };
     62 
     63   config = mkIf cfg.enable (mkMerge [
     64     {
     65       systemd.services.mbtileserver = {
     66         inherit description;
     67         after = [ "network.target" ];
     68         wantedBy = [ "multi-user.target" ];
     69         environment.TILE_DIR = cfg.tileDir;
     70         serviceConfig = {
     71           DynamicUser = true;
     72           LogsDirectory = "mbtileserver";
     73           ExecStart = "${getExe cfg.package} --enable-fs-watch ${lib.optionalString cfg.tilesOnly "--tiles-only"} --host ${cfg.address} --port ${toString cfg.port}";
     74           Restart = "always";
     75         };
     76       };
     77     }
     78     (mkIf cfg.nginx.enable {
     79       services.nginx = {
     80         enable = true;
     81         virtualHosts."${cfg.nginx.hostName}" = {
     82           locations."/" = {
     83             root = cfg.tileDir;
     84             extraConfig = ''
     85               autoindex on;
     86               autoindex_exact_size off;
     87             '';
     88           };
     89           locations."/services" = {
     90             proxyPass = "http://${cfg.address}:${toString cfg.port}";
     91             extraConfig = ''
     92               #proxy_set_header Host ''$host;
     93               #proxy_set_header X-Forwarded-Host ''$server_name;
     94               #proxy_set_header X-Real-IP ''$remote_addr;
     95               add_header Cache-Control 'public, max-age=3600, must-revalidate';
     96             '';
     97           };
     98         };
     99       };
    100     })
    101   ]);
    102 }