1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#! /bin/sh
#!buildShellBin prepend-path=cabal2nix:coreutils:crate2nix:findutils:gnugrep:jq:nix-prefetch-git:util-linux
# usage: plans [--update] [NAME...]
#
# The stored plans hrm builds Rust and Haskell projects from, instead of
# making them during evaluation:
# ~/sync/src/NAME/Cargo.json a Rust checkout's (crate2nix)
# ~/sync/src/NAME/cabal.nix a Haskell checkout's (cabal2nix)
# ~/sync/src/hrm/pkgs/rust/NAME/Cargo.json
# one kept in hrm, for where there's
# no checkout, like the hosts, unused
# if the package's source has a
# Cargo.json of its own
# Without --update, says for each, or each of the named projects, whether
# its plan is current, stale or missing, as hrm's evaluation sees it, and
# whether a plan kept in hrm is unused. With --update, makes them all again,
# unused ones too, as they may be used on a builder.
set -efu
src=$HOME/sync/src
hrm=$src/hrm
update=false
names=
for arg; do
case $arg in
--update) update=true ;;
-*) echo "plans: unknown option: $arg" >&2; exit 1 ;;
*) names="$names $arg" ;;
esac
done
set -- $names
# NAME KIND PLAN for each plan there is, or could be. KIND is rust or
# haskell for a checkout, hrm for a plan kept in hrm.
plans() {
find "$src" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort |
while read -r name; do
if test -e "$src/$name/Cargo.toml"; then
echo "$name rust $src/$name/Cargo.json"
elif test -e "$src/$name/$name.cabal"; then
echo "$name haskell $src/$name/cabal.nix"
fi
done
find "$hrm/pkgs/rust" -mindepth 2 -maxdepth 2 -name Cargo.json -printf '%h\n' | sort |
while read -r dir; do
echo "$(basename "$dir") hrm $dir/Cargo.json"
done
}
# The plans to work on: those of the named projects, or all.
all=$(plans)
if test $# = 0; then
selected=$all
else
selected=$(
for name; do
if ! printf '%s\n' "$all" | grep -q "^$name "; then
echo "plans: $name: no plan, and no Rust or Haskell checkout" >&2
exit 1
fi
printf '%s\n' "$all" | grep "^$name "
done
)
fi
test -n "$selected" || exit 0
# Evaluates the Nix function $1 with hrm's package set as pkgs, and the
# selected plans as plans.
eval_plans() {
nix-instantiate --eval --strict --json --read-write-mode \
--arg hrm "$hrm" \
--argstr plans "$(
printf '%s\n' "$selected" |
jq -Rn '[inputs | split(" ") | { name: .[0], kind: .[1], plan: .[2] }]'
)" \
-E "
{ hrm, plans }: let
pkgs = (import hrm {}).pkgs;
in ($1) pkgs (builtins.fromJSON plans)
"
}
# For each selected plan, one line: NAME KIND PLAN STATE SOURCE FLAGS...
# STATE is current if hrm's evaluation accepts the plan, stale if not,
# missing if there is none; a plan kept in hrm whose package's source has
# a plan of its own is unused/current or unused/stale. SOURCE and FLAGS
# are what to make a Rust plan from: the source, and crate2nix's feature
# flags. All in one evaluation.
report() {
eval_plans '
pkgs: map ({ name, kind, plan }: let
accepted = x: (builtins.tryEval (builtins.seq x true)).success;
lib = pkgs.lib;
unused = kind == "hrm" && builtins.pathExists "${args.src}/Cargo.json";
args = {
rust = pkgs.rustCrateArgs.${name};
haskell = { src = ""; };
hrm = pkgs.rustHrmCrateArgs.${name};
}.${kind};
check = {
rust = (pkgs.crate2nixPackage args).workspaceMembers;
haskell = pkgs.haskellPackages.${name};
# This plan, even where the plan of the source wins over it.
hrm = (pkgs.crate2nixPackage (args // { resolvedJson = plan; })).workspaceMembers;
};
in {
inherit name kind plan;
# To make a plan from: the source itself, not its copy in the store,
# which can leave Cargo.lock out.
src = if args ? cargoLock then dirOf (toString args.cargoLock) else "${args.src}";
features = args.features or [ "default" ];
state =
if !builtins.pathExists plan then "missing"
else (lib.optionalString unused "unused/")
+ (if accepted check.${kind} then "current" else "stale");
})
' |
jq -r '
.[] | [
.name, .kind, .plan, .state, (if .src == "" then "-" else .src end),
(if .features | index("default") then "--default-features" else "--no-default-features" end),
(.features[] | select(. != "default") | "--features", .)
] | join(" ")
'
}
# Makes the plan PLAN of the project NAME, of kind KIND, from SOURCE with
# FLAGS.
make_plan() {
name=$1 kind=$2 plan=$3 source=$4
shift 4
case $kind in
rust)
# In the checkout, as crate2nix writes paths relative to its output.
(cd "$src/$name" && crate2nix generate --format json -o .Cargo.json.tmp "$@")
mv "$src/$name/.Cargo.json.tmp" "$plan"
;;
hrm)
# In a writable copy of the package's source, for the same reason.
work=$(mktemp -d --tmpdir plans.XXXXXXXX)
cp -R "$source"/. "$work"
chmod -R u+w "$work"
(cd "$work" && crate2nix generate --format json -o Cargo.json "$@")
mv "$work/Cargo.json" "$plan"
rm -R "$work"
;;
haskell)
{
printf '# cabal-sha256: %s\n' "$(sha256sum "$src/$name/$name.cabal" | cut -d ' ' -f 1)"
(cd "$src/$name" && cabal2nix .)
} > "$plan.tmp"
mv "$plan.tmp" "$plan"
;;
esac
}
if $update; then
report |
while read -r name kind plan state source flags; do
echo "plans: making $plan" >&2
make_plan "$name" "$kind" "$plan" "$source" $flags
done
fi
report | cut -d ' ' -f 1-4 | sed "s|$src/||" | column -t
|