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
168
169
170
171
|
#! /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
# 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. With
# --update, makes them again.
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)
"
}
# NAME KIND PLAN STATE for each selected plan. Current means that hrm's
# evaluation accepts it.
states() {
eval_plans '
pkgs: map ({ name, kind, plan }: let
accepted = x: (builtins.tryEval (builtins.seq x true)).success;
check = {
rust = (pkgs.crate2nixPackage pkgs.rustCrateArgs.${name}).workspaceMembers;
haskell = pkgs.haskellPackages.${name};
hrm = (pkgs.crate2nixPackage pkgs.rustHrmCrateArgs.${name}).workspaceMembers;
};
in {
inherit name kind plan;
state =
if !builtins.pathExists plan then "missing"
else if accepted check.${kind} then "current"
else "stale";
})
' |
jq -r '.[] | "\(.name) \(.kind) \(.plan) \(.state)"'
}
# The source to make a Rust plan from, and crate2nix's feature flags for it,
# from hrm's arguments for the package: SOURCE FLAGS...
rust_args() {
nix-instantiate --eval --strict --json --read-write-mode \
--arg hrm "$hrm" --argstr name "$1" --argstr kind "$2" \
-E '
{ hrm, name, kind }: let
pkgs = (import hrm {}).pkgs;
args = {
rust = pkgs.rustCrateArgs.${name};
hrm = pkgs.rustHrmCrateArgs.${name};
}.${kind};
in {
src = "${args.src}";
features = args.features or [ "default" ];
}
' |
jq -r '
[
.src,
(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.
make_plan() {
name=$1 kind=$2 plan=$3
case $kind in
rust)
# In the checkout, as crate2nix writes paths relative to its output.
set -- $(rust_args "$name" "$kind")
shift
(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.
set -- $(rust_args "$name" "$kind")
source=$1
shift
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
printf '%s\n' "$selected" |
while read -r name kind plan; do
echo "plans: making $plan" >&2
make_plan "$name" "$kind" "$plan"
done
fi
states | sed "s|$src/||" | column -t
|