Nix Python

  • can install psycopg2 but can’t pin the specific version via nixpkgs
pyDevEnv = (pkgs.${python}.withPackages
  (ps: with ps; [
	setuptools
	psycopg2
	pandas
	boto3
	botocore
  ]));

“-r” command

  • can’t use this if the requirements file looks like this:
    -r common.txt
    boto3==1.17.2
    botocore==1.20.32
    fake-awsglue==0.0.0.post20190320
    
  • Helper for mach-nix requirements. It looks for includes of the form “-r filename.txt” and recursively inlines them instead as mach-nix doesn’t currently support this feature
{ pkgs, lib }:

let
  lines = file: lib.splitString "\n" (builtins.readFile file);

  # Very simple test to look for lines starting with -r
  isRequire = lib.hasPrefix "-r ";

  # Very simple way to extract filename from a -r line
  requireFile = dir: file: "${dir}/${lib.removePrefix "-r " file}";

  # Return list of lines from a requirements file, resursively including
  # files included using -r
  requires = file:
    let part = lib.partition isRequire (lines file); # right = -r lines, wrong = rest
    in
    part.wrong ++ (
      if builtins.length part.right == 0
      then [ ]
      else builtins.concatMap (x: requires (requireFile (builtins.dirOf file) x)) part.right
    );
in
{
  requirements = file: builtins.concatStringsSep "\n" (requires file);
  • read the requirement.txt file
lib = pkgs.lib;
lib_req = import ./lib.nix { inherit pkgs lib; };
requirements = lib_req requirements.txt; 

Overlays

  • install python globally as an overlay https://ryantm.github.io/nixpkgs/languages-frameworks/python/

encode dependencies in source

#!/usr/bin/env nix-shell #!nix-shell -i python3 -p "python3.withPackages(ps: [ ps.numpy ])" 
import numpy as np a = np.array([1,2]) b = np.array([3,4]) print(f"The dot product of {a} and {b} is: {np.dot(a, b)}")

pkgs.python + mach.mkPython

  • pkgs.python + mach.mkPython cant be used together
  • this initialize 2 different python interpreters and one precedence the other
pyEnv = (pkgs.${python}.withPackages
 (ps: with ps; [
	rasterio
 ]));

pyDevEnv = mach.mkPython {
 requirements = ''
	xarray
	dask
	s3fs
	numpy
	pandas
	typer
 '';
};
in
{
devShells.default = pkgs.mkShellNoCC {
 packages = with pkgs; [
	pyDevEnv
	pyEnv
 ];
};
}

mach-nix providers

  • providers you can specify where the package is being installed from
pyDevEnv = mach.mkPython {
  requirements = ''
	xarray
	dask
	s3fs
	numpy
	pandas
	typer
	rioxarray
  '';
  providers.rioxarray = "wheel,sdist,nixpkgs";
};

Notes mentioning this note

There are no notes linking to this note.