#!/usr/bin/env bash set -euo pipefail # Print styled messages info() { echo -e "\033[1;34m[INFO]\033[0m $1"; } success() { echo -e "\033[1;32m[SUCCESS]\033[0m $1"; } error() { echo -e "\033[1;31m[ERROR]\033[0m $1"; exit 1; } # Check if flake.nix already exists if [[ -f "flake.nix" ]]; then error "A flake.nix file already exists in this directory!" fi # Prompt user for template type echo "Select a flake template to generate:" echo "1) Generic / Minimal" echo "2) Go" echo "3) Node.js / TypeScript" echo "4) Python" echo "5) Rust" read -rp "Enter choice [1-5]: " CHOICE case "$CHOICE" in 1) TEMPLATE_TYPE="generic" PACKAGES="ripgrep jq curl" EXTRA_VARS="" ;; 2) TEMPLATE_TYPE="go" PACKAGES="go gopls golangci-lint delve" EXTRA_VARS="GOPATH = \"\$(pwd)/.go\";" ;; 3) TEMPLATE_TYPE="node" PACKAGES="nodejs_20 corepack nodePackages.typescript-language-server" EXTRA_VARS="" ;; 4) TEMPLATE_TYPE="python" PACKAGES="(python3.withPackages (ps: with ps; [ numpy requests ])) ruff pyright" EXTRA_VARS="" ;; 5) TEMPLATE_TYPE="rust" PACKAGES="cargo rustc clippy rustfmt rust-analyzer" EXTRA_VARS="" ;; *) error "Invalid selection." ;; esac info "Generating flake.nix for [$TEMPLATE_TYPE] environment..." # Generate flake.nix cat < flake.nix { description = "Development environment for $TEMPLATE_TYPE"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; }; outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.\${system}; in { devShells.default = pkgs.mkShell { packages = with pkgs; [ $PACKAGES ]; env = { $EXTRA_VARS }; shellHook = '' echo "🚀 Entering $TEMPLATE_TYPE dev environment" ''; }; } ); } EOF # Optional: Create .envrc for direnv integration if [[ ! -f ".envrc" ]]; then read -rp "Do you want to create a .envrc file for direnv integration? [y/N]: " ENABLE_DIRENV if [[ "$ENABLE_DIRENV" =~ ^[Yy]$ ]]; then echo "use flake" > .envrc info "Created .envrc" if command -v direnv &> /dev/null; then direnv allow fi fi fi # Git integration requirement if [[ -d ".git" ]]; then info "Staging flake.nix in Git..." git add flake.nix if [[ -f ".envrc" ]]; then git add .envrc 2>/dev/null || true fi else info "Note: Git repository not detected. If you initialize Git, remember to run 'git add flake.nix'." fi success "flake.nix successfully generated!" echo -e "\nRun \033[1mnix develop\033[0m to enter your new shell."