Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReleaseRuntimeProvider for exs config at runtime #3705

Closed
wants to merge 1 commit into from

Conversation

kloenk
Copy link

@kloenk kloenk commented Jan 21, 2024

Changes

This lets the user define elixir code as config.

I plane to use this in my approach for OIDC as with this the config can easily define functions to apply roles base on claims in the token.

Tests

  • Automated tests have been added
  • This PR does not require tests

Changelog

  • Entry has been added to changelog
  • This PR does not make a user-facing change
    At this time the config is just an extra way to configure, so the user does not yet have to be aware of it.

Documentation

  • Docs have been updated
  • This change does not need a documentation update

Dark mode

  • The UI has been tested both in dark and light mode
  • This PR does not change the UI

This lets the user define elixir code as config
@kloenk kloenk mentioned this pull request Jan 21, 2024
8 tasks
@ruslandoga
Copy link
Contributor

ruslandoga commented Feb 20, 2024

👋 @kloenk

I think this should be already possible by mounting a custom config/runtime.exs

$ docker run --rm -t ghcr.io/ruslandoga/analytics:my-build ls releases/0.0.1
consolidated        iex                 runtime.exs         start_clean.boot    vm.args
elixir              plausible.rel       start.boot          start_clean.script
env.sh              remote.vm.args      start.script        sys.config
$ docker run --rm -t ghcr.io/ruslandoga/analytics:my-build head releases/0.0.1/runtime.exs
import Config
import Plausible.ConfigHelpers
require Logger

if config_env() in [:dev, :test] do
  Envy.load(["config/.env.#{config_env()}"])
end

if config_env() == :small_dev do
  Envy.load(["config/.env.dev"])
$ docker run --rm -t -v ./config.exs:/app/releases/0.0.1/runtime.exs ghcr.io/ruslandoga/analytics:my-build head releases/0.0.1/runtime.exs
import Config

raise "oops"
$ docker run --rm -t -v ./config.exs:/app/releases/0.0.1/runtime.exs ghcr.io/ruslandoga/analytics:my-build
ERROR! Config provider Config.Reader failed with:
** (RuntimeError) oops
    /app/releases/0.0.1/runtime.exs:3: (file)
    (elixir 1.16.0) src/elixir.erl:405: :elixir.eval_external_handler/3
    (stdlib 5.2) erl_eval.erl:750: :erl_eval.do_apply/7
    (elixir 1.16.0) src/elixir.erl:378: :elixir.eval_forms/4
    (elixir 1.16.0) lib/module/parallel_checker.ex:112: Module.ParallelChecker.verify/1
    (elixir 1.16.0) lib/code.ex:568: Code.validated_eval_string/3
    (elixir 1.16.0) lib/config.ex:290: Config.__eval__!/3
    (elixir 1.16.0) lib/config/reader.ex:92: Config.Reader.read!/2

@ruslandoga ruslandoga self-assigned this Feb 20, 2024
@kloenk
Copy link
Author

kloenk commented Feb 20, 2024

As I don't use docker and my package gets installed into a immutable place (nix) this is less convenient. So the approach in this PR would be favorable for me, but I could work around it as well.

@ruslandoga ruslandoga added the self-hosting Anything self-hosted label Mar 17, 2024
@ruslandoga
Copy link
Contributor

ruslandoga commented Mar 17, 2024

Right, I think allowing extra config might be a useful escape hatch for the configurations not covered by the current env vars (like #3799). I explored a bit and it seems like it isn't necessary to implement a custom config reader. The default one could be reused and there are multiple ways to do it, here's the approach I like:

  1. we would add an intermediary config that allows import

mix.exs

  defp releases do
    [
      plausible: [
        config_providers: [
          {Config.Reader, path: {:system, "RELEASE_ROOT", "/import_config.exs"}, imports: []}
        ]
      ]
    ]
  end
  1. we would check for an env var like EXTRA_CONFIG_PATH and conditionally import another config

rel/overlays/import_config.exs

import Conifg

if extra_config_path = System.get_env("EXTRA_CONFIG_PATH") do
  import_config extra_config_path
end

PR: #3906

@ruslandoga ruslandoga mentioned this pull request Mar 17, 2024
4 tasks
@ruslandoga
Copy link
Contributor

ruslandoga commented Mar 19, 2024

I'm going to close this as #3906 has been merged.

@ruslandoga ruslandoga closed this Mar 19, 2024
@ruslandoga
Copy link
Contributor

@kloenk thank you!

@ruslandoga ruslandoga removed their assignment Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
self-hosting Anything self-hosted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants