Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Last active February 11, 2024 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/9d86fe4375826e76cf136d3c08eceafe to your computer and use it in GitHub Desktop.
Save pmarreck/9d86fe4375826e76cf136d3c08eceafe to your computer and use it in GitHub Desktop.
A basic flake.nix file for cross-platform Python projects. Use with "nix develop". pip, etc. should work via venv.
{
description = "A flake for pythonification";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }@inputs:
flake-utils.lib.eachSystem ["x86_64-darwin" "aarch64-darwin" "x86_64-linux" "aarch64-linux"] (system:
let
pkgs = import nixpkgs { inherit system; };
pythonPackages = pkgs.python3Packages;
venvDir = "./env";
runPackages = with nixpkgs; [
pythonPackages.python
pythonPackages.venvShellHook
];
devPackages = with nixpkgs; runPackages ++ [
pythonPackages.pylint
pythonPackages.flake8
pythonPackages.black
];
postShellHook = ''
PYTHONPATH=\$PWD/\${venvDir}/\${pythonPackages.python.sitePackages}/:\$PYTHONPATH
'';
in {
runShell = pkgs.mkShell {
inherit venvDir;
name = "pythonify-run";
packages = runPackages;
postShellHook = postShellHook;
};
devShells = {
default = pkgs.mkShell {
inherit venvDir;
name = "pythonify-dev";
packages = devPackages;
postShellHook = postShellHook;
};
};
});
}
@pmarreck
Copy link
Author

If you copy this into an existing non-Nixified git-cloned python project, remember to "git add flake.nix" or it won't be "seen".

Also, may need to modify based on your particular requirements.

I haven't tried it yet on linux, but it works on macOS.

You access it/drop into its environment via nix develop.

I'm not 100% sure about the "runShell" definition. May need to modify this for runs and builds that are not "development", such as if you want any build products to be available on your PATH from elsewhere. (Happy to take recommendations here!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment