Nix flakes on the AT protocol
3.2 kB
105 lines
1#!/usr/bin/env bash
2#
3# This file will publish and build flakes that are on atproto
4
5set -euo pipefail
6
7check_command() {
8 if ! command -v $1 >/dev/null 2>&1; then
9 echo "Missing command $1"
10 echo "Install it and try this again or alternatively, try building this through the flake."
11 exit 1
12 fi
13}
14
15check_command goat
16check_command git
17check_command jq
18
19check_login() {
20 if ! goat account check-auth >/dev/null 2>&1; then
21 echo "Please make sure you have logged in before-hand. Try:"
22 echo ""
23 printf "\t%s\n" "goat account login -u your_username_here -p your_app_password_here"
24 echo ""
25 echo "If you don't want to supply your password on the command-line, use a CLI password manager like \`pass\`:"
26 echo ""
27 printf "\t%s\n" "pass insert atproto/nixat"
28 printf "\t%s\n" "goat account login -u your_username_here -p \"\$(pass show atproto/nixat)\""
29 exit 1
30 fi
31}
32
33build() {
34 local aturi="$1"
35 shift
36 echo "building flake pointed to by \`${aturi}\`:"
37 check_login
38 record="$(goat get "$aturi")"
39 source="$(jq -r '.source' <<<"$record")"
40 rev="$(jq -r '.rev' <<<"$record")"
41 narHash="$(jq -r '.narHash' <<<"$record")"
42
43 defaultFileContent="$(printf 'let src = builtins.fetchGit { url = "%s"; rev = "%s"; }; in assert src.narHash == "%s"; src.outPath' "$source" "$rev" "$narHash")"
44 srcPath="$(nix eval --expr "$defaultFileContent" | sed -r 's/\"([^\"]+)\"/\1/')"
45 nix build "$srcPath"
46}
47
48publish() {
49 local path="$(realpath "$1")" version="$2"
50 shift
51 shift
52 echo "publishing flake at ${path} with version ${version}"
53
54 local rev source narHash publishedAt record
55 rev="$(git -C "$path" rev-parse HEAD)"
56 source="$(git -C "$path" remote get-url origin)"
57 narHash="$(nix flake metadata --json "git+file://${path}?rev=${rev}" | jq -r .locked.narHash)"
58 publishedAt="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
59
60 record="$(printf '{"$type":"space.stau.nix.release","rev":"%s","source":"%s","narHash":"%s","publishedAt":"%s"}' "$rev" "$source" "$narHash" "$publishedAt")"
61 jq -n "$record" | goat record create --rkey "$version" --no-validate -
62}
63
64usage() {
65 echo "Usage: $0 <subcmd>"
66 echo ""
67 echo "The subcommands are:"
68 echo ""
69 printf "\t%s\n" "build <aturi> build a flake pointed at by <aturi>"
70 printf "\t%s\n" "publish <path> <version> publish the flake at <path> with version <version>"
71 printf "\t%s\n" "help | -h | --help | ? show this help"
72}
73
74if [ $# -gt 0 ]; then
75 case $1 in
76 "build")
77 if [ $# -gt 1 ]; then
78 build "$2"
79 else
80 echo "expected an AT URI as an argument"
81 usage
82 exit 1
83 fi
84 ;;
85 "publish")
86 if [ $# -gt 2 ]; then
87 publish "$2" "$3"
88 else
89 echo "expected a path and a version name as arguments"
90 usage
91 exit 1
92 fi
93 ;;
94 "help"|"-h"|"--help"|"?")
95 usage
96 ;;
97 *)
98 usage
99 exit 1
100 ;;
101 esac
102else
103 usage
104 exit 1
105fi