Skip to content

Commit 5c3eec6

Browse files
committed
Merge branch 'pyproject-location'
2 parents 4e446c6 + 37cb967 commit 5c3eec6

File tree

11 files changed

+142
-32
lines changed

11 files changed

+142
-32
lines changed

devenv.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
cat > docs/languages-all.md <<EOF
117117
\`\`\`nix
118118
${lib.concatStringsSep "\n " (map (lang: "languages.${lang}.enable = true;") (builtins.attrNames config.languages))}
119-
\`\`\`
119+
\`\`\`
120120
EOF
121121
'';
122122

examples/python-directory/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# Devenv
3+
.devenv*
4+
devenv.local.nix
5+

examples/python-directory/.test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -exu
3+
POETRY_VENV="$PWD/directory/.venv"
4+
[ -d "$POETRY_VENV" ]
5+
[ "$(command -v python)" = "$POETRY_VENV/bin/python" ]
6+
python --version
7+
poetry --version
8+
python -c 'import requests'
9+
cd directory
10+
[ "$(poetry env info --path)" = "$POETRY_VENV" ]
11+
poetry run python -c 'import requests'

examples/python-directory/devenv.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ pkgs, config, ... }:
2+
3+
{
4+
languages.python = {
5+
enable = true;
6+
directory = "./directory";
7+
poetry = {
8+
enable = true;
9+
install.enable = true;
10+
activate.enable = true;
11+
};
12+
};
13+
}

examples/python-directory/devenv.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inputs:
2+
nixpkgs-python:
3+
url: github:cachix/nixpkgs-python
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[build-system]
2+
requires = ["poetry-core"]
3+
build-backend = "poetry.core.masonry.api"
4+
5+
[tool.poetry]
6+
name = "python-directory"
7+
version = "0.1.0"
8+
description = ""
9+
authors = [
10+
"Bob van der Linden <bobvanderlinden@gmail.com>",
11+
"Matthias Thym <git@thym.at>"
12+
]
13+
readme = "README.md"
14+
15+
[tool.poetry.dependencies]
16+
python = "^3.11"
17+
requests = "^2.30"

examples/python-poetry/.test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env bash
22
set -exu
3+
34
POETRY_VENV="$PWD/.venv"
45
[ -d "$POETRY_VENV" ]
56
[ "$(poetry env info --path)" = "$POETRY_VENV" ]
67
[ "$(command -v python)" = "$POETRY_VENV/bin/python" ]
78
python --version
89
poetry --version
910
poetry run python -c "import os; print(os.environ['LD_LIBRARY_PATH'])"
10-
ls .devenv/profile/lib
1111
poetry run python -c 'import numpy'
1212
python -c 'import numpy'
1313
python -c 'import pjsua2'

examples/python-poetry/devenv.nix

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
config.languages.python.package.pkgs.pjsua2
77
];
88

9-
# this envvar can be removed and the lib can be moved into
10-
# languages.python.libraries when we start working against env-venv
11-
env.LD_LIBRARY_PATH = lib.makeLibraryPath [
12-
# A native dependency of numpy
13-
pkgs.zlib
14-
];
15-
169
languages.python = {
1710
enable = true;
18-
poetry.enable = true;
11+
poetry = {
12+
enable = true;
13+
install = {
14+
enable = true;
15+
installRootPackage = false;
16+
onlyInstallRootPackage = false;
17+
compile = false;
18+
quiet = false;
19+
groups = [ ];
20+
ignoredGroups = [ ];
21+
onlyGroups = [ ];
22+
extras = [ ];
23+
allExtras = false;
24+
verbosity = "no";
25+
};
26+
activate.enable = true;
27+
package = pkgs.poetry;
28+
};
1929
};
2030
}

examples/python-poetry/devenv.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inputs:
2+
nixpkgs-python:
3+
url: github:cachix/nixpkgs-python

examples/python-poetry/pyproject.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
[build-system]
2+
requires = ["poetry-core"]
3+
build-backend = "poetry.core.masonry.api"
4+
15
[tool.poetry]
26
name = "python-poetry"
3-
version = "0.1.0"
7+
version = "0.2.0"
48
description = ""
5-
authors = ["Bob van der Linden <bobvanderlinden@gmail.com>"]
9+
authors = [
10+
"Bob van der Linden <bobvanderlinden@gmail.com>",
11+
"Matthias Thym <git@thym.at>"
12+
]
613
readme = "README.md"
714

815
[tool.poetry.dependencies]
916
python = "^3.10"
1017
numpy = "^1.24.1"
11-
12-
13-
[build-system]
14-
requires = ["poetry-core"]
15-
build-backend = "poetry.core.masonry.api"

src/modules/languages/python.nix

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ let
3939
};
4040

4141
initVenvScript = pkgs.writeShellScript "init-venv.sh" ''
42-
# Make sure any tools are not attempting to use the python interpreter from any
42+
# Make sure any tools are not attempting to use the Python interpreter from any
4343
# existing virtual environment. For instance if devenv was started within an venv.
4444
unset VIRTUAL_ENV
4545
46-
VENV_PATH="${config.env.DEVENV_STATE}/venv"
46+
VENV_PATH="${config.env.DEVENV_STATE}/${lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''}venv"
4747
4848
profile_python="$(${readlink} ${package.interpreter})"
4949
devenv_interpreter_path="$(${pkgs.coreutils}/bin/cat "$VENV_PATH/.devenv_interpreter" 2> /dev/null || echo false )"
@@ -82,22 +82,22 @@ let
8282
initPoetryScript = pkgs.writeShellScript "init-poetry.sh" ''
8383
function _devenv_init_poetry_venv
8484
{
85-
# Make sure any tools are not attempting to use the python interpreter from any
85+
# Make sure any tools are not attempting to use the Python interpreter from any
8686
# existing virtual environment. For instance if devenv was started within an venv.
8787
unset VIRTUAL_ENV
8888
89-
# Make sure poetry's venv uses the configured python executable.
89+
# Make sure poetry's venv uses the configured Python executable.
9090
${cfg.poetry.package}/bin/poetry env use --no-interaction --quiet ${package.interpreter}
9191
}
9292
9393
function _devenv_poetry_install
9494
{
95-
local POETRY_INSTALL_COMMAND=(${cfg.poetry.package}/bin/poetry install --no-interaction ${lib.concatStringsSep " " cfg.poetry.install.arguments})
95+
local POETRY_INSTALL_COMMAND=(${cfg.poetry.package}/bin/poetry install --no-interaction ${lib.concatStringsSep " " cfg.poetry.install.arguments} ${lib.optionalString (cfg.directory != config.devenv.root) ''--directory=${cfg.directory}''})
9696
# Avoid running "poetry install" for every shell.
97-
# Only run it when the "poetry.lock" file or python interpreter has changed.
97+
# Only run it when the "poetry.lock" file or Python interpreter has changed.
9898
# We do this by storing the interpreter path and a hash of "poetry.lock" in venv.
99-
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[@]}"
100-
local POETRY_CHECKSUM_FILE="$DEVENV_ROOT"/.venv/poetry.lock.checksum
99+
local ACTUAL_POETRY_CHECKSUM="${package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}pyproject.toml):$(${pkgs.nix}/bin/nix-hash --type sha256 "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}poetry.lock):''${POETRY_INSTALL_COMMAND[@]}"
100+
local POETRY_CHECKSUM_FILE="$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}.venv/poetry.lock.checksum
101101
if [ -f "$POETRY_CHECKSUM_FILE" ]
102102
then
103103
read -r EXPECTED_POETRY_CHECKSUM < "$POETRY_CHECKSUM_FILE"
@@ -116,7 +116,7 @@ let
116116
fi
117117
}
118118
119-
if [ ! -f pyproject.toml ]
119+
if [ ! -f "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}pyproject.toml ]
120120
then
121121
echo "No pyproject.toml found. Run 'poetry init' to create one." >&2
122122
else
@@ -125,7 +125,7 @@ let
125125
_devenv_poetry_install
126126
''}
127127
${lib.optionalString cfg.poetry.activate.enable ''
128-
source "$DEVENV_ROOT"/.venv/bin/activate
128+
source "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}.venv/bin/activate
129129
''}
130130
fi
131131
'';
@@ -173,6 +173,17 @@ in
173173
example = "3.11 or 3.11.2";
174174
};
175175

176+
directory = lib.mkOption {
177+
type = lib.types.str;
178+
default = config.devenv.root;
179+
defaultText = lib.literalExpression "config.devenv.root";
180+
description = ''
181+
The Python project's root directory. Defaults to the root of the devenv project.
182+
Can be an absolute path or one relative to the root of the devenv project.
183+
'';
184+
example = "./directory";
185+
};
186+
176187
venv.enable = lib.mkEnableOption "Python virtual environment";
177188

178189
venv.requirements = lib.mkOption {
@@ -205,6 +216,16 @@ in
205216
default = false;
206217
description = "Whether the root package (your project) should be installed. See `--no-root`";
207218
};
219+
onlyInstallRootPackage = lib.mkOption {
220+
type = lib.types.bool;
221+
default = false;
222+
description = "Whether to only install the root package (your project) should be installed, but no dependencies. See `--only-root`";
223+
};
224+
compile = lib.mkOption {
225+
type = lib.types.bool;
226+
default = false;
227+
description = "Whether `poetry install` should compile Python source files to bytecode.";
228+
};
208229
quiet = lib.mkOption {
209230
type = lib.types.bool;
210231
default = false;
@@ -213,7 +234,17 @@ in
213234
groups = lib.mkOption {
214235
type = lib.types.listOf lib.types.str;
215236
default = [ ];
216-
description = "Which dependency-groups to install. See `--with`.";
237+
description = "Which dependency groups to install. See `--with`.";
238+
};
239+
ignoredGroups = lib.mkOption {
240+
type = lib.types.listOf lib.types.str;
241+
default = [ ];
242+
description = "Which dependency groups to ignore. See `--without`.";
243+
};
244+
onlyGroups = lib.mkOption {
245+
type = lib.types.listOf lib.types.str;
246+
default = [ ];
247+
description = "Which dependency groups to exclusively install. See `--only`.";
217248
};
218249
extras = lib.mkOption {
219250
type = lib.types.listOf lib.types.str;
@@ -225,9 +256,17 @@ in
225256
default = false;
226257
description = "Whether to install all extras. See `--all-extras`.";
227258
};
259+
verbosity = lib.mkOption {
260+
type = lib.types.enum [ "no" "little" "more" "debug" ];
261+
default = "no";
262+
description = "What level of verbosity the output of `poetry install` should have.";
263+
};
264+
};
265+
activate.enable = lib.mkOption {
266+
type = lib.types.bool;
267+
default = false;
268+
description = "Whether to activate the poetry virtual environment automatically.";
228269
};
229-
activate.enable = lib.mkEnableOption "activate the poetry virtual environment automatically";
230-
231270
package = lib.mkOption {
232271
type = lib.types.package;
233272
default = pkgs.poetry;
@@ -240,11 +279,18 @@ in
240279
config = lib.mkIf cfg.enable {
241280
languages.python.poetry.install.enable = lib.mkIf cfg.poetry.enable (lib.mkDefault true);
242281
languages.python.poetry.install.arguments =
243-
lib.optional (!cfg.poetry.install.installRootPackage) "--no-root" ++
282+
lib.optional cfg.poetry.install.onlyInstallRootPackage "--only-root" ++
283+
lib.optional (!cfg.poetry.install.installRootPackage && !cfg.poetry.install.onlyInstallRootPackage) "--no-root" ++
284+
lib.optional cfg.poetry.install.compile "--compile" ++
244285
lib.optional cfg.poetry.install.quiet "--quiet" ++
245286
lib.optionals (cfg.poetry.install.groups != [ ]) [ "--with" ''"${lib.concatStringsSep "," cfg.poetry.install.groups}"'' ] ++
287+
lib.optionals (cfg.poetry.install.ignoredGroups != [ ]) [ "--without" ''"${lib.concatStringsSep "," cfg.poetry.install.ignoredGroups}"'' ] ++
288+
lib.optionals (cfg.poetry.install.onlyGroups != [ ]) [ "--only" ''"${lib.concatStringsSep " " cfg.poetry.install.onlyGroups}"'' ] ++
246289
lib.optionals (cfg.poetry.install.extras != [ ]) [ "--extras" ''"${lib.concatStringsSep " " cfg.poetry.install.extras}"'' ] ++
247-
lib.optional cfg.poetry.install.allExtras "--all-extras";
290+
lib.optional cfg.poetry.install.allExtras "--all-extras" ++
291+
lib.optional (cfg.poetry.install.verbosity == "little") "-v" ++
292+
lib.optional (cfg.poetry.install.verbosity == "more") "-vv" ++
293+
lib.optional (cfg.poetry.install.verbosity == "debug") "-vvv";
248294

249295
languages.python.poetry.activate.enable = lib.mkIf cfg.poetry.enable (lib.mkDefault true);
250296

0 commit comments

Comments
 (0)