fireproofsocks

fireproofsocks

Is it possible to customize the location of the priv/ directory? From what I understand, this is part of the OTP directory structure, so it may be hard-coded out of necessity. There are some cases (particularly when code interacts with other languages e.g. NodeJS) when it would be helpful to be able to configure the location of static assets to be somewhere other than priv/. I’ve had some success using the overlays option with releases, but it’s not quite the same as having the assurance that an entire directory will exist in a predictable place.

Can anyone shed some light on this? TIA!

Showing Posts 1 to 1

Eiji

Eiji

This is more Erlang-related question than Elixir. On one hand having a path to some directory in let’s say environment variable is good idea. Elixir on its own is flexible and does not force much things like many languages/frameworks. However it’s based on Erlang and this is a bit different story.

Returns the path to the priv directory in an application. Equivalent to code:lib_dir(Name, priv)

Source: https://www.erlang.org/doc/man/code#priv_dir-1

Returns the path to a subdirectory directly under the top directory of an application. Normally the subdirectories reside under the top directory for the application, but when applications at least partly reside in an archive, the situation is different. Some of the subdirectories can reside as regular directories while others reside in an archive file. It is not checked whether this directory exists.

Source: https://www.erlang.org/doc/man/code#lib_dir-2

Both lib_dir and priv_dir functions are essential in Erlang and trying to changing priv path may cause unexpected problems. Also priv files are usually packages into release, so even if it would be in different directory on dev/build machine then you would need to alter the packaging logic, so the files would be extracted in another location on prod machine.

Other solutions may also have their own downsides. Symbolic links may work without any problem or be not supported at all. The biggest problem here would be for example an existing symbolic link to not existing path. Creating directory would not work as same as without symlink.

Also have in mind that in priv directory there could be much more files than just assets. Trying to change priv location at least in long term perspective seems not worth the effort.

However that’s not all options we have, right? Elixir is flexible, so let’s try find something on Elixir side that works better in your use case … I would go in a bit different way of thinking. I see 2 options:

  1. Add a code which at runtime, right after application starts, changes some configuration files. That should be simple to do and this way other applications do not need to worry about paths as all of them could simply fetch a value of environment variable.

  2. Plug.Static-based solution - if assets are all you need then you should be fine with the plug’s flexibility.

  • :from - the file system path to read static assets from. It can be either: a string containing a file system path, an atom representing the application name (where assets will be served from priv/static), a tuple containing the application name and the directory to serve assets from (besides priv/static), or an MFA tuple.

Source: https://hexdocs.pm/plug/Plug.Static.html

This way allows not only to specify a different location for each environment, but also it’s easy to add checks and fall back just in case something bad would happen. That’s said … since it happens on runtime it could (but not need) to increase a response time …

plug Plug.Static, from: {MyApp.ModuleName, :func_name, [Mix.env()])

# …

def func_name(env) when env in ~w[dev test]a do
  # path on dev and CI machines
  :my_app
  |> :code.priv_dir()
  |> Path.join("static")
end

def func_name(_env) do
  # for example fetch an environment variable
  prod_path = get_prod_path()

  if File.exists?(prod_path) do
    # prod-only path
    prod_path
  else
    # fallback
    func_name(:dev)
  end
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews