This commit is contained in:
Lukas Wurzinger 2025-07-26 15:39:07 +02:00
parent 996bcd0fa1
commit f022074dd6
No known key found for this signature in database
4 changed files with 63 additions and 19 deletions

View file

@ -18,3 +18,23 @@ Example `flendor.json`:
}
}
```
Example usage:
```
$ ./flendor
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/forgesync.git to vendor/forgesync
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/hxwrap.git to vendor/hxwrap
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/musicomp.git to vendor/musicomp
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/mympv.git to vendor/mympv
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/myphps.git to vendor/myphps
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/nini.git to vendor/nini
./flendor: copying flake git+https://forgejo.helveticanonstandard.net/helvetica/xenumenu.git to vendor/xenumenu
./flendor: removing old flake at vendor/asdf
./flendor: removing old flake at vendor/hjkl
```
## Why?
I don't know.
I personally just use this to vendor my own flakes in my NixOS configuration, so that if my Forgejo server ever stops working, I still have the possibility to rebuild its configuration without much hassle.

View file

@ -40,10 +40,14 @@
treefmt = {
projectRootFile = "flake.nix";
programs.nixfmt = {
programs = {
nixfmt = {
enable = true;
package = pkgs.nixfmt-rfc-style;
};
shfmt.enable = true;
};
};
pre-commit.settings.hooks = {

48
flendor
View file

@ -4,6 +4,8 @@ set -o errexit
set -o nounset
set -o pipefail
shopt -s nullglob
progname=$0
warn() {
@ -21,7 +23,7 @@ error() {
args=$(
getopt \
--options t:rv \
--options d:rv \
--longoptions vendor:,refresh,verbose \
--name "$progname" \
-- "$@"
@ -31,10 +33,8 @@ eval set -- "$args"
nixflags=()
rmflags=()
mkdirflags=()
cpflags=()
rsyncflags=()
prefetchflags=()
verbose=false
while true; do
case $1 in
-d | --vendor)
@ -48,9 +48,7 @@ while true; do
-v | --verbose)
nixflags+=(--verbose)
rmflags+=(--verbose)
mkdirflags+=(--verbose)
cpflags+=(--verbose)
verbose=true
rsyncflags+=(--verbose)
shift
;;
--)
@ -70,21 +68,41 @@ if [[ ! -v vendor ]]; then
vendor=$(jq --exit-status --raw-output '.vendor' <<<"$json")
fi
keeppaths=()
while IFS= read -r k; do
name=$(jq --null-input --argjson k "$k" --raw-output '$k')
if [[ $name =~ / ]]; then
error 'flake input name cannot contain slashes'
fi
flake=$(jq --argjson k "$k" --raw-output '.flakes.[$k]' <<<"$json")
dest="$vendor/$name"
if [[ $verbose == true ]]; then
keeppaths+=("$dest")
warn "copying flake $flake to $dest"
fi
rm --recursive --force "${rmflags[@]}" -- "$dest"
src=$(nix "${nixflags[@]}" flake prefetch --json "${prefetchflags[@]}" -- "$flake" | jq --exit-status --raw-output '.storePath')
src=$(nix flake prefetch --json "${prefetchflags[@]}" -- "$flake" | jq --exit-status --raw-output '.storePath')
mkdir --parents "${mkdirflags[@]}" -- "$(dirname -- "$dest")"
cp --recursive --no-preserve all "${cpflags[@]}" -- "$src/." "$dest"
rsync --recursive --delete --update --mkpath "${rsyncflags[@]}" -- "$src/" "$dest"
done < <(jq '.flakes | keys[]' <<<"$json")
for path in "$vendor"/{,.}*; do
keep=0
for keeppath in "${keeppaths[@]}"; do
a=$(realpath --strip -- "$path")
b=$(realpath --strip -- "$keeppath")
if [[ $a == "$b" ]]; then
keep=1
fi
done
if (( ! keep )); then
warn "removing old flake at $path"
rm --recursive --force "${rmflags[@]}" -- "$path"
fi
done

View file

@ -1,12 +1,14 @@
{
writeShellApplication,
jq,
rsync,
}:
writeShellApplication {
name = "jq";
name = "flendor";
runtimeInputs = [
jq
rsync
];
text = builtins.readFile ./flendor;