feat: add notebook-focused dev shell

This commit is contained in:
Rasyidan Akbar F. 2025-10-27 11:27:22 +07:00
commit 3da4f4d493
4 changed files with 110 additions and 62 deletions

View file

@ -34,37 +34,35 @@ nix develop .#python-uv
- Ruff (linter/formatter) - Ruff (linter/formatter)
- Pyright (type checker) - Pyright (type checker)
### `ml-ai` ### `ai-notebook`
Machine Learning and AI development with HuggingFace model management. Notebook-focused ML/AI environment with HuggingFace tooling and JupyterLab.
```bash ```bash
nix develop .#ml-ai nix develop .#ai-notebook
``` ```
**Includes:** **Includes:**
- Python 3.12 with huggingface-hub - Python 3.12 with JupyterLab, NumPy/Pandas/SciPy stack
- `huggingface-cli` for downloading models - HuggingFace `transformers`, `datasets`, `accelerate`, `tokenizers`
- UV for package management - Vision tooling: OpenCV, scikit-image, Pillow, albumentations, Altair/Plotly
- Git LFS for large files - Optional CPU PyTorch (Linux hosts) plus torchvision/torchaudio
- Ruff and Pyright - UV, Ruff, Pyright, Git LFS
**Model cache:** `~/.cache/huggingface` (persistent across projects) **HuggingFace caches:**
- Models: `~/.cache/huggingface/hub`
- Transformers: `~/.cache/huggingface/transformers`
**Example usage:** **Example usage:**
```bash ```bash
# Download a TTS model # Launch JupyterLab with AI helpers
huggingface-cli download facebook/mms-tts-eng jupyter lab
# Download a small LLM # Download a model or dataset
huggingface-cli download meta-llama/Llama-3.2-1B huggingface-cli download meta-llama/Llama-3.2-1B
python -c "from datasets import load_dataset; load_dataset('ag_news')"
# Download specific files only # Fine-tune with Accelerate
huggingface-cli download openai/whisper-large-v3 --include "*.json" "*.safetensors" python -m accelerate.commands.launch train.py
# Use in your project
uv init my-ml-project
cd my-ml-project
uv add transformers torch
``` ```
### `go` ### `go`
@ -114,7 +112,7 @@ Use the full path to your dotfiles:
```bash ```bash
cd ~/my-project cd ~/my-project
nix develop ~/Development/dotfiles#ml-ai nix develop ~/Development/dotfiles#ai-notebook
``` ```
### Option 2: Flake Registry Alias (Recommended) ### Option 2: Flake Registry Alias (Recommended)
@ -130,7 +128,7 @@ nix registry list | grep dotfiles
**Then from anywhere:** **Then from anywhere:**
```bash ```bash
cd ~/any-project cd ~/any-project
nix develop dotfiles#ml-ai nix develop dotfiles#ai-notebook
``` ```
Benefits: Shorter commands, cleaner paths, easy to remember! Benefits: Shorter commands, cleaner paths, easy to remember!
@ -145,14 +143,14 @@ nix registry add dotfiles ~/Development/dotfiles
# In each project: # In each project:
cd ~/my-ml-project cd ~/my-ml-project
echo "use flake dotfiles#ml-ai" > .envrc echo "use flake dotfiles#ai-notebook" > .envrc
direnv allow direnv allow
``` ```
**Alternative: Without registry (using full path):** **Alternative: Without registry (using full path):**
```bash ```bash
cd ~/my-ml-project cd ~/my-ml-project
echo "use flake ~/Development/dotfiles#ml-ai" > .envrc echo "use flake ~/Development/dotfiles#ai-notebook" > .envrc
direnv allow direnv allow
``` ```
@ -172,7 +170,7 @@ Create a `flake.nix` in your project that imports the shell:
let let
system = "aarch64-darwin"; system = "aarch64-darwin";
in { in {
devShells.${system}.default = dotfiles.devShells.${system}.ml-ai; devShells.${system}.default = dotfiles.devShells.${system}.ai-notebook;
}; };
} }
``` ```
@ -187,9 +185,9 @@ nix develop
| Method | Command | Pros | | Method | Command | Pros |
|--------|---------|------| |--------|---------|------|
| Direct path | `nix develop ~/Development/dotfiles#ml-ai` | Simple, no setup | | Direct path | `nix develop ~/Development/dotfiles#ai-notebook` | Simple, no setup |
| Registry | `nix develop dotfiles#ml-ai` | Clean, short commands | | Registry | `nix develop dotfiles#ai-notebook` | Clean, short commands |
| direnv + registry | `echo "use flake dotfiles#ml-ai" > .envrc` | Auto-loads, clean | | direnv + registry | `echo "use flake dotfiles#ai-notebook" > .envrc` | Auto-loads, clean |
| Project flake | `nix develop` | Shareable, version-controlled | | Project flake | `nix develop` | Shareable, version-controlled |
## Adding New Shells ## Adding New Shells

54
devshells/ai-notebook.nix Normal file
View file

@ -0,0 +1,54 @@
{ pkgs, lib, python ? pkgs.python312, ... }:
let
pythonWithAI = python.withPackages (ps:
let
basePackages = with ps; [
accelerate
albumentations
altair
datasets
evaluate
huggingface-hub
ipykernel
jupyterlab
matplotlib
numpy
opencv4
pandas
pillow
plotly
scipy
scikit-image
scikit-learn
seaborn
sentencepiece
tokenizers
transformers
];
linuxOnlyPackages = lib.optionals pkgs.stdenv.isLinux
(with ps; [ torch torchaudio torchvision ]);
in basePackages ++ linuxOnlyPackages);
in pkgs.mkShell {
name = "ai-notebook";
packages = with pkgs; [ pythonWithAI uv git-lfs ruff pyright ];
shellHook = ''
export UV_PYTHON="${pythonWithAI}/bin/python3"
export UV_LINK_MODE=copy
export PYTHONNOUSERSITE=1
export HF_HOME="''${XDG_CACHE_HOME:-$HOME/.cache}/huggingface"
export HF_HUB_CACHE="$HF_HOME/hub"
export TRANSFORMERS_CACHE="$HF_HOME/transformers"
export JUPYTER_CONFIG_DIR="''${XDG_CONFIG_HOME:-$HOME/.config}/jupyter"
mkdir -p "$HF_HOME" "$JUPYTER_CONFIG_DIR"
echo "AI notebook shell ready"
echo "Python: ${python.version}"
echo "HuggingFace cache: $HF_HOME"
echo "Launch JupyterLab: jupyter lab"
echo "Launch classic notebook: jupyter notebook"
'';
}

View file

@ -0,0 +1,31 @@
{ pkgs, python ? pkgs.python312, ... }:
let
# Python environment with notebook tooling and core data-science libs
pythonWithNotebook = python.withPackages (ps:
with ps; [
altair
ipykernel
jupyterlab
matplotlib
numpy
pandas
polars
plotly
scipy
scikit-learn
seaborn
]);
in pkgs.mkShell {
name = "jupyter-notebook";
packages = [ pythonWithNotebook pkgs.ruff pkgs.pyright pkgs.git ];
shellHook = ''
export PYTHONNOUSERSITE=1
export JUPYTER_CONFIG_DIR="''${XDG_CONFIG_HOME:-$HOME/.config}/jupyter"
mkdir -p "$JUPYTER_CONFIG_DIR"
echo "Jupyter notebook shell ready"
echo "Python: ${python.version}"
echo "Launch notebook: jupyter lab"
echo "Launch classic: jupyter notebook"
'';
}

View file

@ -1,35 +0,0 @@
{ pkgs, python ? pkgs.python312, ... }:
let
# Create a Python environment with huggingface-cli
pythonWithHF = python.withPackages (ps: [ ps.huggingface-hub ]);
in pkgs.mkShell {
name = "ml-ai";
packages = with pkgs; [ pythonWithHF uv git-lfs ruff pyright ];
shellHook = ''
# Set up UV
export UV_PYTHON="${pythonWithHF}/bin/python3"
export UV_LINK_MODE=copy
# Set up HuggingFace cache (persistent across projects)
export HF_HOME="''${XDG_CACHE_HOME:-$HOME/.cache}/huggingface"
export HF_HUB_CACHE="$HF_HOME/hub"
mkdir -p "$HF_HOME"
# Optional: Set offline mode (uncomment to use only cached models)
# export HF_HUB_OFFLINE=1
echo "ML/AI shell ready"
echo "Python: ${python.version}"
echo "HuggingFace cache: $HF_HOME"
echo ""
echo "Download models with:"
echo " huggingface-cli download <model-id>"
echo ""
echo "Example TTS:"
echo " huggingface-cli download facebook/mms-tts-eng"
echo ""
echo "Example LLM:"
echo " huggingface-cli download meta-llama/Llama-3.2-1B"
'';
}