#! /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