]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - scripts/refresh_flakes
Add monitoring script with smartctl
[perso/Immae/Config/Nix.git] / scripts / refresh_flakes
CommitLineData
1a64deeb
IB
1#!/usr/bin/env bash
2
3set -e
4
5declare -A refreshed
6
7while [ -n "$1" ]; do
8 case "$1" in
9 --no-new-inputs)
10 no_new_inputs="y"
11 shift;;
12 *)
13 flake_or_dir="$1"
14 shift;;
15 esac
16done
17
18refresh_deps() {
19 local flake
20 local inputs=()
21 local depname
22 local deppath
23 flake="$(realpath $1)"
24 if [ "${refreshed[$flake]}" = 1 ]; then
25 return
26 fi
27 pushd "$flake" 2>/dev/null >/dev/null
28 if [ -z "$no_new_inputs" ]; then
29 nix --no-warn-dirty flake lock
30 fi
31 if [ ! -e "$flake/flake.lock" ]; then
32 popd 2>/dev/null >/dev/null
33 refreshed[$flake]=1
34 return
35 fi
36
37 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)
38 if [ -n "$deps" ]; then
39 while read depname deppath; do
40 refresh_deps "$deppath"
41 inputs+=(--update-input "$depname")
42 done <<<"$deps"
43 fi
44 nix --no-warn-dirty flake lock "${inputs[@]}"
45 popd 2>/dev/null >/dev/null
46 refreshed[$flake]=1
47}
48
49git_dir=$(git rev-parse --show-toplevel)
50
51# If argument is given (flake.nix or directory containing), refresh that argument
52# Otherwise, if we are in a subdirectory containing a flake.nix, refresh that
53# Otherwise, refresh all
54if [ -n "$flake_or_dir" ]; then
55 if [ -d "$flake_or_dir" -a -e "$1/flake.nix" ]; then
56 refresh_deps "$flake_or_dir"
57 elif [ -f "$flake_or_dir" -a -e "$(dirname $flake_or_dir)/flake.nix" ]; then
58 refresh_deps "$(dirname $flake_or_dir)"
59 else
60 echo "No flake.nix file in specified location"
61 exit 1
62 fi
63else
64 if [ "$(pwd)" != "$git_dir" -a -e "$(pwd)/flake.nix" ]; then
65 refresh_deps "$(pwd)"
66 else
67 find $git_dir -name "flake.lock" | while read flake; do
68 refresh_deps "$(dirname $flake)"
69 done
70 fi
71fi