From ecd91d5063e7b432b751ce706ec8b3559a89eeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domen=20Ko=C5=BEar?= Date: Fri, 2 Jun 2023 13:34:01 +0100 Subject: [PATCH] Add languages.python.version --- examples/python/.test.sh | 3 +++ examples/python/devenv.nix | 6 ++++++ examples/python/devenv.yaml | 5 +++++ src/modules/languages/python.nix | 24 +++++++++++++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 examples/python/.test.sh create mode 100644 examples/python/devenv.nix create mode 100644 examples/python/devenv.yaml diff --git a/examples/python/.test.sh b/examples/python/.test.sh new file mode 100755 index 000000000..dd99dd0d7 --- /dev/null +++ b/examples/python/.test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -ex +python --version | grep "3.7.16" diff --git a/examples/python/devenv.nix b/examples/python/devenv.nix new file mode 100644 index 000000000..ab39b4534 --- /dev/null +++ b/examples/python/devenv.nix @@ -0,0 +1,6 @@ +{ pkgs, ... }: + +{ + languages.python.enable = true; + languages.python.version = "3.7.16"; +} diff --git a/examples/python/devenv.yaml b/examples/python/devenv.yaml new file mode 100644 index 000000000..21272489a --- /dev/null +++ b/examples/python/devenv.yaml @@ -0,0 +1,5 @@ +inputs: + nixpkgs: + url: github:NixOS/nixpkgs/nixpkgs-unstable + nixpkgs-python: + url: github:cachix/nixpkgs-python \ No newline at end of file diff --git a/src/modules/languages/python.nix b/src/modules/languages/python.nix index 807fecf0f..d644c277d 100644 --- a/src/modules/languages/python.nix +++ b/src/modules/languages/python.nix @@ -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" '' @@ -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 = { @@ -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);