summaryrefslogtreecommitdiffstats
path: root/pkgs/shell/plans
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-02 15:53:48 +0000
committertv <tv@krebsco.de>2026-12-02 18:02:39 +0000
commit819523723a7fba80957e4bc9326d5a8f4926fa21 (patch)
tree40302ab28597f9192395461256af091c5a32fcdf /pkgs/shell/plans
parent20a6dbc80bea9fbf9b16f094c46cd571c055ee04 (diff)
plans: init
Says whether the stored plans hrm builds Rust and Haskell projects from are current, stale or missing, as hrm's evaluation sees them: the Cargo.json and cabal2nix.nix of the checkouts in ~/sync/src, and the Cargo.json kept in pkgs/rust/<pname>/. With --update, makes them again, for all projects or the named ones.
Diffstat (limited to 'pkgs/shell/plans')
-rwxr-xr-xpkgs/shell/plans171
1 files changed, 171 insertions, 0 deletions
diff --git a/pkgs/shell/plans b/pkgs/shell/plans
new file mode 100755
index 00000000..16e84187
--- /dev/null
+++ b/pkgs/shell/plans
@@ -0,0 +1,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