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,9 +40,13 @@
treefmt = { treefmt = {
projectRootFile = "flake.nix"; projectRootFile = "flake.nix";
programs.nixfmt = { programs = {
enable = true; nixfmt = {
package = pkgs.nixfmt-rfc-style; enable = true;
package = pkgs.nixfmt-rfc-style;
};
shfmt.enable = true;
}; };
}; };

48
flendor
View file

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