nur-packages

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

mbtileserver.nix (2689B)


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