This Nix flake provides reusable development environments and tools for setting up new projects. It can be used independently or integrated with the main system configuration flake.
- Bootstrap Environment:
- Universal shell for initializing projects.
- Includes essential tools like
git
,pre-commit
, and programming language-specific utilities. - Detects whether Nix is installed, and whether flakes are activated.
- Reusable Shells:
- Pre-configured environments for Python, Web, Rust, Go, and Java projects.
- Project Initialization:
- Automates project directory creation and environment setup using
project-init.sh
.
- Automates project directory creation and environment setup using
Inside your project directory, run the following command:
curl -fsSL "https://raw.githubusercontent.com/iansherr/nix-devflake/main/devinit/project-init.sh" | bash
- You can copy the init script and flake configuration to a new project directory using the following command: Create a new directory in your home that's called nix-devflake.
mkdir ~/nix-devflake
- Then copy the development directory to the nixdev directory.
git clone https://github.com/iansherr/nix-devflake ~/nix-devflake
- Then create a new directory for your project and copy the development directory to the new project directory.
mkdir ~/projects/my-project/.devenv && cp -r ~/nix-devflake/ ~/projects/my-project/.devenv
- Then navigate to the new project directory and run the initialization script.
cd ~/projects/my-project
chmod +x ./devenv/devinit/project-init.sh ./.devenv/devinit/project-init.sh
- Follow the prompts.
- Make sure you are inside your project folder:
cd ~/projects/my-project
- Check if the required files exist:
ls .devenv/devinit/flake.nix .devenv/devinit/project-init.sh
If both files exist, continue. If they are missing, copy the development flake from your template directory:
- Now, enter the Nix bootstrap environment.
nix develop .#bootstrap
This prepares the shell for the project setup.
- Create a project subdirectory. This keeps the flake and Nix weirdness out of your git repo.
mkdir my-project
So, you should now have a directory structure like this:
~/my-project/
├── .devenv/
│ ├── devinit/
│ ├── flake.nix
│ ├── project-init.sh
│ ├── README.md
├─ my-project
│ ├── Empty OR Whatever_Project_Files
- Create the relevant flake for your project.
cat <<EOF >flake.nix
{
description = "${PROJECT_NAME} development environment";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs }: let
# Helper function to iterate over supported systems
forEachSystem = f: {
x86_64-linux = f "x86_64-linux";
x86_64-darwin = f "x86_64-darwin";
aarch64-darwin = f "aarch64-darwin";
aarch64-linux = f "aarch64-linux";
riscv64-linux = f "riscv64-linux";
};
in {
devShells = forEachSystem (system: let
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
};
in {
default = pkgs.mkShell {
name = "${ENVIRONMENT}dev";
buildInputs = with pkgs; [
# common
git
pre-commit
# neovim and plugins build requirements
cmake
curl
zoxide
ncurses
neovim
unzip
# Needed by plugins
fd
lazygit
jq
ripgrep
tree-sitter
xclip
# OS
bashmount
EOF
Add relevant extras for your project
cat <<EOF >>flake.nix
# Python
python3
poetry
black
flake8
mypy
isort
pylint
EOF
Finish the flake file.
cat <<EOF >>flake.nix
];
shellHook = ''
echo "${ENVIRONMENT} development environment loaded!"
if [ -f .pre-commit-config.yaml ]; then
echo "Pre-commit config detected. Installing hooks..."
pre-commit install
fi
'';
};
});
};
}
EOF
- Tell Nix to use the flake.
echo "use flake ." > .envrc
direnv allow
nix flake update
direnv reload
- Move into the project directory.
cd my-project
- Have fun.
This project is licensed under the MIT License.