Skip to content

Commit

Permalink
flake template: Use project.renderers & reword comments
Browse files Browse the repository at this point in the history
  • Loading branch information
adisbladis committed Nov 11, 2023
1 parent 8272c6c commit 1db4afd
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions templates/pyproject/flake.nix
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
{
description = "A basic flake using pyproject.toml project metadata";

inputs.pyproject.url = "github:nix-community/pyproject.nix";
inputs.pyproject.inputs.nixpkgs.follows = "nixpkgs";
inputs.pyproject-nix.url = "github:nix-community/pyproject.nix";
inputs.pyproject-nix.inputs.nixpkgs.follows = "nixpkgs";

outputs = { nixpkgs, pyproject, ... }:
outputs = { nixpkgs, pyproject-nix, ... }:
let
inherit (nixpkgs) lib;

# Loads pyproject.toml into a high-level project representation
# Do you notice how this is not tied to any `system` attribute or package sets?
# That is because `project` refers to a pure data representation.
project = pyproject.lib.project.loadPyproject {
# Read & unmarshal pyproject.toml
pyproject = lib.importTOML ./pyproject.toml;
project = pyproject-nix.lib.project.loadPyproject {
# Read & unmarshal pyproject.toml relative to this project root.
# projectRoot is also used to set `src` for renderers such as buildPythonPackage.
projectRoot = ./.;
};

# This example is only using x86_64-linux
pkgs = nixpkgs.legacyPackages.x86_64-linux;

# We are using the default Python3 interpreter & package set
# We are using the default Python3 interpreter & package set.
# This means that you are purposefully ignoring:
# - Version bounds
# - Dependency sources (meaning local path dependencies won't resolve to the local path)
#
# To use packages from local sources see "Overriding Python packages" in the nixpkgs manual:
# https://nixos.org/manual/nixpkgs/stable/#reference
#
# Or use an overlay generator such as pdm2nix:
# https://github.com/adisbladis/pdm2nix
python = pkgs.python3;

in
{

# Create a development shell containing dependencies from `pyproject.toml`
devShells.x86_64-linux.default =
let
# Returns a function that can be passed to `python.withPackages`
arg = pyproject.lib.renderers.withPackages { inherit python project; };
arg = project.renderers.withPackages { inherit python; };

# Returns a wrapped environment (virtualenv like) with all our packages
pythonEnv = python.withPackages arg;

in
# Create a devShell like normal.
pkgs.mkShell {
packages = [ pythonEnv ];
};
Expand All @@ -43,8 +53,10 @@
packages.x86_64-linux.default =
let
# Returns an attribute set that can be passed to `buildPythonPackage`.
attrs = pyproject.lib.renderers.buildPythonPackage { inherit python project; };
attrs = project.renderers.buildPythonPackage { inherit python; };
in
# Pass attributes to buildPythonPackage.
# Here is a good spot to add on any missing attributes such as `src`.
python.pkgs.buildPythonPackage attrs;
};
}

0 comments on commit 1db4afd

Please sign in to comment.