Skip to content

Commit f9129df

Browse files
committed
refactor: expose shared actions and unify their calling iface
1 parent a41983e commit f9129df

16 files changed

+127
-125
lines changed

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
};
3939
outputs = inputs: let
4040
blockTypes = import ./src/blocktypes.nix {inherit (inputs) nixpkgs;};
41+
sharedActions = import ./src/actions.nix {inherit (inputs) nixpkgs;};
4142
deSystemize = inputs.nosys.lib.deSys;
4243
grow = import ./src/grow.nix {
4344
inherit (inputs) nixpkgs yants;
@@ -58,7 +59,7 @@
5859
in
5960
{
6061
inherit (inputs) yants dmerge incl; # convenience re-exports
61-
inherit blockTypes;
62+
inherit blockTypes sharedActions;
6263
inherit (blockTypes) runnables installables functions data devshells containers files microvms nixago nomadJobManifests;
6364
inherit grow growOn deSystemize pick harvest winnow;
6465
systems = l.systems.doubles;

src/actions.nix

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{nixpkgs}: let
2+
l = nixpkgs.lib // builtins;
3+
mkCommand = import ./mkCommand.nix {inherit nixpkgs;};
4+
5+
contextFreeDrv = target: l.unsafeDiscardStringContext target.drvPath;
6+
7+
build = system: target:
8+
mkCommand system {
9+
name = "build";
10+
description = "build it";
11+
command = ''
12+
# ${target}
13+
nix build ${contextFreeDrv target}
14+
'';
15+
targetDrv = target.drvPath;
16+
proviso =
17+
# bash
18+
''
19+
function proviso() {
20+
local -n input=$1
21+
local -n output=$2
22+
23+
local drvs
24+
local -a uncached
25+
26+
# FIXME: merge upstream to avoid any need for runtime context
27+
command nix build github:divnix/nix-uncached/v2.12.1
28+
29+
drvs="$(command jq -r '.targetDrv | select(. != "null")' <<< "''${input[@]}")"
30+
31+
mapfile -t uncached < <(command nix show-derivation $drvs | jq -r '.[].outputs.out.path' | result/bin/nix-uncached)
32+
33+
if [[ -n ''${uncached[*]} ]]; then
34+
mapfile -t uncached < <(command nix show-derivation ''${uncached[@]} \
35+
| command jq -r '.| to_entries[] | select(.value|.env.preferLocalBuild != "1") | .key')
36+
fi
37+
38+
if [[ -n ''${uncached[*]} ]]; then
39+
local list filtered
40+
41+
list=$(command jq -ncR '[inputs]' <<< "''${uncached[@]}")
42+
filtered=$(command jq -c 'select([.targetDrv] | inside($p))' --argjson p "$list" <<< "''${input[@]}")
43+
44+
output=$(command jq -cs '. += $p' --argjson p "$output" <<< "$filtered")
45+
fi
46+
}
47+
'';
48+
};
49+
50+
run = system: target: let
51+
programName =
52+
target.meta.mainProgram
53+
or (l.getName target);
54+
in
55+
mkCommand system {
56+
name = "run";
57+
description = "run it";
58+
# this is the exact sequence mentioned by the `nix run` docs
59+
# and so should be compatible
60+
command = ''
61+
${target.program or "${target}/bin/${programName}"} "$@"
62+
'';
63+
};
64+
in {inherit build run;}

src/blocktypes.nix

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
{nixpkgs}: let
2-
mkCommand = system: type: args:
3-
args
4-
// {
5-
command = (nixpkgs.legacyPackages.${system}.writeShellScript "${type}-${args.name}" args.command).overrideAttrs (self: {
6-
passthru =
7-
self.passthru
8-
or {}
9-
// nixpkgs.lib.optionalAttrs (args ? proviso) {
10-
proviso = builtins.toFile "${args.name}-proviso" args.proviso;
11-
}
12-
// nixpkgs.lib.optionalAttrs (args ? targetDrv) {
13-
inherit (args) targetDrv;
14-
};
15-
});
16-
};
2+
sharedActions = import ./actions.nix {inherit nixpkgs;};
3+
mkCommand = import ./mkCommand.nix {inherit nixpkgs;};
174
in {
18-
runnables = import ./blocktypes/runnables.nix {inherit nixpkgs mkCommand;};
19-
installables = import ./blocktypes/installables.nix {inherit nixpkgs mkCommand;};
5+
runnables = import ./blocktypes/runnables.nix {inherit nixpkgs mkCommand sharedActions;};
6+
installables = import ./blocktypes/installables.nix {inherit nixpkgs mkCommand sharedActions;};
207
functions = import ./blocktypes/functions.nix {inherit nixpkgs mkCommand;};
218
data = import ./blocktypes/data.nix {inherit nixpkgs mkCommand;};
22-
devshells = import ./blocktypes/devshells.nix {inherit nixpkgs mkCommand;};
23-
containers = import ./blocktypes/containers.nix {inherit nixpkgs mkCommand;};
9+
devshells = import ./blocktypes/devshells.nix {inherit nixpkgs mkCommand sharedActions;};
10+
containers = import ./blocktypes/containers.nix {inherit nixpkgs mkCommand sharedActions;};
2411
files = import ./blocktypes/files.nix {inherit nixpkgs mkCommand;};
2512
microvms = import ./blocktypes/microvms.nix {inherit nixpkgs mkCommand;};
2613
nixago = import ./blocktypes/nixago.nix {inherit nixpkgs mkCommand;};

src/blocktypes/actions/build.nix

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/blocktypes/actions/run.nix

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/blocktypes/arion.nix

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,42 @@
2525
}: let
2626
cmd = "arion --prebuilt-file ${target.config.out.dockerComposeYaml}";
2727
in [
28-
(mkCommand system "arion" {
28+
(mkCommand system {
2929
name = "up";
3030
description = "arion up";
3131
command = ''
3232
${cmd} up "$@"
3333
'';
3434
})
35-
(mkCommand system "arion" {
35+
(mkCommand system {
3636
name = "ps";
3737
description = "exec this arion task to ps";
3838
command = ''
3939
${cmd} ps "$@"
4040
'';
4141
})
42-
(mkCommand system "arion" {
42+
(mkCommand system {
4343
name = "stop";
4444
description = "arion stop";
4545
command = ''
4646
${cmd} stop "$@"
4747
'';
4848
})
49-
(mkCommand system "arion" {
49+
(mkCommand system {
5050
name = "rm";
5151
description = "arion rm";
5252
command = ''
5353
${cmd} rm "$@"
5454
'';
5555
})
56-
(mkCommand system "arion" {
56+
(mkCommand system {
5757
name = "config";
5858
description = "check the docker-compose yaml file";
5959
command = ''
6060
${cmd} config "$@"
6161
'';
6262
})
63-
(mkCommand system "arion" {
63+
(mkCommand system {
6464
name = "arion";
6565
description = "pass any command to arion";
6666
command = ''

src/blocktypes/containers.nix

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
nixpkgs,
33
mkCommand,
4+
sharedActions,
45
}: let
56
l = nixpkgs.lib // builtins;
67
/*
@@ -22,16 +23,16 @@
2223
fragmentRelPath,
2324
target,
2425
}: [
25-
(import ./actions/build.nix target (mkCommand system "containers"))
26-
(mkCommand system "containers" {
26+
(sharedActions.build system target)
27+
(mkCommand system {
2728
name = "print-image";
2829
description = "print out the image name & tag";
2930
command = ''
3031
echo
3132
echo "${target.imageName}:${target.imageTag}"
3233
'';
3334
})
34-
(mkCommand system "containers" {
35+
(mkCommand system {
3536
name = "publish";
3637
description = "copy the image to its remote registry";
3738
command = let
@@ -86,21 +87,21 @@
8687
}
8788
'';
8889
})
89-
(mkCommand system "containers" {
90+
(mkCommand system {
9091
name = "copy-to-registry";
9192
description = "copy the image to its remote registry";
9293
command = ''
9394
${target.copyToRegistry}/bin/copy-to-registry
9495
'';
9596
})
96-
(mkCommand system "containers" {
97+
(mkCommand system {
9798
name = "copy-to-docker";
9899
description = "copy the image to the local docker registry";
99100
command = ''
100101
${target.copyToDockerDaemon}/bin/copy-to-docker-daemon
101102
'';
102103
})
103-
(mkCommand system "containers" {
104+
(mkCommand system {
104105
name = "copy-to-podman";
105106
description = "copy the image to the local podman registry";
106107
command = ''

src/blocktypes/data.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
jq = ["${pkgs.jq}/bin/jq" "-r" "'.'" "${json}"];
3333
fx = ["|" "xargs" "cat" "|" "${pkgs.fx}/bin/fx"];
3434
in [
35-
(mkCommand system "data" {
35+
(mkCommand system {
3636
name = "write";
3737
description = "write to file";
3838
command = "echo ${json}";
3939
})
40-
(mkCommand system "data" {
40+
(mkCommand system {
4141
name = "explore";
4242
description = "interactively explore";
4343
command = l.concatStringsSep "\t" (jq ++ fx);

src/blocktypes/devshells.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
nixpkgs,
33
mkCommand,
4+
sharedActions,
45
}: let
56
l = nixpkgs.lib // builtins;
67
mkDevelopDrv = import ../devshell-drv.nix;
@@ -23,8 +24,8 @@
2324
}: let
2425
developDrv = mkDevelopDrv target;
2526
in [
26-
(import ./actions/build.nix developDrv (mkCommand system "devshells"))
27-
(mkCommand system "devshells" {
27+
(sharedActions.build system target)
28+
(mkCommand system {
2829
name = "enter";
2930
description = "enter this devshell";
3031
command = ''

src/blocktypes/files.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
file = toString target;
2222
bat = "${nixpkgs.legacyPackages.${system}.bat}/bin/bat";
2323
in [
24-
(mkCommand system "files" {
24+
(mkCommand system {
2525
name = "explore";
2626
description = "interactively explore with bat";
2727
command = ''

0 commit comments

Comments
 (0)