Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

atomize entire config, support nix-caches

+67 -35
+61 -31
config/runtime.exs
··· 54 54 } 55 55 } 56 56 }, 57 - "public_url" => %{ 58 - "type" => "string", 59 - "format" => "uri" 60 - }, 61 57 "listen_address" => %{ 62 58 "type" => "string", 63 59 "format" => "ipv4" ··· 68 64 "minimum" => 80, 69 65 "maximum" => 65535 70 66 }, 67 + "nix_caches" => %{ 68 + "type" => "array", 69 + "items" => %{ 70 + "type" => "object", 71 + "properties" => %{ 72 + "public_key" => %{ 73 + "type" => "string" 74 + }, 75 + "url" => %{ 76 + "type" => "string", 77 + "format" => "uri" 78 + } 79 + } 80 + }, 81 + "required" => ["public_key"] 82 + }, 83 + "public_url" => %{ 84 + "type" => "string", 85 + "format" => "uri" 86 + }, 71 87 "secret_key_base_file" => %{ 72 88 "type" => "string" 73 89 } ··· 85 101 with {:ok, contents} <- File.read(config_file), 86 102 {:ok, json} <- Jason.decode(contents), 87 103 :ok <- ExJsonSchema.Validator.validate(ExJsonSchema.Schema.resolve(@schema), json) do 88 - json 104 + json |> atomize() 89 105 else 90 - {:error, _err} -> 106 + {:error, err} -> 91 107 Logger.error(~s"Failed to read configuration file #{config_file}") 108 + Logger.error(err) 92 109 Kernel.exit(1) 93 110 end 94 111 95 112 # set log level to atom and remove from config 96 - if Map.has_key?(json_config, "log_level") do 97 - level = Map.get(json_config, "log_level") |> String.to_existing_atom() 113 + if Keyword.has_key?(json_config, :log_level) do 114 + level = Keyword.get(json_config, :log_level) |> String.to_existing_atom() 98 115 Logger.info(~s"Overriding log level from config to #{level}") 99 116 100 117 config :logger, :console, level: level ··· 102 119 103 120 Logger.debug("Loaded configuration") 104 121 Logger.debug(json_config) 105 - json_config = json_config |> Map.delete("log_level") 122 + json_config = json_config |> Keyword.delete(:log_level) 106 123 107 124 # load some defaults 108 - public_url = json_config |> Map.get("public_url", "http://127.0.0.1:4000") 109 - put_config(:auth, oidc_redirect_uri: ~s"#{public_url}/auth") 110 - listen_address = json_config |> Map.get("listen_address", "127.0.0.1") 111 - listen_port = json_config |> Map.get("listen_port", 4000) 125 + public_url = json_config |> Keyword.get(:public_url, "http://127.0.0.1:4000") 126 + 127 + json_config = 128 + json_config 129 + |> Keyword.put( 130 + :auth, 131 + json_config 132 + |> Keyword.get(:auth) 133 + |> Keyword.put(:oidc_redirect_uri, ~s"#{public_url}/auth") 134 + ) 135 + 136 + listen_address = json_config |> Keyword.get(:listen_address, "127.0.0.1") 137 + listen_port = json_config |> Keyword.get(:listen_port, 4000) 112 138 113 139 secret_key_base = 114 - with %{"secret_key_base_file" => secret_key_base_file} <- json_config, 140 + with secret_key_base_file <- json_config |> Keyword.get(:secret_key_base_file), 115 141 {:ok, secret_key_base} <- read_credential(secret_key_base_file) do 116 142 secret_key_base 117 143 else ··· 119 145 Logger.warning("Failed to load secret_key_base from secret file, #{err}.") 120 146 Kernel.exit(1) 121 147 122 - %{} -> 148 + [_ | _] -> 123 149 Logger.warning("No secret_key_base_file in configuration. Exiting!") 124 150 Kernel.exit(1) 125 151 end 126 152 127 153 json_config = 128 - with %{"database" => database} <- json_config, 129 - %{"password_file" => password_file} <- database, 154 + with database <- json_config |> Keyword.get(:database), 155 + password_file <- database |> Keyword.get(:password_file), 130 156 {:ok, password} <- read_credential(password_file) do 131 - json_config |> Map.put("database", database |> Map.put("password", password)) 157 + json_config |> Keyword.put(:database, database |> Keyword.put(:password, password)) 132 158 else 133 159 # assume missing password_file is intentional 134 - %{} -> 160 + [_ | _] -> 135 161 Logger.debug("No database password_file to read.") 136 162 json_config 137 163 ··· 141 167 end 142 168 143 169 json_config = 144 - with %{"auth" => auth} <- json_config, 145 - %{"oidc_client_secret_file" => oidc_client_secret_file} <- auth, 170 + with auth <- json_config |> Keyword.get(:auth), 171 + oidc_client_secret_file <- auth |> Keyword.get(:oidc_client_secret_file), 146 172 {:ok, oidc_client_secret} <- read_credential(oidc_client_secret_file) do 147 - json_config |> Map.put("auth", auth |> Map.put("oidc_client_secret", oidc_client_secret)) 173 + json_config 174 + |> Keyword.put(:auth, auth |> Keyword.put(:oidc_client_secret, oidc_client_secret)) 148 175 else 149 176 {:error, err} -> 150 177 Logger.warning("Failed to load oidc_client_secret from secret file, #{err}.") 151 178 Kernel.exit(1) 152 179 153 - %{} -> 180 + [_ | _] -> 154 181 Logger.warning("No auth.oidc_client_secret_file in configuration. Exiting!") 155 182 Kernel.exit(1) 156 183 end ··· 158 185 Logger.debug("Modified configuration") 159 186 Logger.debug(json_config) 160 187 161 - json_config |> Enum.map(&load_config(&1)) 188 + json_config |> Enum.map(fn {k, v} -> put_config(k, v) end) 162 189 163 190 # load some non-app namespaced configs 164 191 %URI{scheme: scheme, host: host, port: port} = URI.parse(public_url) ··· 173 200 Logger.info("Finished loading configuration") 174 201 end 175 202 176 - defp load_config({config_atom, values}) when is_map(values) do 177 - config_atom = String.to_atom(config_atom) 178 - values = Keyword.new(values, fn {k, v} -> {String.to_atom(k), v} end) 179 - put_config(config_atom, values) 203 + defp atomize([head | rest]) do 204 + [atomize(head), atomize(rest)] |> List.flatten() 180 205 end 181 206 182 - defp load_config({config_atom, value}) when is_binary(value) or is_number(value) do 183 - config_atom = String.to_atom(config_atom) 184 - put_config(config_atom, value) 207 + defp atomize(map = %{}) do 208 + map 209 + |> Enum.map(fn {k, v} -> {String.to_atom(k), atomize(v)} end) 185 210 end 211 + 212 + defp atomize(nil), do: nil 213 + defp atomize(other), do: other 186 214 187 215 defp read_credential(path) when is_binary(path) do 188 216 case path |> File.read() do ··· 193 221 other 194 222 end 195 223 end 224 + 225 + defp read_credential(nil), do: {:error, :is_nil} 196 226 197 227 defp put_config(config_atom, new_values) when is_atom(config_atom) and is_list(new_values) do 198 228 config =
+6 -4
dev-server.json
··· 13 13 "password_file": "VERSION" 14 14 }, 15 15 "listen_port": 4000, 16 - "nix_cache": { 17 - "url": "https://cache.junco.dev/v3", 18 - "public_key": "v3:aMXMnngJoGU8dpELPyNAhADrOgrn5GiXWP90NiB4aFY=" 19 - }, 16 + "nix_caches": [ 17 + { 18 + "url": "https://cache.junco.dev/v3", 19 + "public_key": "v3:aMXMnngJoGU8dpELPyNAhADrOgrn5GiXWP90NiB4aFY=" 20 + } 21 + ], 20 22 "client_bin": { 21 23 "x86_64-linux": "/nix/store/path-to-client" 22 24 },