How To Add Dependency Sourced From Bitbucket Private Repository?

If this is the Hex.pm version:

def deps do
[{:repository_name, “~> 0.1.0”}]
end

What’s the Bitbucket private repository equivalent?

  • How/where do I specify repository access credentials?

  • How do I define the repository location?

When You run

$ mix help deps

Dependencies must be specified in the mix.exs file in one of the following
formats:

{app, requirement}
{app, opts}
{app, requirement, opts}

Where:

• app is an atom
• requirement is a version requirement or a regular expression
• opts is a keyword list of options

By default, dependencies are fetched using the Hex package manager
(https://hex.pm/):

{:plug, ">= 0.4.0"}

By specifying such dependencies, Mix will automatically install Hex (if it
wasn’t previously installed) and download a package suitable to your project.

Mix also supports Git and path dependencies:

{:foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1"}
{:foobar, path: "path/to/foobar"}

And also in umbrella dependencies:

{:my_app, in_umbrella: true}

So my bet is You cannot do it, but You can use path dependencies

So I imagine the workaround would be adding a local file dependency, and downloading the private repo to local first?

And if that’s the solution, then I would simply add the local file path in the path atom?

Yes, as a workaround

No. You simply specify a git dependency using git protocol (git@bitbucket.com:...). Everything else is handled by your local git and ssh installs.

7 Likes

Exactly! If mix then has problems downloading it because ssh requires a password, then you can use ssh-copy-id and/or the SSH-settings in Bitbucket to make this problem go away :slight_smile: .

2 Likes

oh, nice to learn it is possible, thanks for answering

May I ask a question on this?

Let us assume that like explained here I fetched a bitbucket repository of mine as dependency in some Elixir project.

Is it not recommended to edit source code in my_project/deps/my_lib so that I can push it to remote repo whenever I want?

I would not edit a dependency in your deps folder, no. Instead you should clone the dependency, make your changes there, and submit it back upstream if it’s an external dependency or just push it if it’s something you control. Then pull in the updated version in your current project.

2 Likes

I would clone the repo locally and use a path: "/.../..." dependency in mix.exs. Mix is smart enough to recompile when you edit your code, so it is (to me) the best way to work on a project and on a dependency together.

Of course it is cumbersome if you work with other people, but in my team we use paths like "../some-other/project" so siblings projects are always siblings on the path level too. Otherwise you can use one more directory level in the main project and add the dependency as a git submodule (and keep path: "../other-project" in mix.exs.

2 Likes