From e6873dbd775d03522e254c90320a4b25b565e590 Mon Sep 17 00:00:00 2001 From: Evie Litherland-Smith Date: Tue, 17 Dec 2024 11:54:22 +0000 Subject: [PATCH] Add pyshell.nix helper function for making wrapped python shells Just import, optionally changing python version or adding extra packages (e.g. poetry) to the shell Update default python env Remove pyshell template from Emacs templates due to new change --- system/home/emacs/templates | 12 --------- system/home/programming/python/default.nix | 19 ++++++------- system/home/programming/python/pyshell.nix | 31 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 system/home/programming/python/pyshell.nix diff --git a/system/home/emacs/templates b/system/home/emacs/templates index 8f0caf85..a243638f 100644 --- a/system/home/emacs/templates +++ b/system/home/emacs/templates @@ -9,18 +9,6 @@ nix-mode > "};" n "in" n > "fhs.env") -(pyshell "let" n - > "pkgs = import " (p "") " { };" n - > "fix-python = (builtins.getFlake \"github:GuillaumeDesforges/fix-python\").packages.${pkgs.system}.default;" n - > "python3 = pkgs." (p "python3") ";" n - "in" n - "pkgs.mkShellNoCC {" n - > "packages = [python3 fix-python pkgs.uv];" n - > "shellHook = ''" n - > "export PYTHONPATH=$(readlink -f " (p "./.") "):$PYTHONPATH" n - > "export MPLBACKEND=" (p "TkAgg") n - > "'';"n - "}") ;; Local Variables: ;; mode: lisp-data diff --git a/system/home/programming/python/default.nix b/system/home/programming/python/default.nix index 7808eee4..3b5739a4 100644 --- a/system/home/programming/python/default.nix +++ b/system/home/programming/python/default.nix @@ -1,23 +1,24 @@ { pkgs, ... }: -# MPLBACKEND -{ - home.packages = with pkgs; [ - (python3.withPackages ( +let + pythonEnv = ( + pkgs.python312.withPackages ( ps: with ps; [ python-lsp-server - pylsp-rope - rope isort flake8 mypy - pyyaml numpy xarray netcdf4 matplotlib ] - )) - ruff + ) + ); +in +{ + home.packages = [ + pkgs.ruff + pythonEnv ]; xdg.configFile."ruff/pyproject.toml".source = ./ruff.toml; } diff --git a/system/home/programming/python/pyshell.nix b/system/home/programming/python/pyshell.nix new file mode 100644 index 00000000..5777e876 --- /dev/null +++ b/system/home/programming/python/pyshell.nix @@ -0,0 +1,31 @@ +{ + pkgs ? import { }, + python3 ? pkgs.python3, + extraPackages ? with pkgs; [ uv ], +}: +let + inherit (pkgs) lib; + ldlibs = lib.makeLibraryPath ( + with pkgs; + [ + stdenv.cc.cc + glibc + zlib + zstd + ] + ); + pythonWrapped = pkgs.symlinkJoin { + name = "python"; + paths = [ python3 ]; + buildInputs = [ pkgs.makeWrapper ]; + postBuild = '' + for file in $out/bin/*; do wrapProgram "$file" --prefix LD_LIBRARY_PATH : "${ldlibs}"; done + ''; + }; +in +pkgs.mkShellNoCC { + packages = [ pythonWrapped ] ++ extraPackages; + shellHook = '' + [ -f .venv/bin/activate ] && source .venv/bin/activate + ''; +}