A devenv.nix module for integrating go-task into your development environment. This module automatically adds go-task to your devenv packages and provides declarative configuration for your tasks through Nix.
- Automatically adds go-task to your devenv packages
- Declarative task configuration in Nix
- Automatic generation of Taskfile.yml from Nix configuration
- Support for all go-task features including dependencies, variables, environment variables, and more
- Configurable taskfile location (defaults to
Taskfile.dist.yml)
To use devenv-go-task in your devenv config,
first add it as an input in your devenv.yaml:
inputs:
nixpkgs:
url: github:cachix/devenv-nixpkgs/rolling
devenv-go-task:
url: github:landure/devenv-go-taskThen in your devenv.nix, import and configure the plugin:
{
pkgs,
lib,
config,
inputs,
...
}: {
imports = [inputs.devenv-go-task.plugin];
go-task.enable = true;
go-task.settings = {
vars = {
GREETING = "Hello World!";
};
env = {
CGO_ENABLED = "0";
};
};
go-task.tasks = {
build = {
desc = "Build the project";
cmds = [
"echo 'Building project...'"
"go build -o dist/app ./cmd/app"
];
sources = ["**/*.go"];
generates = ["dist/app"];
};
test = {
desc = "Run tests";
cmds = ["go test ./..."];
deps = ["build"];
};
clean = {
desc = "Clean build artifacts";
cmds = ["rm -rf dist/"];
};
};
}Type: bool
Default: false
Enable the go-task module. When enabled, go-task will be added to your devenv packages and the taskfile will be generated.
Type: package
Default: pkgs.go-task
The go-task package to use. You can override this to use a different version or build of go-task.
Type: str
Default: "Taskfile.dist.yml"
The path where the generated taskfile will be written. This is relative to your project root.
Type: submodule
Default: {}
Global taskfile settings that apply to all tasks:
version(str, default:"3"): Taskfile format versionvars(attrsOf str, default:{}): Global variables available to all tasksenv(attrsOf str, default:{}): Global environment variablesdotenv(listOf str, default:[]): Dotenv files to loadoutput(nullOr enum, default:null): Output mode ("interleaved","group", or"prefixed")silent(bool, default:false): Global silent mode
Type: attrsOf (submodule)
Default: {}
An attribute set defining your tasks. Each task can have the following options:
desc(nullOr str, default:null): Task description shown intask --listcmds(listOf str, default:[]): Commands to execute for this taskdeps(listOf str, default:[]): Task dependencies that must run firstsources(listOf str, default:[]): Source files that trigger task execution when changedgenerates(listOf str, default:[]): Files generated by this taskvars(attrsOf str, default:{}): Task-specific variablesenv(attrsOf str, default:{}): Task-specific environment variablessilent(bool, default:false): Suppress command outputmethod(nullOr enum ["checksum" "timestamp" "none"], default:null): Method to check if task is up-to-datedir(nullOr str, default:null): Directory to run the task inpreconditions(listOf submodule, default:[]): Conditions that must be met before running the tasksh(str): Shell command to checkmsg(nullOr str, default:null): Error message if precondition fails
See the example directory for a complete working example showing various task configurations including:
- Build tasks with source tracking
- Test tasks with dependencies
- Development server tasks with variables and environment
- Cleanup tasks
- Global variables and environment settings
- Preconditions and error handling
Here's a more comprehensive example showing advanced features:
{
pkgs,
lib,
config,
inputs,
...
}: {
imports = [inputs.devenv-go-task.plugin];
go-task.enable = true;
go-task.taskfile = "Taskfile.yml";
# Global settings
go-task.settings = {
version = "3";
vars = {
APP_NAME = "my-app";
BUILD_DIR = "dist";
VERSION = "1.0.0";
};
env = {
CGO_ENABLED = "0";
GOOS = "linux";
};
output = "prefixed";
dotenv = [".env" ".env.local"];
};
go-task.tasks = {
# Default task
default = {
desc = "Run development tasks";
deps = ["install" "build" "test"];
};
# Installation
install = {
desc = "Install dependencies";
cmds = [
"go mod download"
"go mod tidy"
];
sources = ["go.mod" "go.sum"];
preconditions = [
{
sh = "which go";
msg = "Go is not installed or not in PATH";
}
];
};
# Building
build = {
desc = "Build the application";
deps = ["install"];
cmds = [
"mkdir -p {{.BUILD_DIR}}"
"go build -ldflags '-X main.version={{.VERSION}}' -o {{.BUILD_DIR}}/{{.APP_NAME}} ./cmd/{{.APP_NAME}}"
];
sources = ["**/*.go" "go.mod" "go.sum"];
generates = ["{{.BUILD_DIR}}/{{.APP_NAME}}"];
method = "checksum";
};
# Testing
test = {
desc = "Run all tests";
deps = ["install"];
cmds = [
"go test -v -race ./..."
];
env = {
GO_ENV = "test";
};
};
# Coverage
coverage = {
desc = "Generate test coverage report";
deps = ["install"];
cmds = [
"go test -v -race -coverprofile=coverage.out ./..."
"go tool cover -html=coverage.out -o coverage.html"
"echo 'Coverage report: coverage.html'"
];
generates = ["coverage.out" "coverage.html"];
};
# Development server
dev = {
desc = "Start development server";
deps = ["build"];
cmds = [
"./{{.BUILD_DIR}}/{{.APP_NAME}}"
];
vars = {
PORT = "8080";
};
env = {
GO_ENV = "development";
LOG_LEVEL = "debug";
};
};
# Docker operations
docker-build = {
desc = "Build Docker image";
deps = ["build"];
cmds = [
"docker build -t {{.APP_NAME}}:{{.VERSION}} ."
"docker tag {{.APP_NAME}}:{{.VERSION}} {{.APP_NAME}}:latest"
];
sources = ["Dockerfile" "{{.BUILD_DIR}}/{{.APP_NAME}}"];
preconditions = [
{
sh = "which docker";
msg = "Docker is not installed";
}
{
sh = "docker info";
msg = "Docker daemon is not running";
}
];
};
# Cleanup
clean = {
desc = "Clean all build artifacts";
cmds = [
"rm -rf {{.BUILD_DIR}}"
"rm -f coverage.out coverage.html"
"go clean -cache -testcache -modcache"
];
};
# Linting and formatting
lint = {
desc = "Run linter";
cmds = [
"golangci-lint run"
];
sources = ["**/*.go"];
};
format = {
desc = "Format code";
cmds = [
"go fmt ./..."
"goimports -w ."
];
sources = ["**/*.go"];
};
};
}When you run devenv shell, the module will:
- Add go-task to your shell packages
- Generate the taskfile from your Nix configuration
- Make tasks available via the
taskcommand
You can then use standard go-task commands:
# List all available tasks
task --list
# Run a specific task
task build
# Run with verbose output
task --verbose testInstead of maintaining a separate Taskfile.yml,
you can now define your tasks directly in your devenv.nix configuration.
This provides:
- Type safety: Nix will validate your task configuration
- Integration: Tasks can reference other parts of your devenv config
- Consistency: Keep all development configuration in one place
- Reusability: Share task configurations across projects