-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dlesl/nix-lock
Add `rebar3 nix lock` command
- Loading branch information
Showing
9 changed files
with
103 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
{erl_opts, [debug_info]}. | ||
{deps, [ {hex2nix, {git, "/home/gleber/code/erl/hex2nix/"}} | ||
, {erlware_commons, "1.0.0"}]}. | ||
{deps, []}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1 @@ | ||
{"1.1.0", | ||
[{<<"cf">>,{pkg,<<"cf">>,<<"0.2.1">>},1}, | ||
{<<"erlware_commons">>,{pkg,<<"erlware_commons">>,<<"1.0.0">>},0}, | ||
{<<"getopt">>,{pkg,<<"getopt">>,<<"0.8.2">>},1}, | ||
{<<"ibrowse">>,{pkg,<<"ibrowse">>,<<"4.4.0">>},1}, | ||
{<<"jsx">>,{pkg,<<"jsx">>,<<"2.8.2">>},1}]}. | ||
[ | ||
{pkg_hash,[ | ||
{<<"cf">>, <<"69D0B1349FD4D7D4DC55B7F407D29D7A840BF9A1EF5AF529F1EBE0CE153FC2AB">>}, | ||
{<<"erlware_commons">>, <<"087467DE5833C0BB5B3CCDD387F9E9C1FB816A75B7A709629BF24B5ED3246C51">>}, | ||
{<<"getopt">>, <<"B17556DB683000BA50370B16C0619DF1337E7AF7ECBF7D64FBF8D1D6BCE3109B">>}, | ||
{<<"ibrowse">>, <<"2D923325EFE0D2CB09B9C6A047B2835A5EDA69D8A47ED6FF8BC03628B764E991">>}, | ||
{<<"jsx">>, <<"7ACC7D785B5ABE8A6E9ADBDE926A24E481F29956DD8B4DF49E3E4E7BCC92A018">>}]} | ||
]. | ||
[]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
-module(rebar3_nix_lock_prv). | ||
|
||
-export([init/1, do/1, format_error/1]). | ||
|
||
-define(NIX_DEPS, "# Generated by rebar3_nix | ||
{ fetchHex, fetchFromGitHub }: | ||
{~s | ||
} | ||
"). | ||
|
||
-define(FETCH_HEX, " | ||
~s = fetchHex { | ||
pkg = \"~s\"; | ||
version = \"~s\"; | ||
sha256 = \"~s\"; | ||
};"). | ||
|
||
-define(FETCH_GIT, " | ||
~s = fetchGit { | ||
url = \"~s\"; | ||
rev = \"~s\"; | ||
};"). | ||
|
||
-define(FETCH_FROM_GITHUB, " | ||
~s = fetchFromGitHub { | ||
owner = \"~s\"; | ||
repo = \"~s\"; | ||
rev = \"~s\"; | ||
sha256 = \"~s\"; | ||
};"). | ||
|
||
-spec init(rebar_state:t()) -> {ok, rebar_state:t()}. | ||
init(State) -> | ||
Provider = providers:create([ | ||
{namespace, nix}, | ||
{name, lock}, | ||
{module, ?MODULE}, | ||
{bare, true}, | ||
{deps, [{default, lock}]}, | ||
{example, "rebar3 nix lock -o rebar-deps.nix"}, | ||
{opts, [{out, $o, "out", {string, "rebar-deps.nix"}, "Output file."}]}, | ||
{short_desc, "Export rebar3 dependencies for nix"}, | ||
{desc, "Export rebar3 dependencies for nix"} | ||
]), | ||
{ok, rebar_state:add_provider(State, Provider)}. | ||
|
||
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. | ||
do(State) -> | ||
Lock = rebar_state:lock(State), | ||
Deps = [to_nix(rebar_app_info:name(AppInfo), rebar_app_info:source(AppInfo)) | ||
|| AppInfo <- Lock], | ||
Drv = io_lib:format(?NIX_DEPS, [Deps]), | ||
ok = file:write_file(out_path(State), Drv), | ||
{ok, State}. | ||
|
||
out_path(State) -> | ||
{Args, _} = rebar_state:command_parsed_args(State), | ||
proplists:get_value(out, Args). | ||
|
||
-spec format_error(any()) -> iolist(). | ||
format_error(Reason) -> | ||
io_lib:format("~p", [Reason]). | ||
|
||
to_nix(Name, {pkg, PkgName, Vsn, _OldHash, Hash, _Repo}) -> | ||
io_lib:format(?FETCH_HEX, [Name, PkgName, Vsn, to_sri(Hash)]); | ||
to_nix(Name, {git, Url, {ref, Ref}}) -> | ||
case string:prefix(string:lowercase(Url), "https://github.com/") of | ||
nomatch -> | ||
io_lib:format(?FETCH_GIT, [Name, Url, Ref]); | ||
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, " 2>/dev/null"], | ||
Hash = case string:trim(os:cmd(Prefetch)) of | ||
[] -> | ||
rebar_api:abort( | ||
"prefetch failed, make sure nix-prefetch-url is on your PATH", | ||
[]); | ||
Hash0 -> | ||
Hash0 | ||
end, | ||
io_lib:format(?FETCH_FROM_GITHUB, [Name, Owner, Repo, Ref, Hash]) | ||
end; | ||
to_nix(Name, Other) -> | ||
rebar_api:abort("rebar3_nix: unsupported dependency type ~p for ~s~n", [Other, Name]), | ||
undefined. | ||
|
||
to_sri(Sha256) when is_list(Sha256) -> | ||
to_sri(list_to_binary(Sha256)); | ||
to_sri(<<Sha256Base16:64/binary>>) -> | ||
Sha256 = binary_to_integer(Sha256Base16, 16), | ||
["sha256-", base64:encode(<<Sha256:32/big-unsigned-integer-unit:8>>)]. |
This file was deleted.
Oops, something went wrong.