elevation-server.nix (2647B)
1 { 2 config, 3 lib, 4 pkgs, 5 ... 6 }: 7 8 with lib; 9 10 let 11 cfg = config.services.elevation-server; 12 in 13 { 14 options.services.elevation-server = { 15 enable = mkEnableOption "elevation-server"; 16 package = mkPackageOption pkgs "elevation-server" { }; 17 address = mkOption { 18 type = types.str; 19 default = "127.0.0.1"; 20 description = "Address to bind to."; 21 }; 22 port = mkOption { 23 type = types.port; 24 default = 8080; 25 description = "Port to listen."; 26 }; 27 threads = mkOption { 28 type = types.int; 29 default = 10; 30 description = "Maximum number of concurrently served requests."; 31 }; 32 demTiles = mkOption { 33 type = types.path; 34 default = "/srv/tilesets/dem_tiles"; 35 description = "The path to file with elevation tile."; 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.elevation-server = { 65 description = "Elevation server"; 66 after = [ "network.target" ]; 67 wantedBy = [ "multi-user.target" ]; 68 serviceConfig = { 69 DynamicUser = true; 70 LogsDirectory = "elevation-server"; 71 ExecStart = "${getBin cfg.package}/bin/elevation_server -dem ${cfg.demTiles} -host ${cfg.address} -port ${toString cfg.port} -threads ${toString cfg.threads}"; 72 Restart = "always"; 73 }; 74 }; 75 } 76 (mkIf cfg.nginx.enable { 77 services.nginx = { 78 enable = true; 79 virtualHosts."${cfg.nginx.hostName}" = { 80 locations."/" = { 81 proxyPass = "http://${cfg.address}:${toString cfg.port}"; 82 extraConfig = '' 83 more_clear_headers Access-Control-Allow-Origin; 84 more_clear_headers Access-Control-Allow-Credentials; 85 more_set_headers 'Access-Control-Allow-Origin: $http_origin'; 86 more_set_headers 'Access-Control-Allow-Credentials: true'; 87 more_set_headers 'Cache-Control: max-age=86400'; 88 more_set_headers 'Vary: Origin'; 89 ''; 90 }; 91 }; 92 }; 93 }) 94 ]); 95 }