Packaging/Python: Difference between revisions

Add a section to explain how to locally test a packaged Python library
Abus (talk | contribs)
m Fixed formatting error with note
 
(5 intermediate revisions by one other user not shown)
Line 24: Line 24:


   inputs = {
   inputs = {
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
     flake-utils.url = "github:numtide/flake-utils";
     flake-utils.url = "github:numtide/flake-utils";
   };
   };
Line 32: Line 32:
       let
       let
         pkgs = import nixpkgs { inherit system; };
         pkgs = import nixpkgs { inherit system; };
        python = pkgs.python3;
         # replace ./derivation.nix with the path (relative to this file) to your derivation
         # replace ./default.nix with the path to your derivation, relative to this file
         mypackage = pkgs.python3Packages.callPackage ./derivation.nix {
         mypackage = pkgs.callPackage ./default.nix {
          inherit (python.pkgs) buildPythonPackage package names of dependencies here;
           lib = pkgs.lib;
           lib = pkgs.lib;
         };
         };
       in {
       in {
         devShells.default = pkgs.mkShell {
         packages.default = pkgs.python3.withPackages(_: [ mypackage ]);
          buildInputs = [ python mypackage ];
        };
       });
       });
}
}
</syntaxhighlight>Replace "mypackage" with your package name, and "package names of dependencies here" with a (space-separated) list of the dependencies you listed in your buildPythonPackage call.
</syntaxhighlight>Replace "mypackage" with your package name. Then, run the flake to get into a python REPL with your package, ready to be imported:<syntaxhighlight lang="shell-session">
 
$ nix run
Next, enter the devShell and launch a Python REPL:<syntaxhighlight lang="shell-session">
$ nix develop
$ python
Python 3.11.10 (main, Sep  7 2024, 01:03:31) [GCC 13.2.0] on linux
Python 3.11.10 (main, Sep  7 2024, 01:03:31) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Type "help", "copyright", "credits" or "license" for more information.
Line 56: Line 49:


== Pip and Virtualenv enabled nix-shell ==
== Pip and Virtualenv enabled nix-shell ==
It might be the case that you simply need to prototype fast small projects with <code>pip</code> and <code>virtualenv</code> without the need for relying on the dependencies being already packaged in nixpkgs.  
It might be the case that you simply need to prototype fast small projects with <code>pip</code> and <code>virtualenv</code> without the need for relying on the dependencies being already packaged in nixpkgs.
{{Notice|Keep in mind that the virtualenv symlinks will be invalidated if you update your system!||
 
{{Note|Keep in mind that the virtualenv symlinks will be invalidated if you update your system!}}
   
   
For a local working python environment you can use the following <code>shell.nix</code><ref>https://groups.google.com/forum/#!topic/nix-devel/3qPfwCAV3GE</ref>.
For a local working python environment you can use the following <code>shell.nix</code><ref>https://groups.google.com/forum/#!topic/nix-devel/3qPfwCAV3GE</ref>.