nib build¶
Build a standalone macOS .app bundle from a Nib application. The output is a self-contained application that runs without requiring Python or any dependencies on the target system.
Usage¶
nib build [script.py] [options]
If no script is specified, nib build reads the entry point from pyproject.toml:
nib build # Uses [tool.nib] entry (defaults to src/main.py)
nib build src/main.py # Explicit script path
The resulting bundle is written to dist/AppName.app by default.
Options¶
General¶
| Flag | Description |
|---|---|
-o, --output <dir> |
Output directory (default: dist/) |
-n, --name <name> |
App display name (default: from pyproject.toml or script name) |
-i, --icon <path> |
Icon file: .icns or .png (PNG is auto-converted to ICNS) |
--identifier <id> |
Bundle identifier (default: com.nib.<name>) |
--version <ver> |
App version string (default: 1.0.0) |
--extra-deps <pkgs> |
Additional pip packages to include, comma-separated |
--exclude <names> |
Files and directories under the script's directory not to copy, comma-separated. tests, scripts, dist, build, .venv and .git are always excluded |
--min-macos <ver> |
Minimum macOS version (default: 14.0, which the runtime needs) |
--arch <arch> |
Target architecture: arm64 or x86_64 (default: current machine) |
--python-version <ver> |
Python major.minor to bundle, for example 3.13 (default: the Python running nib) |
--system-python [cmd] |
Do not bundle an interpreter: the app runs on python3 from your login shell, or the command or path given. See System Python |
-v, --verbose |
Info-level logs. -vv for debug-level logs |
Code protection¶
| Flag | Description |
|---|---|
--native |
Compile .py files to native .so modules via Cython |
--obfuscate |
Strip source file paths and line numbers from .pyc bytecode. Names are kept so tracebacks, logging and pickling keep working |
--no-compile |
Keep .py source files as-is (skip bytecode compilation) |
By default, Python files are compiled to .pyc bytecode with optimization level 2 (docstrings and asserts removed).
Mutually exclusive flags
--nativeand--no-compilecannot be used together--obfuscateand--no-compilecannot be used together--nativeand--obfuscatecannot be used together (native code is already opaque)
Optimization¶
| Flag | Description |
|---|---|
--optimize |
Optimize bundle size: strips debug symbols from binaries, prunes unused stdlib modules |
System Python¶
--system-python builds an app that runs on a Python installed on the Mac instead of carrying its own. Your code is still compiled and its dependencies vendored into the bundle; only the interpreter is left out, which is most of the size. A counter app goes from about 50 MB to 8 MB.
nib build --system-python # python3, as your login shell finds it
nib build --system-python python3.12 # another command
nib build --system-python /opt/conda/envs/tools/bin/python # a fixed path
The same choice goes in pyproject.toml as system_python = true, a command name or a path.
How the app finds Python¶
Nib assumes no install location. A command name is resolved the way your terminal resolves it: the build and the app ask your login shell (command -v python3), which loads your configuration, so Homebrew, pyenv, conda or any other setup gives the same answer as in a terminal. An app opened from the Finder does not see that configuration by itself, which is why the shell is asked.
Asking the shell takes from a few tens to a few hundred milliseconds, so the app does it as little as possible. At launch it tries, in order:
- The interpreter that worked last time, which the app remembers.
- The interpreter the build found, recorded in
Info.plist. - A new lookup through the login shell. Its result is remembered for the next launch.
The first two are checked without starting anything: the file must exist and its name must be the right version, such as python3.12. On the Mac you built on, the app normally starts from step 2 and never asks the shell; on another Mac it asks once.
A path given in system_python is used as is and never looked up.
Version and portability¶
The bytecode and the vendored extensions are built by the Python running nib, so the app's interpreter must be the same minor version. The build checks this and stops if python3 in your shell is another version, for example 3.13 while nib runs on 3.12; set system_python to python3.12, or run nib with 3.13.
At launch, an interpreter of another minor version is refused with a message saying to rebuild, instead of failing on import. This happens after an upgrade such as Homebrew moving python3 from 3.12 to 3.13.
Packages in your user site are ignored at launch, so they cannot shadow the vendored ones. --python-version and --arch must match the Python running nib. --native, --obfuscate and --optimize work as usual.
Use it for apps you build for yourself; bundle the interpreter for anything you distribute.
Build pipeline¶
The build process runs through six phases:
-
Setup: Locates the
nib-runtimeSwift executable and detects third-party dependencies via AST analysis of your script. Dependencies listed in[project].dependenciesinpyproject.tomltake precedence over auto-detection. -
Python environment: Downloads a portable python-build-standalone distribution (cached in
~/.cache/nib/), extracts it, and vendors all dependencies withpip install --target. With--system-pythononly the vendoring runs. -
Copy code: Copies your script, sibling
.pyfiles, local packages, and theassets/directory into the bundle. Fonts inassets/are automatically registered. -
Compile: Compiles Python to bytecode (
.pyc), native modules (.so), or leaves source as-is depending on flags. -
Finalize: Installs the Swift runtime as the main executable, converts the icon to
.icnsformat, and generatesInfo.plistfrom your configuration. -
Cleanup and sign: Prunes the Python distribution (removes test suites, idle, tkinter, etc.), optionally strips binaries, and applies an ad-hoc code signature.
Bundle structure¶
My App.app/
└── Contents/
├── Info.plist
├── MacOS/
│ └── My App # Swift runtime (main executable)
└── Resources/
├── app/
│ ├── main.pyc # Your compiled application code
│ └── vendor/ # Vendored dependencies + nib SDK
├── assets/ # Your assets (icons, images, fonts)
└── python/ # Embedded Python distribution (absent with --system-python)
At launch the runtime is the parent: it opens the socket, then starts Python on main.pyc.
Examples¶
Build with defaults from pyproject.toml:
nib build
Build with a custom name and icon:
nib build src/main.py --name "Weather Widget" --icon src/assets/icon.png
Build with native compilation for maximum code protection:
nib build --native
Cython required
The --native flag requires Cython to be installed: pip install cython
Build an optimized, obfuscated release:
nib build --obfuscate --optimize
Build for a specific architecture:
nib build --arch x86_64
Build a small app for your own Mac, on the Python installed there:
nib build --system-python
Build with additional dependencies that auto-detection missed:
nib build --extra-deps "pillow,scipy"
Configuration precedence¶
When both CLI arguments and pyproject.toml settings are present, CLI arguments take precedence:
- Command-line arguments (highest priority)
[tool.nib.build]section inpyproject.toml- Default values
See the pyproject.toml configuration reference for all available settings.
Related¶
- pyproject.toml Config: build options
- Assets and Fonts: assets in the bundle
- nib run
- Architecture: bundled launch mode