rebar3 icon indicating copy to clipboard operation
rebar3 copied to clipboard

How to use rebar3 3.18.0 to set up my hexpm server?

Open tothlac opened this issue 2 years ago • 3 comments

What is the easiest way using the current 3.18.0 version of rebar3 to configure my hexpm server where all my required pkgs are mirrored?

In earlier versions of rebar3 there was a rebar_packages_cdn option that I could use in rebar.config, and there was a $HEX_CDN os environment option which could be easily used to define where hex packages are located on the local network.

As I see these options are not used anymore...

Actually, I see rebar_packages_cdn is still used in rebar3.erl:

    HexCDN = case os:getenv("HEX_CDN") of
                 false -> ?DEFAULT_CDN;
                 [] -> ?DEFAULT_CDN;
                 CDN -> CDN
             end,
    State2 = rebar_state:set(State1, rebar_packages_cdn, HexCDN),

I can't see any other parts of the code where this rebar_packages_cdn is used.

I was trying to set up my hexpm server differently today using this page:

http://rebar3.org/docs/configuration/configuration/#hex-repos-and-indexes

I put the following lines into ~/.config/rebar3/hex.config:

{hex, [
   {repos, [
      #{name => <<"myhexpm">>,
        api_url => <<"https://myhexpm.com:8081/api">>,
        repo_url => <<"https://myhexpm.com:8081">>
      }
   ]}
]}.

Using this I've got this error, looks like the file should contain a simple map, and it seems to me there is no option to configure multiple servers here using a list as it is described on the above-mentioned page.

===> Uncaught error: {badmap,
                             {hex,
                              [{repos,
                                [#{api_url =>
                                    <<"https://myhexpm.com:8081">>,
                                   name => <<"hexpm">>,
                                   repo_url =>
                                    <<"https://myhexpm.com:8081">>}]}]}}
===> Stack trace to the error location:
[{maps,get,
       [<<"hexpm">>,
        {hex,[{repos,[#{api_url => <<"https://myhexpm.com:8081">>,
                        name => <<"hexpm">>,
                        repo_url =>
                            <<"https://myhexpm.com:8081">>}]}]},
        #{}],
       [{file,"maps.erl"},{line,188}]},
 {rebar_hex_repos,'-merge_with_base_and_auth/3-lc$^0/1-0-',3,
                  [{file,"..."},
                   {line,80}]},
 {rebar_hex_repos,from_state,2,
                  [{file,"..."},
                   {line,40}]},
 {rebar_pkg_resource,init,2,
                     [{file,"..."},
                      {line,35}]},
 {rebar_state,add_resource,2,
              [{file,"..."},
               {line,391}]},
 {lists,foldl,3,[{file,"lists.erl"},{line,1267}]},
 {rebar3,run_aux,2,
         [{file,"..."},{line,149}]},
 {rebar3,main,1,
         [{file,"..."},{line,66}]}]

I was also trying to recreate this file using rebar3_hex plugin and calling rebar3 hex user auth command, which created the following contents in the hex.config file:

%% coding: utf-8
#{<<"myhexpm">> =>
      #{read_key => <<"...">>,
        repo_key => <<"...">>,
        username => <<"tothlac">>,
        api_url => <<"https://myhexpm.com:8081/repo/hexpm">>,
        repo_url => <<"https://myhexpm.com:8081/repo">>,
        write_key =>
            {<<...>>,
             {<<...>>,
              <<...>>}}}}.

Using those options when I disabled hex.pm in my /etc/hosts I still had this problem:

===> Getting definition for package jsx from repo hexpm
===> Hex get_package request failed: {error,
                                             {failed_connect,
                                              [{to_address,
                                                {"repo.hex.pm",443}},
                                               {inet,[inet],econnrefused}]}}

So somehow it was still trying to connect to the default repo.hex.pm.

Is this hex.config file the easiest way to configure my hexpm server, or are there still some easier options for that? In case this is the only option can you help me to figure out what am I missing?

tothlac avatar Apr 13 '22 18:04 tothlac

Hi, I think the problem here is the docs aren't clear enough :

{hex, [
   {repos, [
      #{name => <<"myhexpm">>,
        api_url => <<"https://myhexpm.com:8081/api">>,
        repo_url => <<"https://myhexpm.com:8081">>
      }
   ]}
]}.

The configuration format above is meant to go in your project's rebar.config or your global rebar.config.

%% coding: utf-8
#{<<"myhexpm">> =>
      #{read_key => <<"...">>,
        repo_key => <<"...">>,
        username => <<"tothlac">>,
        api_url => <<"https://myhexpm.com:8081/repo/hexpm">>,
        repo_url => <<"https://myhexpm.com:8081/repo">>,
        write_key =>
            {<<...>>,
             {<<...>>,
              <<...>>}}}}.

Of course, doing that means you need to call this <<"hexpm">> vs <<"myhexpm">>, if you're indeed wishing to always use you hexpm instance vs the public hexpm.

rebar3_hex will introduce a config provider soon that will ease manipulating this file, in general one should never alter this file directly, but you simply have no other choice at the moment, so quite understandable.

Does putting the config you originally tried in rebar.config (either project or global level) resolve the issue?

starbelly avatar Apr 21 '22 00:04 starbelly

Having put what you've sent into my rebar.config, I got this error:

===> Unable to parse config. Term is not in {Key, Value} format:
#{<<"myhexpm">> =>
      #{api_url => <<"https://myhexpm.com:8081/repo/hexpm">>,
        read_key => <<"...">>,repo_key => <<"...">>,
        repo_url => <<"https://myhexpm.com:8081/repo">>,
        username => <<"tothlac">>,
        write_key => {<<"...">>,{<<"...">>,<<"...">>}}}}

I've tried to change it to a proplist, like this:

{<<"myhexpm">>,
      #{read_key => <<"...">>,
        repo_key => <<"...">>,
        username => <<"tothlac">>,
        api_url => <<"https://myhexpm.com:8081/repo/hexpm">>,
        repo_url => <<"https://myhexpm.com:8081/repo">>,
        write_key =>
            {<<"...">>,
             {<<"...">>,
              <<"...">>}}}}.

In that case it still tries to connect to hex.pm

===> Request for package hackney-1.17.0 failed: {failed_connect, [{to_address, {"repo.hex.pm",443}}, {inet, [inet], econnrefused}]}

tothlac avatar Apr 25 '22 14:04 tothlac

@tothlac Right, the form has to be :

{hex, [
   {repos, [
      #{name => <<"myhexpm">>,
        api_url => <<"https://myhexpm.com:8081/api">>,
        repo_url => <<"https://myhexpm.com:8081">>
      }
   ]}
]}.

Edit:

This is old and was a quick write up that should be formalized into a proper document, but this may help : https://gist.github.com/starbelly/e7c0604513e41d0a2af456aa7abcc8a2

starbelly avatar Apr 27 '22 00:04 starbelly