Skip to content

Commit

Permalink
feat: simplify proviso
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Jun 28, 2023
1 parent e0cde04 commit e4b9ae7
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/std.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
) &&git add -f flake.lock)
# continue normally ...
- name: Standard Discovery
uses: divnix/std-action/discover@v0.23.0
uses: divnix/std-action/discover@main
id: discovery

build_cli:
Expand Down
6 changes: 3 additions & 3 deletions src/local/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/std/fwlib/actions/build-filter.jq

This file was deleted.

72 changes: 42 additions & 30 deletions src/std/fwlib/actions/build-proviso.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
function getUncachedDrvs {
local -a uncached
local drv
drv=$1
declare action="$1"
declare targetDrv

mapfile -t uncached < <(
command nix-store --realise --dry-run "$drv" 2>&1 1>/dev/null \
| command sed -nrf @extractor@
)
eval "$(jq -r '@sh "targetDrv=\(.targetDrv)"' <<< "$action" )"

test ''${#uncached[@]} -eq 0 && return;
mapfile -t uncached < <(
command nix-store --realise --dry-run "$targetDrv" 2>&1 1>/dev/null \
| command sed -nr '
# If the line "will be built" is matched ...
/will be built/ {
# Create a label to iterate over dervivations
:b
# Get next line from input into pattern buffer
# (Overwrite the pattern buffer)
n
# If the line matches a nix store path ...
/\/nix/ {
# ... strip leading whitespaces and print it
s/\s*(.*)/\1/p
# and go on with the next line (step back to b)
bb
}
}
'
)

if (
command nix show-derivation ''${uncached[@]} 2> /dev/null \
| command jq --exit-status \
' with_entries(
select(.value|.env.preferLocalBuild != "1")
) | any
' 1> /dev/null
); then
echo "$drv"
fi
}
if [[ ${#uncached[@]} -eq 0 ]];
then
exit 1
fi

export -f getUncachedDrvs
if ! (
command nix show-derivation ''${uncached[@]} 2> /dev/null \
| command jq --exit-status \
' with_entries(
select(.value|.env.preferLocalBuild != "1")
) | any
' 1> /dev/null
); then
exit 1
fi

command jq --raw-output \
--from-file @filter@ \
--arg uncachedDrvs "$(
parallel -j0 getUncachedDrvs ::: "$(
command jq --raw-output 'map(.targetDrv|strings)[]' <<< "$1"
)"
)" <<< "$1"

unset -f getUncachedDrvs
exit 0
20 changes: 0 additions & 20 deletions src/std/fwlib/actions/build-uncached-extractor.sed

This file was deleted.

10 changes: 1 addition & 9 deletions src/std/fwlib/actions/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ in
currentSystem: target: let
pkgs = nixpkgs.${currentSystem};

provisoDrv = pkgs.substituteAll {
src = ./build-proviso.sh;
filter = ./build-filter.jq;
extractor = ./build-uncached-extractor.sed;
};
proviso =
# toFile ensures it get's build
toFile provisoDrv.name
(readFile (toString provisoDrv));
proviso = ./build-proviso.sh;

args = {
targetDrv = target.drvPath;
Expand Down
27 changes: 10 additions & 17 deletions src/std/fwlib/blockTypes/containers-proviso.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
function scopeo_inspect() {
local image
image="$1"
if command skopeo inspect --insecure-policy "docker://$image" &>/dev/null; then
echo "$image"
fi
}
export -f scopeo_inspect
command jq --raw-output \
--from-file "@filter@" \
--arg available "$(
parallel -j0 scopeo_inspect ::: "$(
command jq --raw-output 'map(.meta.image|strings)[]' <<< "$1"
)"
)" <<< "$1"
unset -f scopeo_inspect
unset images
declare action="$1"
declare image

eval "$(jq -r '@sh "image=\(.meta.image)"' <<< "$action" )"

if command skopeo inspect --insecure-policy "docker://$image" &>/dev/null; then
echo 1
fi

echo 0
5 changes: 0 additions & 5 deletions src/std/fwlib/blockTypes/containers-publish-filter.jq

This file was deleted.

10 changes: 1 addition & 9 deletions src/std/fwlib/blockTypes/containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ in
}: let
inherit (inputs.n2c.packages.${currentSystem}) skopeo-nix2container;
triv = trivial.${currentSystem};

provisoDrv = triv.substituteAll {
src = ./containers-proviso.sh;
filter = ./containers-publish-filter.jq;
};
proviso =
# toFile ensures it get's build
toFile provisoDrv.name
(readFile (toString provisoDrv));
proviso = ./containers-proviso.sh;

tags' =
builtins.toFile "${target.name}-tags.json" (builtins.concatStringsSep "\n" target.image.tags);
Expand Down
6 changes: 3 additions & 3 deletions src/tests/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/_snapshots/bt-blocktypes
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
command = <derivation build>;
description = "build it";
name = "build";
proviso = "/nix/store/wr33aaj6cq4w3apibizxfm76gczdsfhq-build-proviso.sh";
proviso = /nix/store/f10i6z9hwdr2adz7draay6v2jqv66swj-incl/std/fwlib/actions/build-proviso.sh;
targetDrv = "drvPath";
}
{
Expand All @@ -61,7 +61,7 @@
image = "repo:tag";
};
name = "publish";
proviso = "/nix/store/l4x9w3p106lrzh5ksk4k3zi3jdarf6zh-containers-proviso.sh";
proviso = /nix/store/f10i6z9hwdr2adz7draay6v2jqv66swj-incl/std/fwlib/blockTypes/containers-proviso.sh;
}
{
command = <derivation load>;
Expand Down Expand Up @@ -94,7 +94,7 @@
command = <derivation build>;
description = "build it";
name = "build";
proviso = "/nix/store/wr33aaj6cq4w3apibizxfm76gczdsfhq-build-proviso.sh";
proviso = /nix/store/f10i6z9hwdr2adz7draay6v2jqv66swj-incl/std/fwlib/actions/build-proviso.sh;
targetDrv = "drvPath";
}
{
Expand Down Expand Up @@ -127,7 +127,7 @@
command = <derivation build>;
description = "build it";
name = "build";
proviso = "/nix/store/wr33aaj6cq4w3apibizxfm76gczdsfhq-build-proviso.sh";
proviso = /nix/store/f10i6z9hwdr2adz7draay6v2jqv66swj-incl/std/fwlib/actions/build-proviso.sh;
targetDrv = "drvPath";
}
{
Expand Down Expand Up @@ -280,7 +280,7 @@
command = <derivation build>;
description = "build it";
name = "build";
proviso = "/nix/store/wr33aaj6cq4w3apibizxfm76gczdsfhq-build-proviso.sh";
proviso = /nix/store/f10i6z9hwdr2adz7draay6v2jqv66swj-incl/std/fwlib/actions/build-proviso.sh;
targetDrv = "drvPath";
}
{
Expand Down

0 comments on commit e4b9ae7

Please sign in to comment.