mbtileserver.nix (2543B)
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 nginx = mkOption { 34 default = { }; 35 description = '' 36 Configuration for nginx reverse proxy. 37 ''; 38 type = types.submodule { 39 options = { 40 enable = mkOption { 41 type = types.bool; 42 default = false; 43 description = '' 44 Configure the nginx reverse proxy settings. 45 ''; 46 }; 47 hostName = mkOption { 48 type = types.str; 49 description = '' 50 The hostname use to setup the virtualhost configuration 51 ''; 52 }; 53 }; 54 }; 55 }; 56 }; 57 58 config = mkIf cfg.enable (mkMerge [ 59 { 60 systemd.services.mbtileserver = { 61 inherit description; 62 after = [ "network.target" ]; 63 wantedBy = [ "multi-user.target" ]; 64 environment.TILE_DIR = cfg.tileDir; 65 serviceConfig = { 66 DynamicUser = true; 67 LogsDirectory = "mbtileserver"; 68 ExecStart = "${getExe cfg.package} --enable-fs-watch --tiles-only --host ${cfg.address} --port ${toString cfg.port}"; 69 Restart = "always"; 70 }; 71 }; 72 } 73 (mkIf cfg.nginx.enable { 74 services.nginx = { 75 enable = true; 76 virtualHosts."${cfg.nginx.hostName}" = { 77 locations."/" = { 78 root = cfg.tileDir; 79 extraConfig = '' 80 autoindex on; 81 autoindex_exact_size off; 82 ''; 83 }; 84 locations."/services" = { 85 proxyPass = "http://${cfg.address}:${toString cfg.port}"; 86 extraConfig = '' 87 #proxy_set_header Host ''$host; 88 #proxy_set_header X-Forwarded-Host ''$server_name; 89 #proxy_set_header X-Real-IP ''$remote_addr; 90 add_header Cache-Control 'public, max-age=3600, must-revalidate'; 91 ''; 92 }; 93 }; 94 }; 95 }) 96 ]); 97 }