# Installing agent instructions


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
import sys
from tempfile import TemporaryDirectory
from unittest.mock import patch as patch_cli
```

[`install_nbskill`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbskill)
copies the package-owned routing skill and its references into a Codex
or Claude skills directory. The routing file points to the Pyskill API
in `nbskill.skill`; executable guidance stays with the Python package
and its installed version. Pre-commit hooks are an optional installation
step.

``` python
from nbskill.nbskill import install_nbskill
from nbskill.foundation import demo_path, remove_demo_path
from unittest.mock import patch
```

The package ships a compact routing skill. The operational guidance
lives in the Python skill module, so installation copies package-owned
instructions rather than generating a second workflow.

## Production contract

Skill installation is Python-first. It installs only into the requested
Codex or Claude skills directory, optionally installs notebook hooks.

## Project setup and hooks

Project configuration and Git hooks belong to installation. Foundation
only detects project ownership and does not install or repair a
workspace.

Managed hook markers delimit the block installed by nbskill. They let
later installation update that block without replacing unrelated hook
content.

The managed pre-commit block runs `nbdev-clean` and rejects a commit
when cleaning changes files. It then runs notebook tests, using `uv`
when the commands are not on the path.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/nbskill.py#L116"
target="_blank" style="float:right; font-size:smaller">source</a>

### install_nbdev_pre_commit_hooks

``` python
def install_nbdev_pre_commit_hooks(
    path:str='.', run_nbdev_install_hooks:bool=True
):
```

*Install nbdev-clean and nbdev-test pre-commit hooks in a git-backed
nbdev project.*

## Respecting hook removal

[`install_nbdev_pre_commit_hooks`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbdev_pre_commit_hooks)
records installation in `.git/info`. If you remove the hook, a later
call records that choice and leaves it absent. This temporary Git
repository demonstrates both states:

``` python
with TemporaryDirectory() as hook_example_dir:
    hook_example_root = Path(hook_example_dir)
    (hook_example_root/"pyproject.toml").write_text('[project]\nname = "hook_example"\nversion = "0.0.1"\n[tool.nbdev]\n')
    subprocess.run(["git", "init"], cwd=hook_example_root, check=True, capture_output=True)
    installed_hook = install_nbdev_pre_commit_hooks(hook_example_root, run_nbdev_install_hooks=False)
    assert installed_hook["installed"]
installed_hook["installed"]
```

## Bootstrapping project configuration

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/nbskill.py#L254"
target="_blank" style="float:right; font-size:smaller">source</a>

### bootstrap_nbskill_project

``` python
def bootstrap_nbskill_project(
    path:str='.', overwrite_agents:bool=False, pause:str='1'
):
```

*Install nbskill project guardrails once a workspace root is known.*

A project can need a small idempotent bootstrap before using nbskill.
Once a workspace root is known,
[`bootstrap_nbskill_project`](https://MorsCerta-crypto.github.io/nbskill/skill.html#bootstrap_nbskill_project)
can copy the shared agent instructions, make notebook CI serial, and
preserve nbskill metadata through nbdev cleanups without clobbering
local `AGENTS.md` edits.

## Installing agent instructions

The installer copies the compact routing skill and its reference
material. Native notebook methodology lives in the Python skill module;
installation only makes those files available to the chosen agent.

Installation targets select a skills directory before package files are
copied. Existing identical files remain unchanged; overwriting a
modified file depends on the caller’s option.

### [`install_nbskill`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbskill)

[`install_nbskill`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbskill)
returns the paths it changed and the paths already current. Set
`skills_dir` to choose a destination, and `overwrite=False` to protect a
locally edited routing file. The example copies real package files into
a temporary directory:

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/nbskill.py#L313"
target="_blank" style="float:right; font-size:smaller">source</a>

### install_nbskill

``` python
def install_nbskill(
    target:str='codex', # codex, claude, cursor, both, or custom when skills_dir is set
    skills_dir:str | None=None, # Parent skills directory; skill is installed below jupyter-notebooks
    skill_name:str='jupyter-notebooks', # Skill folder name
    overwrite:bool=True, # Update stale managed files
    install_hooks:bool=False, # Install nbdev-clean/nbdev-test pre-commit hooks in the current repo
):
```

*Install current Python workflow instructions.*

``` python
install_root = demo_path("06_skill_usage")
try:
    installation = install_nbskill(skills_dir=install_root)
    assert (install_root / "jupyter-notebooks" / "SKILL.md").exists()
    install_preview = dict(changed=len(installation["installed"]), hooks=installation["hooks"]["reason"])
finally: remove_demo_path(install_root)
install_preview
```

### [`install_nbskill_cli`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbskill_cli)

The command-line entry point exposes the same destination and overwrite
options as
[`install_nbskill`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbskill).
Use `install_nbskill --help` to inspect them. An actual invocation can
copy files into the selected agent directory, so the notebook
demonstrates the Python API against a disposable destination.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/nbskill.py#L329"
target="_blank" style="float:right; font-size:smaller">source</a>

### install_nbskill_cli

``` python
def install_nbskill_cli():
```

*Install notebook workflow instructions from the command line.*

## Preserving installed files

Repeating an installation with unchanged package content leaves every
file byte-identical and reports it under `unchanged`. This checks the
real files rather than a simulated copy operation.

[`bootstrap_nbskill_project`](https://MorsCerta-crypto.github.io/nbskill/skill.html#bootstrap_nbskill_project)
reports an unrelated directory without writing project files.

``` python
with TemporaryDirectory() as unrelated_project:
    bootstrap_status = bootstrap_nbskill_project(unrelated_project)
    assert not bootstrap_status["changed"] and bootstrap_status["reason"] == "not-an-nbdev-project"
bootstrap_status
```

[`install_nbskill_cli`](https://MorsCerta-crypto.github.io/nbskill/skill.html#install_nbskill_cli)
uses an explicit skills directory so an installation can be inspected
independently of the user’s normal configuration.

``` python
with TemporaryDirectory() as cli_skills_dir:
    with patch_cli.object(sys, "argv", ["install_nbskill", "--skills-dir", cli_skills_dir]): cli_status = install_nbskill_cli()
    assert cli_status is None
    assert (Path(cli_skills_dir)/"jupyter-notebooks"/"SKILL.md").is_file()
```
