-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython-envs-poetry.nix
100 lines (98 loc) · 4.02 KB
/
python-envs-poetry.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
topLevel@{ inputs, flake-parts-lib, ... }: {
imports = [
./common.nix
./vscode.nix
./python-vscode.nix
./python-package.nix
inputs.flake-parts.flakeModules.flakeModules
];
flake.flakeModules.pythonEnvsPoetry = flakeModule: {
imports = [
topLevel.config.flake.flakeModules.common
topLevel.config.flake.flakeModules.vscode
topLevel.config.flake.flakeModules.pythonVscode
topLevel.config.flake.flakeModules.pythonPackage
];
options.perSystem = flake-parts-lib.mkPerSystemOption
({ lib, system, pkgs, ... }: {
ml-ops.common = common: {
options.poetry2nix.pkgs = lib.mkOption {
description = ''
The nix package set to use for poetry2nix.
It is by default set to the nixpkgs from `nix-ml-ops`'s lock file with a python package specified by `perSystem.ml-ops.common.pythonPackage`.
'';
defaultText = lib.literalExpression ''
pkgs.appendOverlays [
(self: super: {
''${common.config.pythonPackage.base-package.pythonAttr} = lib.pipe super.''${common.config.pythonPackage.base-package.pythonAttr} common.config.pythonPackage.pipe;
})
]
'';
default = pkgs.appendOverlays [
(self: super: {
# Set the config.pythonPackage to poetry2nix's `pkgs` so that Python's dependents are rebuilt against the custom Python specified in config.pythonPackage.
${common.config.pythonPackage.base-package.pythonAttr} = lib.pipe super.${common.config.pythonPackage.base-package.pythonAttr} common.config.pythonPackage.pipe;
})
];
};
options.poetry2nix.python = lib.mkOption {
default = common.config.poetry2nix.pkgs.${common.config.pythonPackage.base-package.pythonAttr};
};
options.poetry2nix.poetry2nixLib = lib.mkOption {
default = (inputs.poetry2nix.lib.mkPoetry2Nix {
pkgs = common.config.poetry2nix.pkgs;
});
};
};
ml-ops.devcontainer = devcontainer: {
options.poetry-add-requirements-txt = lib.mkOption {
type = lib.types.package;
default = devcontainer.config.poetry2nix.poetry2nixLib.mkPoetryApplication {
projectDir = inputs.poetry-add-requirements-txt;
preferWheels = true;
};
};
config.devenvShellModule = {
scripts.import-requirements-to-poetry.exec = ''
if [ ! -f ./pyproject.toml ]
then
poetry init --no-interaction
fi
if [ ! -f ./poetry.lock ]
then
if [ -f ./requirements.txt ]
then
${devcontainer.config.poetry-add-requirements-txt}/bin/poeareq ./requirements.txt
fi
if [ -f ./requirements-dev.txt ]
then
${devcontainer.config.poetry-add-requirements-txt}/bin/poeareq -D ./requirements-dev.txt
fi
fi
'';
languages.python.enable = true;
languages.python.poetry.enable = true;
};
};
ml-ops.runtime = runtime: {
options.poetryEnvArgs = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
config.poetryEnvArgs = {
preferWheels = lib.mkDefault true;
projectDir = lib.mkDefault "${flakeModule.self}";
python = runtime.config.poetry2nix.python;
groups = [ ];
};
options.poetryEnv = lib.mkOption {
type = lib.types.package;
default = runtime.config.poetry2nix.poetry2nixLib.mkPoetryEnv runtime.config.poetryEnvArgs;
};
config.devenvShellModule.packages = lib.mkIf (builtins.pathExists "${flakeModule.self}/poetry.lock") [
runtime.config.poetryEnv
];
};
});
};
}