Skip to content

Commit

Permalink
tasks: convert some of the enterShell into tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Sep 3, 2024
1 parent 960db9d commit 37d3f1f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 26 deletions.
6 changes: 0 additions & 6 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ EOF
'';
};


tasks."devenv:sleep3".exec = "sleep 3";
tasks."devenv:sleep".exec = "sleep 5";
tasks."devenv:sleep".depends = [ "devenv:sleep3" ];
tasks."devenv:enterShell".depends = [ "devenv:sleep" ];

pre-commit.hooks = {
nixpkgs-fmt.enable = true;
#shellcheck.enable = true;
Expand Down
6 changes: 6 additions & 0 deletions devenv/init/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
git --version
'';

# https://devenv.sh/tasks/
# tasks = {
# "myproj:setup".exec = "mytool build";
# "devenv:enterShell".depends = ["myproj:setup"];
# };

# https://devenv.sh/tests/
enterTest = ''
echo "Running tests"
Expand Down
81 changes: 67 additions & 14 deletions docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,32 @@ Tasks allow you to form dependencies between commands, executed in parallel.
{
tasks."myapp:hello" = {
exec = ''echo "Hello, world!"'';
desc = "hello world in bash";
};
}
```

```shell-session
$ devenv tasks run hello
• Building shell ...
• Entering shell ...
Hello, world!
Hello, world
$
```

## enterShell / enterTest

If you'd like the tasks to run as part of the `enterShell` or `enterTest`:

```nix title="devenv.nix"
{ pkgs, lib, config, ... }:
{
tasks = {
"bash:hello".exec = "echo 'Hello world from bash!'";
"devenv:enterShell".depends = [ "bash:hello" ];
"devenv:enterTest".depends = [ "bash:hello" ];
};
}
```

## Using your favourite language

Tasks can also reference scripts and depend on other tasks, for example when entering the shell:
Expand All @@ -31,25 +44,65 @@ Tasks can also reference scripts and depend on other tasks, for example when ent
{
tasks = {
"python:hello"" = {
exec = ''print("Hello world from Python!")'';
exec = ''
print("Hello world from Python!")
'';
package = config.languages.python.package;
};
"bash:hello" = {
exec = "echo 'Hello world from bash!'";
depends = [ "python:hello" ];
};
"devenv:enterShell".depends = [ "bash:hello" ];
};
}
```

```shell-session
$ devenv shell
• Building shell ...
• Entering shell ...
...
$
```


`status`
## Avoiding running expensive `exec` via `status` check

If you define a `status` command, it will be executed first and if it returns `0`, `exec` will be skipped.

```nix title="devenv.nix"
{ pkgs, lib, config, ... }:
{
tasks = {
"myapp:migrations" = {
exec = "db-migrate";
status = "db-needs-migrations";
};
};
}
```

## Inputs / Outputs

Tasks support passing inputs and produce outputs, both as JSON objects:

- `$DEVENV_TASK_INPUTS`: JSON object serializing `tasks."myapp:mytask".inputs`.
- `$DEVENV_TASK_OUTPUTS`: a writable file with tasks' outputs in JSON.
- `$DEVENV_TASKS_OUTPUTS`: JSON object with dependent tasks as keys and their outputs as values.

```nix title="devenv.nix"
{ pkgs, lib, config, ... }:
{
tasks = {
"myapp:mytask" = {
exec = ''
echo $DEVENV_TASK_INPUTS > $DEVENV_ROOT/inputs.json
echo '{ "output" = 1; }' > $DEVENV_TASK_OUTPUTS
echo $DEVENV_TASKS_OUTPUTS > $DEVENV_ROOT/outputs.json
'';
inputs = {
value = 1;
};
};
};
}
```

## SDK

See [xxx](xxx) for a proposal how defining tasks in your favorite language would look like.
11 changes: 7 additions & 4 deletions src/modules/integrations/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

config = lib.mkIf ((lib.filterAttrs (id: value: value.enable) config.pre-commit.hooks) != { }) {
ci = [ config.pre-commit.run ];
enterTest = ''
pre-commit run -a
'';
# Add the packages for any enabled hooks at the end to avoid overriding the language-defined packages.
packages = lib.mkAfter ([ config.pre-commit.package ] ++ (config.pre-commit.enabledPackages or [ ]));
enterShell = config.pre-commit.installationScript;
tasks = {
# TODO: split installation script into status + exec
"devenv:pre-commit:install".exec = config.pre-commit.installationScript;
"devenv:pre-commit:run".exec = "pre-commit run -a";
"devenv:enterShell".depends = [ "devenv:pre-commit:install" ];
"devenv:enterTest".depends = [ "devenv:pre-commit:run" ];
};
};
}
4 changes: 2 additions & 2 deletions src/modules/tasks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ in
description = "Runs when entering the test environment";
};
};
#enterShell = "devenv tasks run devenv:enterShell";
#enterTest = "devenv tasks run devenv:enterTest";
enterShell = "devenv tasks run devenv:enterShell";
enterTest = "devenv tasks run devenv:enterTest";
};
}

0 comments on commit 37d3f1f

Please sign in to comment.