aboutsummaryrefslogtreecommitdiff
path: root/scripts/refresh_flakes
blob: 2bbcad660eaf45756503afac38ffef0c795d2c9b (plain) (blame)
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
#!/usr/bin/env bash

set -e

declare -A refreshed

while [ -n "$1" ]; do
  case "$1" in
    --no-new-inputs)
      no_new_inputs="y"
      shift;;
    *)
      flake_or_dir="$1"
      shift;;
  esac
done

refresh_deps() {
  local flake
  local inputs=()
  local depname
  local deppath
  flake="$(realpath $1)"
  if [ "${refreshed[$flake]}" = 1 ]; then
    return
  fi
  pushd "$flake" 2>/dev/null >/dev/null
  if [ -z "$no_new_inputs" ]; then
    nix --no-warn-dirty flake lock
  fi
  if [ ! -e "$flake/flake.lock" ]; then
    popd 2>/dev/null >/dev/null
    refreshed[$flake]=1
    return
  fi

  deps=$(jq -r '. as $root | .nodes[.root].inputs|values|to_entries|map({ key: .key, value: $root.nodes[.value].original.path })[]|select(.value != null)|.key + " " + .value' < flake.lock)
  if [ -n "$deps" ]; then
    while read depname deppath; do
      refresh_deps "$deppath"
      inputs+=(--update-input "$depname")
    done <<<"$deps"
  fi
  nix --no-warn-dirty flake lock "${inputs[@]}"
  popd 2>/dev/null >/dev/null
  refreshed[$flake]=1
}

git_dir=$(git rev-parse --show-toplevel)

# If argument is given (flake.nix or directory containing), refresh that argument
# Otherwise, if we are in a subdirectory containing a flake.nix, refresh that
# Otherwise, refresh all
if [ -n "$flake_or_dir" ]; then
  if [ -d "$flake_or_dir" -a -e "$1/flake.nix" ]; then
    refresh_deps "$flake_or_dir"
  elif [ -f "$flake_or_dir" -a -e "$(dirname $flake_or_dir)/flake.nix" ]; then
    refresh_deps "$(dirname $flake_or_dir)"
  else
    echo "No flake.nix file in specified location"
    exit 1
  fi
else
  if [ "$(pwd)" != "$git_dir" -a -e "$(pwd)/flake.nix" ]; then
    refresh_deps "$(pwd)"
  else
    find $git_dir -name "flake.lock" | while read flake; do
      refresh_deps "$(dirname $flake)"
    done
  fi
fi