Skip to content

Commit

Permalink
Add languages.python.version
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Jun 2, 2023
1 parent 4426f03 commit 92ffb47
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/python/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -ex
[ "$(command -v python)" = "$PWD/.devenv/state/venv/bin/python" ]
python --version | grep "3.7.16"
6 changes: 6 additions & 0 deletions examples/python/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs, ... }:

{
languages.python.enable = true;
languages.python.version = "3.7.16";
}
5 changes: 5 additions & 0 deletions examples/python/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
nixpkgs-python:
url: github:cachix/nixpkgs-python
24 changes: 23 additions & 1 deletion src/modules/languages/python.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
{ pkgs, config, lib, ... }:
{ pkgs, config, lib, inputs, ... }:

let
cfg = config.languages.python;

nixpkgs-python = inputs.nixpkgs-python or (throw ''
To use languages.python.version, you need to add the following to your devenv.yaml:
inputs:
nixpkgs-python:
url: github:cachix/nixpkgs-python
'');

venvPath = "${config.env.DEVENV_STATE}/venv";

initVenvScript = pkgs.writeShellScript "init-venv.sh" ''
Expand Down Expand Up @@ -96,6 +104,16 @@ in
description = "The Python package to use.";
};

version = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The Python version to use.
This automatically sets the `languages.python.package` using [nixpkgs-python](https://github.com/cachix/nixpkgs-python).
'';
example = "3.11 or 3.11.2";
};

venv.enable = lib.mkEnableOption "Python virtual environment";

poetry = {
Expand Down Expand Up @@ -138,6 +156,10 @@ in

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

languages.python.package = lib.mkMerge [
(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"))
];

packages = [
cfg.package
] ++ (lib.optional cfg.poetry.enable cfg.poetry.package);
Expand Down

0 comments on commit 92ffb47

Please sign in to comment.