nix-templates/python/nix/development.nix
2025-07-10 23:07:28 +03:00

79 lines
2 KiB
Nix

{ inputs, ... }:
let
self = inputs.self;
nixpkgs = inputs.nixpkgs;
/*
* Change this value ({major}.{min}) to
* update the Python virtual-environment
* version. When you do this, make sure
* to delete the `.venv` directory to
* have the hook rebuild it for the new
* version, since it won't overwrite an
* existing one. After this, reload the
* development shell to rebuild it.
* You'll see a warning asking you to
* do this when version mismatches are
* present. For safety, removal should
* be a manual step, even if trivial.
*/
version = "3.13";
in
{
flake.overlays.dev = nixpkgs.lib.composeManyExtensions [
# NOTE: Put development overlays here.
];
perSystem = { system, pkgs-dev, lib, ... }:
let
concatMajorMinor = v:
pkgs-dev.lib.pipe v [
pkgs-dev.lib.versions.splitVersion
(pkgs-dev.lib.sublist 0 2)
pkgs-dev.lib.concatStrings
];
python = pkgs-dev."python${concatMajorMinor version}";
in
{
_module.args.pkgs-dev = import nixpkgs {
inherit system;
overlays = [ self.overlays.dev ];
};
devShells.default = pkgs-dev.mkShellNoCC rec {
venvDir = ".venv";
postShellHook = ''
venvVersionWarn() {
local venvVersion
venvVersion="$("$venvDir/bin/python" -c 'import platform; print(platform.python_version())')"
[[ "$venvVersion" == "${python.version}" ]] && return
cat <<EOF
Warning: Python version mismatch: [$venvVersion (venv)] != [${python.version}]
Delete '$venvDir' and reload to rebuild for version ${python.version}
EOF
}
venvVersionWarn
'';
packages = [
pkgs-dev.nil
python.pkgs.venvShellHook
python.pkgs.pip
pkgs-dev.basedpyright
pkgs-dev.ruff
pkgs-dev.ty
pkgs-dev.pre-commit
];
};
};
}