diff --git a/examples/python/.test.sh b/examples/python/.test.sh new file mode 100755 index 000000000..08910a4a6 --- /dev/null +++ b/examples/python/.test.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -ex +[ "$(command -v python)" = "$PWD/.devenv/state/venv/bin/python" ] +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..ab1c2e561 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);