Skip to content

Instantly share code, notes, and snippets.

@joriatyBen
Last active May 1, 2020 12:38
Show Gist options
  • Save joriatyBen/0949d00ab167df7ef89b9cddae7324a1 to your computer and use it in GitHub Desktop.
Save joriatyBen/0949d00ab167df7ef89b9cddae7324a1 to your computer and use it in GitHub Desktop.

This is a result from python.section.md and it has a lack of full explanation. For me its kinda reminder how to start.

  1. Global installation of Python in configuration.nix in environment.systemPackages with e.g. (python36.withPackages(ps: with ps; [ numpy ])) here i chose python 3.6 any ohter version should work that way.
  2. Create a nix expression like python.nix with all dependencies for Python:
with import <nixpkgs> {};

(python35.withPackages (ps: [ps.numpy ps.toolz])).env
  1. Set up temporary Python environment with nix-shell: $ nix-shell python.nix
  2. Run/execute any python script in nix-shell: $ nix-shell python.nix --run "python3 mypyscript.py"

Or try this as an alternative concept:

Check it in the manual

  1. imperative pip in a virtualenv like in other OS. default.nix:
with import <nixpkgs> {};
let
  pythonEnv = python37.withPackages (ps: [
    ps.virtualenv
    ps.pip
  ]);
in mkShell {
  buildInputs = [
    pythonEnv
    hello
  ];

  shellHook = ''
    virtualenv --no-setuptools venv
    export PATH=$PWD/venv/bin:$PATH
    export PYTHONPATH=venv/lib/python3.7/site-packages/:$PYTHONPATH
    pip install -r requirements.txt
  '';
}

1.1 or just default.nix:

with import <nixpkgs> {};
with python37Packages;
stdenv.mkDerivation {
  name = "gmailenv";
  src = null;
  buildInputs = [
    python37Full
    python37Packages.virtualenv
    python37Packages.pip
  ];
  shellHook = ''
    virtualenv --no-setuptools venv
    export PATH=$PWD/venv/bin:$PATH
    export PYTHONPATH=venv/lib/python3.7/site-packages/:$PYTHONPATH
    pip install -r requirements.txt
  '';
}
  1. run it $ nix-shell default.nix
  2. In an optional requirements.txt file list whatever python modules e.g. matplotlib, pandas
  3. Therefore you could look for packages e.g. numpy $ nix-env -qa '.*numpy.*'
  4. Exit $ nix-shell like Ctrl+D or type $ exit.
  5. Now u should be ready to have an easy imperativ venv with pip
  6. After you run $ nix-shell default.nix run virtualenv venv
  7. source venv/bin/activate
  8. if you want to leave deactivate followed by Ctrl + D) <- no need, you should be in your virtualenv after starting the shell.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment