Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rebar3_nix_init_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buildRebar3 {
"{ pkgs ? import <nixpkgs> { } }:
with pkgs.beamPackages;
rebar3Relx {
name = \"~s\";
pname = \"~s\";
version = \"0.1.0\";
src = ./.;
releaseType = \"~s\";
Expand Down
27 changes: 22 additions & 5 deletions src/rebar3_nix_lock_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ in self
beamDeps = [ ~s];
};").

-define(DEP_SUBDIR, "
~s = builder rec {
name = \"~s\";
version = \"~s\";
src = ~s;
beamDeps = [ ~s];
sourceRoot = \"${src.name}/~s\";
};").

-define(FETCH_HEX,
"fetchHex {
pkg = \"~s\";
Expand Down Expand Up @@ -97,29 +106,37 @@ format_error(Reason) ->

to_nix(AppInfo, AllDepsNames) ->
Name = rebar_app_info:name(AppInfo),
{Vsn, Src} = src(Name, rebar_app_info:source(AppInfo)),
{Vsn, Src, Subdir} = src(Name, rebar_app_info:source(AppInfo)),
Deps = [[BinName, " "] || BinName <- app_deps(AppInfo),
lists:member(BinName, AllDepsNames)],
io_lib:format(?DEP, [Name, Name, Vsn, Src, Deps]).
case Subdir of
root ->
io_lib:format(?DEP, [Name, Name, Vsn, Src, Deps]);
{subdir, Dir} ->
io_lib:format(?DEP_SUBDIR, [Name, Name, Vsn, Src, Deps, Dir])
end.

src(_, {pkg, PkgName, Vsn, _OldHash, Hash, _Repo}) ->
{Vsn, io_lib:format(?FETCH_HEX, [PkgName, Vsn, to_sri(Hash)])};
{Vsn, io_lib:format(?FETCH_HEX, [PkgName, Vsn, to_sri(Hash)]), root};
src(_, {git, Url, {ref, Ref}}) ->
case string:prefix(string:lowercase(Url), "https://github.com/") of
nomatch ->
Prefetch = ["nix-prefetch-git --quiet ", Url, " ", Ref],
{ok, Json} = rebar3_nix_utils:cmd(Prefetch),
{ok, #{<<"sha256">> := Hash}, _} =
rebar3_nix_jsone_decode_vendored:decode(unicode:characters_to_binary(Json)),
{"git", io_lib:format(?FETCH_GIT, [Url, Ref, Hash])};
{"git", io_lib:format(?FETCH_GIT, [Url, Ref, Hash]), root};
Path ->
[Owner, Repo0] = string:split(Path, "/", trailing),
Repo = re:replace(Repo0, "\\.git$", "", [{return, list}]),
Prefetch = ["nix-prefetch-url --unpack https://github.com/",
Owner, "/", Repo, "/tarball/", Ref],
{ok, Hash} = rebar3_nix_utils:cmd(Prefetch),
{"git", io_lib:format(?FETCH_FROM_GITHUB, [Owner, Repo, Ref, Hash])}
{"git", io_lib:format(?FETCH_FROM_GITHUB, [Owner, Repo, Ref, Hash]), root}
end;
src(Name, {git_subdir, Url, {ref, Ref}, Subdir}) ->
{Vsn, Src, root} = src(Name, {git, Url, {ref, Ref}}),
{Vsn, Src, {subdir, Subdir}};
src(Name, Other) ->
rebar_api:abort("rebar3_nix: unsupported dependency type ~p for ~s~n", [Other, Name]),
undefined.
Expand Down
8 changes: 5 additions & 3 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NIX_PATH := nixpkgs=https://github.com/NixOS/nixpkgs/archive/56d11e1467123fb1cd6212418aa2703520415524.tar.gz
NIX_PATH := nixpkgs=https://github.com/NixOS/nixpkgs/archive/72841a4a8761d1aed92ef6169a636872c986c76d.tar.gz

all: clean
nix-shell --run 'make test'
Expand All @@ -9,7 +9,9 @@ PROJECTS := test_app test_lib test_release test_escript test_profiles
DEPS := \
{cowboy, "2.8.0"}, \
{hackney, {git, "https://github.com/benoitc/hackney.git", {ref, "01bb4249f595e0aa692db28ef23e70f57ae2d4ed"}}}, \
{zj, {git, "https://gitlab.com/zxq9/zj.git", {ref, "27b7252f82c726d78f7ea297644b6b8ad90df479"}}}
{zj, {git, "https://gitlab.com/zxq9/zj.git", {ref, "27b7252f82c726d78f7ea297644b6b8ad90df479"}}}, \
{opentelemetry_api, {git_subdir, "https://github.com/open-telemetry/opentelemetry-erlang.git", \
{ref, "6f1d63673ccd628d057f2cc3f4909ac63279cc10"}, "apps/opentelemetry_api"}}

APP_DEPS := cowboy

Expand All @@ -20,7 +22,7 @@ test: $(PROJECTS)

test_profiles: test_lib
cp -R test_lib test_profiles
printf "\n{profiles, [{test, [{deps, [meck]}]}]}.\n" >> test_profiles/rebar.config
printf "\n{profiles, [{test, [{deps, [{meck, \"1.0.0\"}]}]}]}.\n" >> test_profiles/rebar.config
cd test_profiles && rebar3 as test nix lock
nix-build test_profiles
nix-shell ./test_profiles --run \
Expand Down