TIL: Using nix run to lint one-off Python scripts
In my previous post about Nix, I pointed out the benefits of using Nix for throwaway scripts.
I have a repo where I use this for a little Python script. I don’t want the overhead of a Poetry (or whatever the current hotness is) environment just for a 50 or so line Python script, so I use a Nix shebang.
But what about linting? In the Justfile,1 I have a task for running the quadruple threat of Python linting tools: Black, isort, mypy, and flake8.
The commands I’m using are:
nix run nixpkgs#isort -- ./my_script.py
nix run nixpkgs#black -- ./my_script.py
nix run nixpkgs#mypy -- ./my_script.py
nix-shell -p python312Packages.flake8 --run "flake8 ./my_script.py --ignore=E203,E265,E501,E701"
As for the --ignore
on Flake8: E265 gets triggered by Nix shebangs. E501 is
line length, and Black uses longer line
lengths.
Black’s documentation suggests disabling
E203 and E701.
I’m using experimental-features = nix-command flakes
in my
~/.config/nix/nix.conf
, otherwise the nix run
wouldn’t work.
One could also use Git hooks, perhaps with the excellent pre-commit framework, but I tend to prefer moving linting to a Makefile/Justfile, because some people don’t do the pre-commit thing.
They’re Makefiles but with fewer headaches, as I have written previously.
[return]