Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce languages.python.finalPackage #1542

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions src/modules/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,7 @@ let
);

readlink = "${pkgs.coreutils}/bin/readlink -f ";
package = pkgs.callPackage "${pkgs.path}/pkgs/development/interpreters/python/wrapper.nix" {
python = cfg.package;
requiredPythonModules = cfg.package.pkgs.requiredPythonModules;
makeWrapperArgs = [
"--prefix"
"LD_LIBRARY_PATH"
":"
libraries
] ++ lib.optionals pkgs.stdenv.isDarwin [
"--prefix"
"DYLD_LIBRARY_PATH"
":"
libraries
];
};
inherit (cfg.finalPackage) interpreter sitePackages;

requirements = pkgs.writeText "requirements.txt" (toString (
if lib.isPath cfg.venv.requirements
Expand Down Expand Up @@ -52,7 +38,7 @@ let

VENV_PATH="${config.env.DEVENV_STATE}/venv"

profile_python="$(${readlink} ${package.interpreter})"
profile_python="$(${readlink} ${interpreter})"
devenv_interpreter_path="$(${pkgs.coreutils}/bin/cat "$VENV_PATH/.devenv_interpreter" 2> /dev/null || echo false )"
venv_python="$(${readlink} "$devenv_interpreter_path")"

Expand All @@ -71,15 +57,15 @@ let
[ -f "${config.env.DEVENV_STATE}/poetry.lock.checksum" ] && rm ${config.env.DEVENV_STATE}/poetry.lock.checksum
''}
${if cfg.uv.enable then ''
echo uv venv -p ${package.interpreter} "$VENV_PATH"
uv venv -p ${package.interpreter} "$VENV_PATH"
echo uv venv -p ${interpreter} "$VENV_PATH"
uv venv -p ${interpreter} "$VENV_PATH"
''
else ''
echo ${package.interpreter} -m venv ${if builtins.isNull cfg.version || lib.versionAtLeast cfg.version "3.9" then "--upgrade-deps" else ""} "$VENV_PATH"
${package.interpreter} -m venv ${if builtins.isNull cfg.version || lib.versionAtLeast cfg.version "3.9" then "--upgrade-deps" else ""} "$VENV_PATH"
echo ${interpreter} -m venv ${if builtins.isNull cfg.version || lib.versionAtLeast cfg.version "3.9" then "--upgrade-deps" else ""} "$VENV_PATH"
${interpreter} -m venv ${if builtins.isNull cfg.version || lib.versionAtLeast cfg.version "3.9" then "--upgrade-deps" else ""} "$VENV_PATH"
''
}
echo "${package.interpreter}" > "$VENV_PATH/.devenv_interpreter"
echo "${interpreter}" > "$VENV_PATH/.devenv_interpreter"
fi

source "$VENV_PATH"/bin/activate
Expand Down Expand Up @@ -143,7 +129,7 @@ let

# Avoid running "uv sync" for every shell.
# Only run it when the "pyproject.toml" file or Python interpreter has changed.
local ACTUAL_UV_CHECKSUM="${package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):''${UV_SYNC_COMMAND[@]}"
local ACTUAL_UV_CHECKSUM="${interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):''${UV_SYNC_COMMAND[@]}"
local UV_CHECKSUM_FILE="$VENV_PATH/uv.sync.checksum"
if [ -f "$UV_CHECKSUM_FILE" ]
then
Expand Down Expand Up @@ -183,7 +169,7 @@ let
unset VIRTUAL_ENV

# Make sure poetry's venv uses the configured Python executable.
${cfg.poetry.package}/bin/poetry env use --no-interaction --quiet ${package.interpreter}
${cfg.poetry.package}/bin/poetry env use --no-interaction --quiet ${interpreter}
}

function _devenv_poetry_install
Expand All @@ -192,7 +178,7 @@ let
# Avoid running "poetry install" for every shell.
# Only run it when the "poetry.lock" file or Python interpreter has changed.
# We do this by storing the interpreter path and a hash of "poetry.lock" in venv.
local ACTUAL_POETRY_CHECKSUM="${package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):$(${pkgs.nix}/bin/nix-hash --type sha256 poetry.lock):''${POETRY_INSTALL_COMMAND[@]}"
local ACTUAL_POETRY_CHECKSUM="${interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):$(${pkgs.nix}/bin/nix-hash --type sha256 poetry.lock):''${POETRY_INSTALL_COMMAND[@]}"
local POETRY_CHECKSUM_FILE=".venv/poetry.lock.checksum"
if [ -f "$POETRY_CHECKSUM_FILE" ]
then
Expand Down Expand Up @@ -239,6 +225,12 @@ in
description = "The Python package to use.";
};

finalPackage = lib.mkOption {
type = lib.types.package;
description = "The final Python package to use.";
internal = true;
};

manylinux.enable = lib.mkOption {
type = lib.types.bool;
default = pkgs.stdenv.isLinux;
Expand Down Expand Up @@ -428,10 +420,27 @@ in
(lib.mkIf (cfg.version != null)
(nixpkgs-python.packages.${pkgs.stdenv.system}.${cfg.version} or (throw "Unsupported Python version, see https://github.com/cachix/nixpkgs-python#supported-python-versions")))
];
languages.python.finalPackage = lib.mkDefault (
pkgs.callPackage "${pkgs.path}/pkgs/development/interpreters/python/wrapper.nix" {
python = cfg.package;
requiredPythonModules = cfg.package.pkgs.requiredPythonModules;
makeWrapperArgs = [
"--prefix"
"LD_LIBRARY_PATH"
":"
libraries
] ++ lib.optionals pkgs.stdenv.isDarwin [
"--prefix"
"DYLD_LIBRARY_PATH"
":"
libraries
];
}
);

cachix.pull = lib.mkIf (cfg.version != null) [ "nixpkgs-python" ];

packages = [ package ]
packages = [ cfg.finalPackage ]
++ (lib.optional cfg.poetry.enable cfg.poetry.package)
++ (lib.optional cfg.uv.enable cfg.uv.package);

Expand Down Expand Up @@ -481,7 +490,7 @@ in
};

enterShell = ''
export PYTHONPATH="$DEVENV_PROFILE/${package.sitePackages}''${PYTHONPATH:+:$PYTHONPATH}"
export PYTHONPATH="$DEVENV_PROFILE/${sitePackages}''${PYTHONPATH:+:$PYTHONPATH}"
'';
};
}