summaryrefslogtreecommitdiff
path: root/home-manager/scripts/create-flake.sh
blob: 1c007018c76a52c7ad07f844832e4c1031753c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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 <<EOF > 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."