Skip to content

Commit

Permalink
blog: 1.2 tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Sep 24, 2024
1 parent 4880780 commit 1fd85e5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Binary file added docs/assets/images/tasks.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions docs/blog/posts/devenv-v1.2-tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
draft: false
date: 2024-09-24
authors:
- domenkozar
---

# devenv 1.2: Tasks for convergent configuration with Nix

It's often not possible to do configuration using Nix, which does [congruent configuration](https://constructolution.wordpress.com/2012/07/08/divergent-convergent-and-congruent-infrastructures/).
<br>

It's even more often practical to start with [convergent configuration](https://constructolution.wordpress.com/2012/07/08/divergent-convergent-and-congruent-infrastructures/)
and translate it later into congruent if/when needed.
<br>

## enterShell

For example, devenv provides [enterShell](/basics/) that corresponds to the shell code executed when you enter a developer environment.
<br>

In devenv itself, we have modules that set up things like pre-commit hooks or Python virtualenv as part of `enterShell`.
<br>

## Tasks

We're introducing [tasks](/tasks/) to:
<br>

- Form dependencies between code, allowing us to parallelize setup as part of `enterShell` and execute tasks after something has been set up, like `python:virtualenv`.
- Write automation in your favorite language, ditching shell code.
- Gain visibility into what's happening inside your developer environment.


## Usage

For example if you'd like to execute python code after virtualenv has been created:

```nix title="devenv.nix"
{ pkgs, lib, config, ... }: {
languages.python.enable = true;
languages.python.venv.enable = true;
tasks = {
"python:setup" = {
exec = "python ${pkgs.writeText "setup.py" ''
print("hello world")
''}";
after = [ "devenv:python:virtualenv" ];
};
"devenv:enterShell".after = [ "python:setup" ];
};
}
```

`python:setup` task executes before `devenv:enterShell` but after `python:virtualenv` task:

![Tasks interactive example](/assets/images/tasks.gif)


For all supported use cases see [tasks documentation](/tasks/).


## Task Server Protocol for SDKs

We've talked to many teams that **dropped Nix** after a while and they usually fit into two categories:

* 1) Maintaining **Nix was too complex** and the team didn't fully onboard, **creating friction inside the teams**.
* 2) Went **all-in Nix** and it took **a big toll on the team productivity**.

While devenv already addresses (1), bridging **the gap between Nix provided developer environments
and existing devops tooling written in your favorite language is still an unsolved problem until now**.
<br>

We've designed [Task Server Protocol](https://github.com/cachix/devenv/issues/1457) so that you can write tasks
using your existing automation by providing an executable that exposes the tasks to devenv:
<br>

```nix title="devenv.nix"
{ pkgs, ... }:
let
myexecutable = pkgs.rustPlatform.buildRustPackage rec {
pname = "foo-bar";
version = "0.1";
cargoLock.lockFile = ./myexecutable/Cargo.lock;
src = pkgs.lib.cleanSource ./myexecutable;
}
in {
task.serverProtocol = [ "${myexecutable}/bin/myexecutable" ];
}
```

In a few weeks we're planning to provide [Rust TSP SDK](https://github.com/cachix/devenv/issues/1457)
with a full test suite so you can implement your own abstraction in your language of choice.
<br>

Tasks will enable you to use your favorite language to replace automation with
`devenv tasks run <names>`.

Tasks will enable us to use Rust instead of maintaining bash for glue code in devenv itself.

## Upgrading

If you run `devenv update` on your existing repository you should already be using tasks,
without needing to upgrade to devenv 1.2.

Domen

0 comments on commit 1fd85e5

Please sign in to comment.