This repository contains a collection of templates for setting up
basic project configurations using the devenv
tool
based on the Nix ecosystem.
Table of Contents
Using Nix to manage project-specific development environments has brought about numerous benefits and advantages. Here's why I gravitate towards Nix:
-
Reproducibility: Nix guarantees that the exact same dependencies and environment configurations are used every time. Whether it's being run on my machine, a colleague's machine, or in CI/CD, the environment remains consistent.
-
Isolation: Each project managed by Nix has its own isolated environment. This means that changes or installations in one project won't inadvertently affect another. This isolation prevents the classic "it works on my machine" problem.
-
Atomicity: Nix operations, like installations or upgrades, are atomic. This means that they either fully succeed or don't make any changes at all, ensuring that there's no in-between broken state.
-
Versioning: It's effortless to switch between different versions of tools, libraries, or even the language runtime itself. This flexibility is invaluable, especially when working on multiple projects with different requirements.
-
Flexibility:
devenv
and Nix are not limited to one language or an specific environment. This allows to craft unified workflows across projects. Easy onboarding with a single command + consistent developer helpers and dependency manager, without limiting developers to include their favorite tooling. -
No Global State: Unlike other package managers, Nix doesn’t have global state. This means you can have multiple versions of a package, each with their dependencies, without conflicts.
-
Declarative Configuration: Using
flake.nix
or similar files, the entire project's dependencies and configurations can be declared in one place. This declarative approach not only ensures clarity but also makes setting up a new machine or onboarding a new developer a breeze. -
Cross-Platform Consistency: Nix works across different Unix systems, ensuring that the environment is consistent whether you're using macOS, Linux, or any other Unix-like system.
By leveraging Nix for development environments, I ensure that each project is self-contained, predictable, and free from the common pitfalls associated with system-wide package managers or mutable environments.
Currently, the available templates are:
- Python
- JavaScript/TypeScript
- Terraform
- Go
- Rust
More templates will be added over time, based on personal preferences. However, pull requests are more than welcome if you'd like to contribute.
- Nix Flake Ecosystem: The aim is to utilize the Nix flake
ecosystem, centralizing as much configuration as possible in the
flake.nix
. This minimizes the number of config files that end up in the project. - Guidance: The
.editorconfig
file is used to set general coding rules to guide developers. - Consistency: For low-effort checks to maintain project consistency, mainstream linters are run on hooks.
Each template environment integrates with a set of tools to streamline development and maintain consistency. Here's a breakdown of the tools used for the respective environments:
- difftastic: A tool that provides a more structured approach to diffing, making it easier to understand changes between files.
- nixfmt: A formatter for Nix code, ensuring that Nix scripts are readable and adhere to a consistent style.
- yamllint: This tool is used to lint YAML files. For our environments, we use a simplified configuration.
- editorconfig: Used to define and maintain consistent coding styles between different editors and IDEs.
- black: The uncompromising Python code formatter. It ensures that Python code adheres to a consistent style, making it readable and clean.
- prettier: An opinionated code formatter that supports multiple languages, including JavaScript and TypeScript. It ensures that code has a consistent style, improving readability.
To use these templates, follow these steps:
-
Install
devenv
: Refer to the instructions at devenv.sh/getting-started. -
Initialize your project:
nix flake init --template github:shahinism/devenv-templates#python
Replace
#python
with your desired template. -
Optional but Recommended: To automatically enable the environment, install
direnv
andnix-direnv
:Add the following to the
.envrc
file:if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs=" fi watch_file flake.nix watch_file flake.lock if ! use flake . --impure then echo "devenv flake could not be built. The devenv environment was not loaded. Make the necessary changes to flake.nix and hit enter to try again." >&2 fi
- Add Nodejs template
- Add Terraform template
- Add Go Template
- Add Rust Template
- Migrate from Terraform to OpenTofu
- Make it modular to slice and dice and have multi-configuration environments also possible.
Pull requests are welcome! If you'd like to contribute a new template or improve an existing one, please feel free to make a pull request.