pythonEnv: init

Add experimental one-liner command to create python environments.

```command
$ nix run github:nix-community/dream2nix#__pythonEnv requests whisper-ctranslate2
...

$ python -c "import requests; print('imported requests')"
imported requests

$ whisper-ctranslate2 --version
whisper-ctranslate2 0.4.1
```
This commit is contained in:
DavHau 2024-03-26 11:37:13 +07:00 committed by mergify[bot]
parent beceb215cc
commit 306e5bff07
3 changed files with 129 additions and 2 deletions

View File

@ -131,6 +131,14 @@
else (lib.head selected).filename
);
noValidFilenameError = filenames:
throw ''
No valid filename found in the list of filenames:
- ${lib.concatStringsSep "\n - " filenames}
System: ${targetPlatform.system}
Python version: ${python3.version}
'';
# Source selectors.
# Prefer to select a wheel from a list of filenames.
# Filenames should already have been filtered on environment usability.
@ -143,7 +151,7 @@
then wheel
else if sdist != null
then sdist
else null;
else noValidFilenameError filenames;
# preferSdistSelector :: [String] -> String
preferSdistSelector = filenames: let
@ -154,7 +162,7 @@
then sdist
else if wheel != null
then wheel
else null;
else noValidFilenameError filenames;
# Get the dependency names out from a list of parsed deps which are
# required due to the current environment.

View File

@ -0,0 +1,57 @@
{self, ...}: {
perSystem = {
pkgs,
lib,
...
}: let
python = pkgs.python3;
pythonAttr = "python${lib.versions.major python.version}${lib.versions.minor python.version}";
pdmConfig = pkgs.writeText "pdm-config.toml" ''
check_update = false
[python]
use_venv = false
'';
pythonEnv = pkgs.writeScriptBin "pythonWith" ''
#!${pkgs.bash}/bin/bash
set -Eeuo pipefail
export PATH="$PATH:${lib.makeBinPath [
pkgs.coreutils
pkgs.pdm
pkgs.yq
]}"
export TMPDIR=$(${pkgs.coreutils}/bin/mktemp -d)
# trap "${pkgs.coreutils}/bin/chmod -R +w '$TMPDIR'; ${pkgs.coreutils}/bin/rm -rf '$TMPDIR'" EXIT
pushd $TMPDIR >/dev/null
# vscode likes to set these for whatever reason and it crashes PDM
unset _PYTHON_SYSCONFIGDATA_NAME _PYTHON_HOST_PLATFORM
echo ${python}/bin/python > .pdm-python
cat <<EOF > pyproject.toml
[project]
name = "temp"
version = "0.0.0"
requires-python = "==${python.version}"
dependencies = [
EOF
for dep in "$@"; do
echo " \"$dep\"," >> pyproject.toml
done
echo "]" >> pyproject.toml
pdm -c ${pdmConfig} lock
popd >/dev/null
# initialize flake template
cat ${./flake-template.nix} > $TMPDIR/flake.nix
sed -i 's\__PYTHON_ATTR__\${pythonAttr}\g' $TMPDIR/flake.nix
# enter dev-shell
nix develop $TMPDIR -c $SHELL
'';
in {
packages.__pythonEnv = pythonEnv;
};
}

View File

@ -0,0 +1,62 @@
{
description = "My flake with dream2nix packages";
inputs = {
dream2nix.url = "github:nix-community/dream2nix/pythonEnv";
nixpkgs.follows = "dream2nix/nixpkgs";
flake-parts.url = "github:hercules-ci/flake-parts";
};
outputs = inputs @ {
self,
dream2nix,
flake-parts,
nixpkgs,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
];
perSystem = {
pkgs,
system,
...
}: let
module = {
config,
dream2nix,
...
}: {
imports = [
dream2nix.modules.dream2nix.WIP-python-pdm
];
deps = {nixpkgs, ...}: {
python = nixpkgs.__PYTHON_ATTR__;
};
pdm.lockfile = ./pdm.lock;
pdm.pyproject = ./pyproject.toml;
mkDerivation = {
src = ./.;
};
};
package = dream2nix.lib.evalModules {
packageSets.nixpkgs = inputs.nixpkgs.legacyPackages.${system};
modules = [
module
{
paths.projectRoot = ./.;
paths.projectRootFile = "flake.nix";
paths.package = ./.;
}
];
};
in {
devShells.default = package.devShell;
};
};
}