### Minimal Lock File Example Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/usage.md This snippet demonstrates the core functionality of implementing a lock file converter in pure Nix using pyproject.nix. It serves as a foundational example for understanding the process. ```nix # minimal-lock.nix { pkgs ? import {}, }: { lockfileConverter = pkgs.callPackage ./default.nix {}; } ``` -------------------------------- ### uv.lock File Example for pyzmq Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/overriding.md An example of a lock file entry for the 'pyzmq' package from a 'uv' lock file, showcasing package metadata like version, source, dependencies, and available wheels. ```toml [[package]] name = "pyzmq" version = "26.2.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "cffi", marker = "implementation_name == 'pypy'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/fd/05/bed626b9f7bb2322cdbbf7b4bd8f54b1b617b0d2ab2d3547d6e39428a48e/pyzmq-26.2.0.tar.gz", hash = "sha256:070672c258581c8e4f640b5159297580a9974b026043bd4ab0470be9ed324f1f", size = 271975 } wheels = [ { url = "https://files.pythonhosted.org/packages/28/2f/78a766c8913ad62b28581777ac4ede50c6d9f249d39c2963e279524a1bbe/pyzmq-26.2.0-cp312-cp312-macosx_10_15_universal2.whl", hash = "sha256:ded0fc7d90fe93ae0b18059930086c51e640cdd3baebdc783a695c77f123dcd9", size = 1343105 }, # More binary wheels removed for brevity ] ``` -------------------------------- ### Classic Nix Installation Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/install.md Demonstrates how to import and use pyproject.nix in a classic Nix environment without relying on Flakes. It fetches the repository using `builtins.fetchGit` and makes it available within the Nix expression. ```nix let pkgs = import { }; inherit (pkgs) lib; pyproject-nix = import (builtins.fetchGit { url = "https://github.com/pyproject-nix/pyproject.nix.git"; }) { inherit lib; }; in ... ``` -------------------------------- ### pyproject.toml Configuration Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/use-cases/pyproject.md This TOML file specifies project metadata and build system requirements according to PEP 621. It defines the project name, version, and dependencies. ```toml # pyproject.toml [project] name = "my-python-project" version = "0.1.0" description = "A sample Python project" authors = [ { "name" = "Your Name", "email" = "your.email@example.com" } ] license = {"file" = "LICENSE"} readme = "README.md" requires-python = ">=3.8" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] [project.urls] "Homepage" = "https://github.com/yourusername/my-python-project" "Bug Tracker" = "https://github.com/yourusername/my-python-project/issues" [build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project.dependencies] # Add your project dependencies here # For example: # requests = "^2.28.1" # numpy = "^1.23.4" ``` -------------------------------- ### flake.nix Configuration Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/use-cases/pyproject.md This Nix configuration file defines the development environment for a Python project managed by pyproject.toml. It utilizes Nixpkgs to fetch and build Python packages. ```nix # flake.nix { description = "A Python project managed by pyproject.toml"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; }; outputs = { self = flake-utils.lib.eachDefaultSystem (system: { packages = { my-python-project = pkgs.python.pkgs.buildPythonPackage { pname = "my-python-project"; version = "0.1.0"; src = ./.; # Use pyproject.toml for build inputs format = "setuptools"; # Dependencies are automatically inferred from pyproject.toml # buildInputs = [ pkgs.python ]; # Not needed if using python.withPackages # Optional: Add runtime dependencies if needed #propagatedBuildInputs = [ pkgs.python ]; meta = { description = "A sample Python project"; license = pkgs.lib.licenses.mit; }; }; }; devShells.default = pkgs.mkShell { packages = [ pkgs.python pkgs.python.pkgs.pip pkgs.python.pkgs.setuptools pkgs.python.pkgs.wheel ]; }; }); }; } ``` -------------------------------- ### Editable Python Wheels in Nix Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/build/editable/README.md This section details the rationale behind building editable Python wheels for installation into custom Nix store prefixes. It explains the Nix build process and the necessity of wheels for editable packages to work with `uv pip install --prefix`. ```nix # Build editable Python wheels ## Rationale - Producing editable wheels for installation into a custom prefix (the Nix store) Within Nix we build each Python package in isolation and install them into their own Nix store prefixes (a requirement for per-package incremental builds). For example the requests package in one of my projects is installed into `/nix/store/xhd2c62gj3b5ikwbpsp5kzyb88jc56g5-requests-2.32.3`. This directory contains only the installed files from requests, and not their dependencies. In the case of editable packages that means we have to produce a wheel to be able to run `uv pip install --prefix /nix/store/...` on it. This package is used by [pyprojectEditableHook](https://pyproject-nix.github.io/pyproject.nix/build/hooks.html#function-library-build.packages.hooks.pyprojectEditableHook). - Re-triggering of build system side effects Because a [pyproject.nix build](https://pyproject-nix.github.io/pyproject.nix/build.html) produced virtual environment lives in the Nix store and th __ any side effects have been discarded. Using this tool you can forcibly re-trigger build-system side effects such as running a `cython` build or bootstrapping `meson-python`'s import hooks. ``` -------------------------------- ### Development Environment Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/HACKING.md Starts the project in watch mode, running a Nix-unit test runner and a documentation server. ```nix nix develop -c hivemind ``` -------------------------------- ### Supplementing Rust Package Metadata Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/hacks.md Provides an example of how to supplement metadata for Rust packages adapted using `importCargoLock`. This includes adding native build inputs and system dependencies, such as OpenSSL, which might be missing from the initial adaptation. ```nix final: prev: { cryptography = (hacks.importCargoLock { prev = prev.cryptography; # Cryptography uses a non-standard location for it's Rust packaging cargoRoot = "src/rust"; }).overrideAttrs (old: { nativeBuildInputs = old.nativeBuildInputs ++ final.resolveBuildSystem { maturin = [ ]; setuptools = [ ]; cffi = [ ]; pycparser = [ ]; }; buildInputs = old.buildInputs or [ ] ++ [ pkgs.openssl ]; }); } ``` -------------------------------- ### Override pyzmq Wheel Dependencies in Nix Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/overriding.md Example of overriding the attributes of the 'pyzmq' package to add runtime dependencies when building from a binary wheel. ```nix { pkgs, pyproject-nix }: let pythonSet = pkgs.callPackage pyproject-nix.build.packages { inherit python; }; pyprojectOverrides = final: prev: { pyzmq = prev.pyzmq.overrideAttrs(old: { buildInputs = (old.buildInputs or [ ]) ++ [ pkgs.zeromq ]; }); }; in pythonSet.overrideScope pyprojectOverrides ``` -------------------------------- ### pyproject.nix Package Definition with Passthru Dependencies Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/build.md This example illustrates how pyproject.nix uses the `passthru` attribute to define runtime dependencies separately from build-time dependencies, a key feature for decoupling and avoiding issues with Nix's propagation mechanism. ```nix stdenv.mkDerivation { pname = "setuptools-scm"; version = "8.1.0"; src = fetchurl { url = "https://files.pythonhosted.org/packages/4f/a4/00a9ac1b555294710d4a68d2ce8dfdf39d72aa4d769a7395d05218d88a42/setuptools_scm-8.1.0.tar.gz"; hash = ""; }; passthru = { dependencies = { packaging = [ ]; setuptools = [ ]; }; optional-dependencies = { toml = { toml = [ ]; }; rich = { rich = [ ]; }; }; }; nativeBuildInputs = [ pyprojectHook ] ++ resolveBuildSystem ( { setuptools = [ ]; } ); } ``` -------------------------------- ### Override pyzmq Sdist Build System and Dependencies in Nix Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/overriding.md Example of overriding attributes for building 'pyzmq' from source (sdist), including adding build inputs and specifying native build inputs for the build system. ```nix { pkgs, pyproject-nix }: let pythonSet = pkgs.callPackage pyproject-nix.build.packages { inherit python; }; pyprojectOverrides = final: prev: { pyzmq = prev.pyzmq.overrideAttrs(old: { buildInputs = (old.buildInputs or [ ]) ++ [ pkgs.zeromq ]; dontUseCmakeConfigure = true; nativeBuildInputs = (old.nativeBuildInputs or []) ++ final.resolveBuildSystem ({ cmake = []; ninja = []; packaging = []; pathspec = []; scikit-build-core = []; } // if python.isPyPy then { cffi = []; } else { cython = []; }); }); }; in pythonSet.overrideScope pyprojectOverrides ``` -------------------------------- ### Project Loading Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/introduction.md Demonstrates loading project metadata from various sources including PEP-621 pyproject.toml, PEP-621 with PDM extensions, Poetry pyproject.toml, and requirements.txt. ```nix # Example of loading a project from pyproject.toml let project = pyproject.project.load { src = ./.; # Optionally specify format if not auto-detected # format = "pep621"; }; in { ... } ``` ```nix # Example of loading a project from requirements.txt let project = pyproject.project.load { src = ./.; format = "requirements-txt"; }; in { ... } ``` -------------------------------- ### Using Nixpkgs Prebuilt Packages Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/hacks.md Demonstrates how to adapt prebuilt packages from Nixpkgs using pyproject-nix's `nixpkgsPrebuilt` adapter. This is useful when wheels are unavailable or Nixpkgs already provides a suitable build. ```nix { callPackage, pyproject-nix, python3, python3Packages }: let python = python3; hacks = callPackage pyproject-nix.build.hacks {}; overlay = final: prev: { # Adapt torch from nixpkgs torch = hacks.nixpkgsPrebuilt { from = python3Packages.torchWithoutCuda; prev = prev.torch; }; }; pythonSet = (callPackage pyproject-nix.build.packages { inherit python; }).overrideScope overlay; in pythonSet.mkVirtualenv "torch-venv" { torch = [ ]; } ``` -------------------------------- ### Creating a Virtual Environment with pyproject.nix Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/build.md This snippet shows how to create a virtual environment using pyproject.nix's `mkVirtualEnv` function, demonstrating how packages are aggregated and how to inject custom packages using `overrideScope`. ```nix { pyproject-nix, pkgs }: let python = pkgs.python312; # Inject your own packages on top with overrideScope pythonSet = pkgs.callPackage pyproject-nix.build.packages { inherit python; }; in pythonSet.pythonPkgsHostHost.mkVirtualEnv "test-venv" { build = [ ]; } ``` -------------------------------- ### Pyproject.nix Build Infrastructure Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/nixpkgs-build.md This snippet describes the core functionality of Pyproject.nix, highlighting its use with nixpkgs and its own improved build infrastructure. ```nix # Build infrastructures Pyproject.nix can be used with nixpkgs `buildPythonPackage`/`packageOverrides`/`withPackages`, but also implements its [own build infrastructure](./build.md) that fixes many structural problems with the nixpkgs implementation. ``` -------------------------------- ### Load requirements.txt with python.withPackages Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/use-cases/requirements.md This snippet demonstrates how to load a requirements.txt file to create a Python environment using the `python.withPackages` function from nixpkgs. It assumes the existence of a `requirements.txt` file in the project. ```nix # requirements.txt Many projects comes without proper packaging and use [`requirements.txt`](https://pip.pypa.io/en/stable/reference/requirements-file-format/) files to declare their dependencies. This example loads `requirements.txt` to create an environment using [`python.withPackages`](https://nixos.org/manual/nixpkgs/stable/#user-guide) with packages from nixpkgs. ## flake.nix ```nix { ... }: { ... } ``` ``` -------------------------------- ### Creating a Base Package Set Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/packages.md This snippet demonstrates how to create a base package set using `pyproject.nix`. It calls the `packages` function from `pyproject-nix.build` and specifies the Python interpreter. ```nix # Returns a scope with base packages. pkgs.callPackage pyproject-nix.build.packages { python = interpreter; } ``` -------------------------------- ### Adding Missing Python Packages to Nixpkgs Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/FAQ.md Demonstrates how to use a Nix overlay to add a Python package (like 'ruff') to the Python environment when it's not available in `python3.pkgs` but exists in the top-level Nixpkgs. ```nix let python = pkgs.python3.override { packageOverrides = self: super: { ruff = pkgs.ruff; }; }; in ... ``` -------------------------------- ### Nix Configuration for pyproject.nix Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/build/checks/fixtures/myapp/README.md This snippet shows the basic Nix configuration for the pyproject.nix file, likely defining project metadata and dependencies for a Nix-based build system. ```nix # myapp A simple application used to integration test build renderers ``` -------------------------------- ### Renderers Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/introduction.md Shows how renderers transform a project's metadata and a Python interpreter derivation into a format suitable for Nixpkgs Python infrastructure. ```nix # Example of using a renderer for buildPythonPackage let project = pyproject.project.load { src = ./.; }; pythonEnv = pkgs.python311; renderer = pyproject.renderers.buildPythonPackage; buildInputs = renderer.render { project = project; python = pythonEnv; }; in pkgs.stdenv.mkDerivation { name = "my-python-package"; src = ./.; buildInputs = buildInputs ++ [ pythonEnv ]; # ... other build attributes } ``` -------------------------------- ### Nix Derivation for pyzmq from Source Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/overriding.md A minimal Nix derivation to build the 'pyzmq' package from its source tarball, including necessary build inputs and hooks. ```nix { stdenv, pyprojectHook, fetchurl }: stdenv.mkDerivation { pname = "pyzmq"; version = "26.2.0"; src = fetchurl { url = "https://files.pythonhosted.org/packages/fd/05/bed626b9f7bb2322cdbbf7b4bd8f54b1b617b0d2ab2d3547d6e39428a48e/pyzmq-26.2.0.tar.gz"; hash = "sha256:070672c258581c8e4f640b5159297580a9974b026043bd4ab0470be9ed324f1f"; }; dontUseCmakeConfigure = true; buildInputs = [ zeromq ]; nativeBuildInputs = [ pyprojectHook ] ++ resolveBuildSystem ({ cmake = []; ninja = []; packaging = []; pathspec = []; scikit-build-core = []; } // if python.isPyPy then { cffi = []; } else { cython = []; }); passthru.dependencies = lib.optionalAttrs python.isPyPy { cffi = []; }; } ``` -------------------------------- ### Handling Dynamic Attributes in buildPythonPackage Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/FAQ.md Illustrates a common issue where `buildPythonPackage` fails due to missing 'version' attributes when dealing with dynamic fields in `pyproject.toml`. It shows how to manually provide the version attribute. ```nix let project = pyproject.project.loadPyproject { pyproject = lib.importTOML ./pyproject.toml; }; python = pkgs.python3; attrs = pyproject.renderers.buildPythonPackage { inherit python project; }; in python.pkgs.buildPythonPackage attrs ``` -------------------------------- ### Filtering Dependencies for Nixpkgs Prebuilt Packages Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/hacks.md Shows how to filter out specific dependencies when using `nixpkgsPrebuilt`, particularly useful for packages like `torch` that might have redundant binary dependencies already handled by the Nixpkgs build. ```nix hacks.nixpkgsPrebuilt { from = python3Packages.torchWithoutCuda; prev = prev.torch.overrideAttrs(old: { passthru = old.passthru // { dependencies = lib.filterAttrs (name: _: ! lib.hasPrefix "nvidia" name) old.passthru.dependencies; }; }); } ``` -------------------------------- ### Manually Specifying Version for buildPythonPackage Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/FAQ.md Provides a solution for the 'version' missing error in `buildPythonPackage` by manually adding the 'version' attribute to the attribute set returned by the renderer. ```nix let project = pyproject.project.loadPyproject { pyproject = lib.importTOML ./pyproject.toml; }; python = pkgs.python3; attrs = pyproject.renderers.buildPythonPackage { inherit python project; }; in python.pkgs.buildPythonPackage (attrs // { version = "1.0"; # Not dynamically inferred }) ``` -------------------------------- ### Validators Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/introduction.md Illustrates the use of validators to check dependency constraints defined in a project, ensuring compliance with specifications. ```nix # Example of validating dependencies let project = pyproject.project.load { src = ./.; }; validationResult = pyproject.validators.validateProjectDependencies project; in if validationResult.isValid then "Dependencies are valid" else "Dependencies are invalid: ${validationResult.message}" ``` -------------------------------- ### Cross-Compilation Overrides for pyzmq in Nix Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/overriding.md Demonstrates how to handle cross-compilation overrides for packages like 'pyzmq' in Nix, ensuring build and host platform dependencies are correctly managed. ```nix { pkgs, pyproject-nix }: let pythonSet = pkgs.callPackage pyproject-nix.build.packages { inherit python; }; pyprojectOverrides = final: prev: { pyzmq = prev.pyzmq.overrideAttrs(old: { buildInputs = (old.buildInputs or [ ]) ++ [ pkgs.zeromq ]; }); pyprojectCrossOverrides = lib.composeExtensions (_final: prev: { pythonPkgsBuildHost = prev.pythonPkgsBuildHost.overrideScope overlay; }) overlay; in pythonSet.overrideScope pyprojectCrossOverrides ``` -------------------------------- ### Project Dependencies Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/lib/fixtures/requirements.txt Specifies the project's dependencies and their versions. This entry details the 'requests' library requirement. ```nix requests == 2.31.0 ``` -------------------------------- ### Building Rust Packages with Cargo Lock Files Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/builders/hacks.md Illustrates how to use `pyproject-nix`'s `importCargoLock` to adapt Python packages that rely on Rust components, like `cryptography`. This method helps manage Rust dependencies by vendoring them. ```nix final: prev: { cryptography = (hacks.importCargoLock { prev = prev.cryptography; # Cryptography uses a non-standard location for it's Rust packaging cargoRoot = "src/rust"; }); } ``` -------------------------------- ### Running Integration Tests Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/HACKING.md Executes integration tests that perform environment constructions and builds. ```nix nix flake check ``` -------------------------------- ### Code Formatting Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/HACKING.md Formats the code using the nix fmt command and verifies Flake checks. ```nix # Format the code nix fmt # Ensure Flake checks pass nix flake check ``` -------------------------------- ### Running Unit Tests Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/HACKING.md Executes the entire unit test suite or tests for an individual function. ```nix # Run the entire unit test suite nix-unit --flake .#libTests ``` ```nix # Run unit tests for an individual function nix-unit --flake .#libTests.pep440.parseVersion ``` -------------------------------- ### Nix Development Shell with Nixpkgs Python Builders Source: https://github.com/pyproject-nix/pyproject.nix/blob/master/doc/src/build.md This snippet demonstrates a common issue in Nix development shells using nixpkgs Python builders, where dependencies can leak into the PYTHONPATH, making undeclared dependencies available and potentially overriding virtualenv dependencies. ```nix let pkgs = import { }; pythonEnv = pkgs.python3.withPackages(ps: [ ps.requests ]); in pkgs.mkShell { packages = [ pkgs.remarshal pythonEnv ]; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.