Hello,
I have a python script that depends on the selenium library and chromedriver; it constantly breaks due to system updates. I thought it might be a good candidate to employ nix
and hopefully get something that works reliably over time. I also want to learn more about flakes.
I’ve spent the morning trying to figure out how to make the most basic and bare-bones flake possible to do this, hoping to avoid extra dependencies like flake-utils
or poetry. I’m only interested in running on aarch64-darwin
for now.
My goal is to be able to run nix run
and have that result in python myscript.py
being called.
Unfortunately, I can’t seem to figure out what the best approach for even this simple task is. It looks like I might need to use buildPythonApplication
? Nixpkgs 23.11 manual | Nix & NixOS
Here is what I have so far, although I’ve now figured out that shellHook
doesn’t do what I thought it would.
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
system = "aarch64-darwin";
pkgs = nixpkgs.legacyPackages.${system};
python = pkgs.python3.withPackages (ps: with ps; [ selenium ]);
in
{
packages.${system}.default =
with import nixpkgs { inherit system; };
pkgs.mkShell {
nativeBuildInputs = [ python ];
shellHook = ''
${python} ./myscript.py
'';
};
};
}
I thought it might help me if anyone would be willing to contribute an example flake or two, ideally not depending on flake-utils
just for instructive purposes, that upon nix run
would:
- run a bash command:
echo foo
- run an existing bash script,
myscript.sh
- run an existing bash script that depends on e.g.
ripgrep
- run a python command:
print("foo")
- run an existing python script:
./myscript.py
- run an existing python script that depends on an arbitrary pypi package
My goal is mainly for adapting several existing single-purpose scripts. Obviously asking for 6 example flakes is a lot, but if anyone things they can pitch in a canonical, best-practices, dependency-free example of a flake that would work on a single system (aarch64-darwin
in my case), I’d really appreciate it! I thought this would be fairly trivial, but after tinkering, googling, and reading all morning, I’d love to have some pointers.
Many thanks in advance.
EDIT: Changed unordered list to numbered to allow specifying which task something was directed at accomplishing.