Skip to content

Commit a1efc40

Browse files
committed
nix: add dart2nix
We can use this to translate pubspec.lock files into deps.nix dependency lists. This initial version is fragile, but works well enough to be helpful.
1 parent 5cb81dd commit a1efc40

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

nix/dart2nix.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
pubspec2nix() {
18+
local pubspec_in="$1";
19+
local nix_out="$2";
20+
if [ -z "$pubspec_in" ] || [ -z "$nix_out" ]; then
21+
echo "usage: dart2nix <pubspec.lock> <packages.nix>" >&2
22+
exit 1
23+
fi
24+
local url_template='https://storage.googleapis.com/pub-packages/packages/{{description.name}}-{{version}}.tar.gz'
25+
local grep_template=' { name = .{{lock.description.name}}.; version = .{{lock.version}}.; sha256 = '
26+
local pkg_template=' { name = "{{lock.description.name}}"; version = "{{lock.version}}"; sha256 = "{{sha256}}"; }'
27+
local PATH="$PATH:/nix/store/s8jfiwj2p6hn3ycj840naf2jkac4azx6-jq-1.6-bin/bin" # 'yq' doesn't work unless jq is in $PATH
28+
local yq="/nix/store/0l1wva10sr2yyqj78pf4nbqkq1al61qr-python3.8-yq-2.11.1/bin/yq"
29+
local mustache="/nix/store/q1lllkgf81rzpsf9lyhw1660q55zn3l7-mustache-go-1.2.0/bin/mustache"
30+
[ -f "$nix_out" ] || echo '[' > "$nix_out"
31+
$yq -c '.packages[]' "$pubspec_in" | while read dep ; do
32+
match="$( echo '{"lock":'$dep'}' | $mustache <(echo "$grep_template") )"
33+
if grep "$match" "$nix_out" ; then
34+
#echo "already got this one, moving on: $dep"
35+
#elif true; then
36+
#echo "would download this one: $match"
37+
true
38+
else
39+
url="$(
40+
echo "$dep" \
41+
| $mustache <(echo "$url_template")
42+
)"
43+
sha256="$(
44+
/nix/store/ckl9rqyajn45crcyw3b743bcaqx1j2w1-nix-2.3.10/bin/nix-hash --flat --base32 --type sha256 <(curl -L "$url")
45+
)"
46+
echo '{"sha256":"'$sha256'","lock":'$dep'}' | $mustache <(echo "$pkg_template") >> "$nix_out"
47+
fi
48+
done
49+
echo ']' >> "$nix_out"
50+
}
51+
pubspec2nix "$@"

nix/default.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let
4949
};
5050

5151
gitignoreSource =
52-
(import sources."gitignore.nix" { inherit (pkgs) lib; }).gitignoreSource;
52+
(import sources.gitignore { inherit (pkgs) lib; }).gitignoreSource;
5353
src = gitignoreSource ./..;
5454

5555
lib = pkgs.lib;
@@ -101,6 +101,8 @@ let
101101
"$@"
102102
'';
103103

104+
dart2nix = pkgs.writeShellScriptBin "dart2nix" (lib.strings.fileContents ./dart2nix.sh);
105+
104106
in rec
105107
{
106108
inherit pkgs src;
@@ -118,6 +120,7 @@ in rec
118120
# Additional tools useful for code work and repository maintenance.
119121
devTools = buildTools // {
120122
inherit (pkgs) niv;
123+
inherit dart2nix;
121124
};
122125

123126
buildInputs = builtins.attrValues ciTools;

0 commit comments

Comments
 (0)