Skip to content

A Nix flake and shell script that dynamically set up development environments for new projects. It can be used independently or integrated with the main system configuration flake.

Notifications You must be signed in to change notification settings

iansherr/nix-devflake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Nix DevFlake

Overview

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.

Features

  • 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.

Usage

Remote easy install (recommended)

Inside your project directory, run the following command:

curl -fsSL "https://raw.githubusercontent.com/iansherr/nix-devflake/main/devinit/project-init.sh" | bash

Easy copy install

  1. 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
  1. Then copy the development directory to the nixdev directory.
git clone https://github.com/iansherr/nix-devflake ~/nix-devflake
  1. 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
  1. 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
  1. Follow the prompts.

Manual install

  1. Make sure you are inside your project folder:
cd ~/projects/my-project
  1. 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:

  1. Now, enter the Nix bootstrap environment.
nix develop .#bootstrap

This prepares the shell for the project setup.

  1. 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
  1. 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
  1. Tell Nix to use the flake.
echo "use flake ." > .envrc
direnv allow
nix flake update
direnv reload
  1. Move into the project directory.
cd my-project
  1. Have fun.

License

This project is licensed under the MIT License.

About

A Nix flake and shell script that dynamically set up development environments for new projects. It can be used independently or integrated with the main system configuration flake.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published